I have a large table which I need to consolidate for reporting purposes. The Table is a data dump from another system. The ROUTE_ID being "Read" is normally a one day task, however, they can span two, three, or even four days including weekends (generally weekends are not worked but there are exceptions. I need to leave the one day routes in the table and show the multi-day routes as a single record. The whole data set contains about 50,000 which is updated every work day. In addition to the table below there are another 20-25 fields I have not shown to keep this a little less dramatic.
The is below a sample of the original data set.
INDEX ROUTE_ID RD_DT ROUTE_TIME Read_Rate ActualReads TotalSkipReads Total_Reads SC01 SC02 1 01B5054T 03-Jul-14 6.2 22 124 9 133 2 01B5057T 01-Apr-14 3.0 13 34 4 38 1 3 01B5057T 02-Apr-14 10.5 11 104 15 119 1 3 10 01B5063T 01-Oct-13 8.0 26 196 10 206 1 4 11 01B5063T 04-Jul-13 4.3 38 153 7 160 2 12 01B5063T 05-Jul-13 1.8 29 44 8 52 15 01C1000W 27-Sep-13 3.9 77 292 5 297 2 16 01C1000W 01-Jul-13 3.8 81 293 16 309 2 17 01C1000W 02-Oct-14 2.6 58 142 10 152 1 4
16356 33R1118W 31-Jan-15 1.3 68 90 1 91 1 16357 33R1118W 02-Feb-15 6.1 60 359 11 370 2 5 16358 33R1118W 04-Feb-15 0.2 60 12 1 13 1
In the above data set I need to see the following when the query is run.
INDEX ROUTE_ID RD_DT ROUTE_TIME Read_Rate ActualReads TotalSkipReads Total_Reads SC01 SC02 Days 1 01B5054T 03-Jul-14 6.2 22 124 9 133 1 2 01B5057T 01-Apr-14 13.5 12 138 19 157 2 3 2 10 01B5063T 01-Oct-13 8.0 26 196 10 206 1 4 1 11 01B5063T 04-Jul-13 6.1 35 197 15 212 2 2 15 01C1000W 27-Sep-13 3.9 77 292 5 297 2 1 16 01C1000W 01-Jul-13 3.8 81 293 16 309 2 1 17 01C1000W 02-Oct-14 2.6 58 142 10 152 1 4 1 16356 33R1118W 31-Jan-15 7.6 62 461 13 474 3 6 3
Read_Rate = Total_Reads/ROUTE_TIME
The other fields are added together when the rows are combined.
I got some help and have this SQL working good, it finds and combines the 2 day routes, but leaves doesn't leave the one day route and also doesn't set the [Days]. I was hoping to make this happen all in one query but my results are always either all records or only the multi-day routes.
SELECT A.INDEX, B.INDEX, A.Route_ID, Right$([A.Route_ID],1) AS RouteType, A.RD_DT, B.RD_DT, A.FirstReadTime, A.LastReadTime, B.FirstReadTime, B.LastReadTime, [SumTotalReads]/([SumHours]+([SumMinutes]/60)) AS ReadRate, [A].[ActualReads]+[B].[ActualReads] AS SumReads, [A].[TotalSkipReads]+[B].[TotalSkipReads] AS SumSkipReads, [SumReads]+[SumSkipReads] AS SumTotalReads, [A].[SC01]+[B].[SC01] AS SumSC01, A.SC02+B.SC02 AS SumSC02
FROM CNTRANS_REAL_LOCAL_QUERY AS A INNER JOIN CNTRANS_REAL_LOCAL_QUERY AS B ON A.Route_ID=B.Route_ID
WHERE (((A.EXCLUDE_PD)=No)
And ((B.EXCLUDE_PD)=No)
And ((B.RD_DT)=A.RD_DT+1))
Or (((B.RD_DT)=A.RD_DT+2))
Or (((B.RD_DT)=A.RD_DT+3))
Or (((B.RD_DT)=A.RD_DT+4))
Or (((B.RD_DT)=A.RD_DT+5))
Or (((B.RD_DT)=A.RD_DT+6));
Of course the more info the query compiles and the more accurate it is the better. One nicey-nice would be to show the first [RD_DT] (Start Read) and the last [RD_DT] (End Read) info for multi-day read sets.
Thanks!