Leighton21
Technical User
Hi All,
I have 2 tables
Table 1
ItemID Start End Hours
D1 01/06/2009 6:12 01/06/2009 6:17 0.10
D1 01/06/2009 7:43 01/06/2009 8:39 0.94
D1 01/06/2009 8:49 01/06/2009 9:45 0.93
D1 01/06/2009 9:49 01/06/2009 9:53 0.07
D1 01/06/2009 10:05 01/06/2009 10:53 0.81
D1 01/06/2009 10:58 01/06/2009 12:14 1.27
D1 01/06/2009 13:55 01/06/2009 15:52 1.95
D1 01/06/2009 15:59 01/06/2009 16:14 0.24
D1 01/06/2009 16:23 01/06/2009 16:59 0.60
D1 01/06/2009 17:06 01/06/2009 17:11 0.08
D1 01/06/2009 17:22 01/06/2009 18:00 0.63
Table 2
ItemID Start End Weight
D1 01/06/2009 6:00 01/06/2009 18:00 0.6
D1 01/06/2009 6:00 01/06/2009 18:00 2.4
D1 01/06/2009 6:00 01/06/2009 18:00 2.4
D1 01/06/2009 6:00 01/06/2009 18:00 2.4
D1 01/06/2009 6:00 01/06/2009 18:00 1.8
What I am trying to do is select from both tables where the Item ID is the same and the date in table 1 are between those in table 2 and then sum the hours and weight so effectively the output would be
ItemID Start End Weight Hrs
D1 01/06/2009 6:00 01/06/2009 18:00 9.6 7.62
I tried
select t2.itemID, t2.start, t2.end, sum(weight), sum(Hrs)
from table2 t2 inner join table1 t1 on t2.itemID = t1.itemID and t1.start >= t2.start and t1.start < t2.end
group by t2.itemID, t2.start, t2.end
this gave the correct hrs but the weight was more than expected this is because every record in table1 is joined to each record in table 2. i.e. record 1 in table 2 is joined to each record from table 2 and so on.
Is there a simple way without grouping the table individually to sum the rows without this undesired effect.
I have 2 tables
Table 1
ItemID Start End Hours
D1 01/06/2009 6:12 01/06/2009 6:17 0.10
D1 01/06/2009 7:43 01/06/2009 8:39 0.94
D1 01/06/2009 8:49 01/06/2009 9:45 0.93
D1 01/06/2009 9:49 01/06/2009 9:53 0.07
D1 01/06/2009 10:05 01/06/2009 10:53 0.81
D1 01/06/2009 10:58 01/06/2009 12:14 1.27
D1 01/06/2009 13:55 01/06/2009 15:52 1.95
D1 01/06/2009 15:59 01/06/2009 16:14 0.24
D1 01/06/2009 16:23 01/06/2009 16:59 0.60
D1 01/06/2009 17:06 01/06/2009 17:11 0.08
D1 01/06/2009 17:22 01/06/2009 18:00 0.63
Table 2
ItemID Start End Weight
D1 01/06/2009 6:00 01/06/2009 18:00 0.6
D1 01/06/2009 6:00 01/06/2009 18:00 2.4
D1 01/06/2009 6:00 01/06/2009 18:00 2.4
D1 01/06/2009 6:00 01/06/2009 18:00 2.4
D1 01/06/2009 6:00 01/06/2009 18:00 1.8
What I am trying to do is select from both tables where the Item ID is the same and the date in table 1 are between those in table 2 and then sum the hours and weight so effectively the output would be
ItemID Start End Weight Hrs
D1 01/06/2009 6:00 01/06/2009 18:00 9.6 7.62
I tried
select t2.itemID, t2.start, t2.end, sum(weight), sum(Hrs)
from table2 t2 inner join table1 t1 on t2.itemID = t1.itemID and t1.start >= t2.start and t1.start < t2.end
group by t2.itemID, t2.start, t2.end
this gave the correct hrs but the weight was more than expected this is because every record in table1 is joined to each record in table 2. i.e. record 1 in table 2 is joined to each record from table 2 and so on.
Is there a simple way without grouping the table individually to sum the rows without this undesired effect.