Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Oatmeal
Starting Member
3 Posts |
Posted - 2012-02-19 : 14:25:22
|
| Hi. Below is a list of the 3 tables.Table1-Location = LocationIDTable2-CableBoxes = CableBoxID, LocationID, StartDate, EndDateTable3-Events = CableBoxID, EventIDTable1 is a location table for all locationID. Each location can have only ONE active cablebox but can have a history of other cables boxes that have been used and replaced.. Table2 is a table that contains all information about CableBoxes, which can be current or previous cablebox that is/was tied to the LocationID. It is a 2nd level dimension table which includes a history of any previous cables boxes that tie to a locationID. There is a StartDate and EndDate for all cablebox. Enddate = '12-31-9999' means it is the current cablebox. Other cable boxes with different enddate means it was replaced because of fault.I am trying to find how many CableBoxID have been in each location (lcoationID). I also want to include how many days the previous non-active cables boxes stayed at each location. I want my results to be anything that is greater than 1 for cable boxes in a locaiton. Basically how many cablebox have been send to the location in total but also group by duration(time/days) the results to how long the 'previous' cable boxes stayed at the location. Essentially I want the data so I can analyize for patterns.My in progress statement:SELECT L.LocationID, COUNT(C.CableBoxID)FROM Location L INNER JOINCableBoxes CON L.locationID = C.locationIDINNER JOIN Events EON C.CableBoxID = E.CableboxIDWHERE C.CableBoxID > 1GROUP BY L.LocationIDTHe issue now is how can I calculated the number of days the previous cable boxes stayed at each location and group them by the each cablebox. I am not sure where to start with adding this criteria. This is where I need help.thx |
|
|
Oatmeal
Starting Member
3 Posts |
Posted - 2012-02-19 : 16:28:46
|
| LocationTablePK (Surrogate Key), LocationID12345, 000000112312, 000000212329, 0000003** LocationID could technically be the PK but it is not.CableBoxTableABC1 (Suggogate Key), CableBoxID, LocationID, StartDate, EndDate1z234, 123A2, 0000001, 01-01-2007, 06-01-20071sw23, 132G1, 0000001, 06-02-2007, 10-01-200721aq1, 1awh1, 0000001, 10-02-2007, 12-31-99993123a, 123fr, 0000002, 01-01-2007, 03-01-2008321as, 321j3, 0000002, 03-02-2008, 12-31-999918812, 1ss23, 0000003, 02-15-2007, 12-31-9999** As you can see Location 0000001 have a history with a total 3 cable boxes or prior history of 2 defective boxes, 0000002 has 1 defective box and 0000003 have zero. All 3 currently have a working Cablebox hence EndDate of 12-31-9999EVentTableEVentLogID (Surrogate Key), EventID, Event Name, CableboxIDASDF, 001, Overheat, 123A2REWQ, 002, NoConnection, 132G1VCXZ, 010, VoltageFlux, 123fg** The Event table is huge as it has every event ever recorded. Some events does not mean the cablebox does not work but just totality of the events being sent from the cablebox.What I require on top of the statement is a way to calculate the uptime (days) of the cablebox before they become inoperable. In the data provided above. Defective Cablebox: 123A2 had an uptime of 150 days or 5 (months) x 30 days(days in month) (I am rounding off here). Defective CableBox: 132G1 has 119 days OR 4 months - 1 day and both were in location 0000001.I am not sure how I would write the statement that would use math function to figure out the 'uptime' of old boxes. |
 |
|
|
namman
Constraint Violating Yak Guru
285 Posts |
Posted - 2012-02-19 : 21:42:49
|
| With that data, what is the desired output? |
 |
|
|
Oatmeal
Starting Member
3 Posts |
Posted - 2012-02-20 : 22:06:52
|
| Output something like this:LocationID, CableBoxID, uptime_days0000001, 132G1, 1190000001, 123A2, 1500000002, 123fr, 425Basically, LocationID grouped with associated cablebox and the # of days on previous cables boxes that were at the same location. Ignoring the current cable box. In essence showing the history of replaced cable boxes and uptime(days) grouped to locationID. |
 |
|
|
|
|
|
|
|