Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Table Joins 1

Status
Not open for further replies.

Leighton21

Technical User
Dec 17, 2001
83
AU
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.




 
What date in Table1?
You have two of them :)
What if you have such record in table1:
[tt]
D1 01/06/2009 17:35 01/06/2009 18:22 0.63
[/tt]
?

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Thanks for the prompt response Borislav

Basically if the start date from table 1 is within the start and end date of table 2 then it is assumed that the sum will be for that period. The way the driving ssyetm works is that time will always be cut off at 18:00 and 06:00.
 
Code:
[COLOR=green]--- Preparing Terst data, you don't need this
[/color]
[COLOR=blue]SET[/color] [COLOR=#FF00FF]DATEFORMAT[/color] MDY

[COLOR=blue]DECLARE[/color] @Table1 [COLOR=blue]AS[/color] [COLOR=blue]Table[/color] (ItemID [COLOR=blue]char[/color](2), [COLOR=#FF00FF]Start[/color] [COLOR=#FF00FF]datetime[/color], EndTime [COLOR=#FF00FF]datetime[/color], Hours [COLOR=blue]numeric[/color](10,2))
[COLOR=blue]INSERT[/color] [COLOR=blue]INTO[/color] @Table1 [COLOR=blue]VALUES[/color]([COLOR=red]'D1'[/color],[COLOR=red]'01/06/2009 6:12'[/color],[COLOR=red]'01/06/2009 6:17'[/color],0.10)
[COLOR=blue]INSERT[/color] [COLOR=blue]INTO[/color] @Table1 [COLOR=blue]VALUES[/color]([COLOR=red]'D1'[/color],[COLOR=red]'01/06/2009 7:43'[/color],[COLOR=red]'01/06/2009 8:39'[/color],0.94)
[COLOR=blue]INSERT[/color] [COLOR=blue]INTO[/color] @Table1 [COLOR=blue]VALUES[/color]([COLOR=red]'D1'[/color],[COLOR=red]'01/06/2009 8:49'[/color],[COLOR=red]'01/06/2009 9:45'[/color],0.93)
[COLOR=blue]INSERT[/color] [COLOR=blue]INTO[/color] @Table1 [COLOR=blue]VALUES[/color]([COLOR=red]'D1'[/color],[COLOR=red]'01/06/2009 9:49'[/color],[COLOR=red]'01/06/2009 9:53'[/color],0.07)
[COLOR=blue]INSERT[/color] [COLOR=blue]INTO[/color] @Table1 [COLOR=blue]VALUES[/color]([COLOR=red]'D1'[/color],[COLOR=red]'01/06/2009 10:05'[/color],[COLOR=red]'01/06/2009 10:53'[/color],0.81)
[COLOR=blue]INSERT[/color] [COLOR=blue]INTO[/color] @Table1 [COLOR=blue]VALUES[/color]([COLOR=red]'D1'[/color],[COLOR=red]'01/06/2009 10:58'[/color],[COLOR=red]'01/06/2009 12:14'[/color],1.27)
[COLOR=blue]INSERT[/color] [COLOR=blue]INTO[/color] @Table1 [COLOR=blue]VALUES[/color]([COLOR=red]'D1'[/color],[COLOR=red]'01/06/2009 13:55'[/color],[COLOR=red]'01/06/2009 15:52'[/color],1.95)
[COLOR=blue]INSERT[/color] [COLOR=blue]INTO[/color] @Table1 [COLOR=blue]VALUES[/color]([COLOR=red]'D1'[/color],[COLOR=red]'01/06/2009 15:59'[/color],[COLOR=red]'01/06/2009 16:14'[/color],0.24)
[COLOR=blue]INSERT[/color] [COLOR=blue]INTO[/color] @Table1 [COLOR=blue]VALUES[/color]([COLOR=red]'D1'[/color],[COLOR=red]'01/06/2009 16:23'[/color],[COLOR=red]'01/06/2009 16:59'[/color],0.60)
[COLOR=blue]INSERT[/color] [COLOR=blue]INTO[/color] @Table1 [COLOR=blue]VALUES[/color]([COLOR=red]'D1'[/color],[COLOR=red]'01/06/2009 17:06'[/color],[COLOR=red]'01/06/2009 17:11'[/color],0.08)
[COLOR=blue]INSERT[/color] [COLOR=blue]INTO[/color] @Table1 [COLOR=blue]VALUES[/color]([COLOR=red]'D1'[/color],[COLOR=red]'01/06/2009 17:22'[/color],[COLOR=red]'01/06/2009 18:00'[/color],0.63)

[COLOR=blue]DECLARE[/color] @Table2 [COLOR=blue]AS[/color] [COLOR=blue]Table[/color] (ItemID [COLOR=blue]char[/color](2), [COLOR=#FF00FF]Start[/color] [COLOR=#FF00FF]datetime[/color], EndTime [COLOR=#FF00FF]datetime[/color], Weight [COLOR=blue]numeric[/color](10,2))
[COLOR=blue]INSERT[/color] [COLOR=blue]INTO[/color] @Table2 [COLOR=blue]VALUES[/color]([COLOR=red]'D1'[/color],[COLOR=red]'01/06/2009 6:00'[/color],[COLOR=red]'01/06/2009 18:00'[/color],0.6)
[COLOR=blue]INSERT[/color] [COLOR=blue]INTO[/color] @Table2 [COLOR=blue]VALUES[/color]([COLOR=red]'D1'[/color],[COLOR=red]'01/06/2009 6:00'[/color],[COLOR=red]'01/06/2009 18:00'[/color],2.4)
[COLOR=blue]INSERT[/color] [COLOR=blue]INTO[/color] @Table2 [COLOR=blue]VALUES[/color]([COLOR=red]'D1'[/color],[COLOR=red]'01/06/2009 6:00'[/color],[COLOR=red]'01/06/2009 18:00'[/color],2.4)
[COLOR=blue]INSERT[/color] [COLOR=blue]INTO[/color] @Table2 [COLOR=blue]VALUES[/color]([COLOR=red]'D1'[/color],[COLOR=red]'01/06/2009 6:00'[/color],[COLOR=red]'01/06/2009 18:00'[/color],2.4)
[COLOR=blue]INSERT[/color] [COLOR=blue]INTO[/color] @Table2 [COLOR=blue]VALUES[/color]([COLOR=red]'D1'[/color],[COLOR=red]'01/06/2009 6:00'[/color],[COLOR=red]'01/06/2009 18:00'[/color],1.8)
[COLOR=green]--- End Preparing Terst data
[/color]
[COLOR=green]--- Just change @Table1 and @Table2 with you actual table names
[/color][COLOR=blue]select[/color] t2.itemID,
       t2.start,
       t2.endTime, 
       t2.weight,
       sum(t1.Hours) [COLOR=blue]AS[/color] Hrs
[COLOR=blue]from[/color] ([COLOR=blue]SELECT[/color] itemID,
             [COLOR=#FF00FF]start[/color],
             endTime, 
             SUM(weight) [COLOR=blue]AS[/color] weight
       [COLOR=blue]FROM[/color] @Table2
       [COLOR=blue]GROUP[/color] [COLOR=blue]BY[/color] itemID,
                [COLOR=#FF00FF]start[/color],
                endTime) t2
[COLOR=blue]inner[/color] [COLOR=blue]join[/color] @table1 t1
           [COLOR=blue]on[/color] t2.itemID = t1.itemID and
              t1.start >= t2.start and t1.start < t2.endTime
[COLOR=blue]group[/color] [COLOR=blue]by[/color] t2.itemID,
         t2.start,
         t2.endTime, 
         t2.weight


NOT TESTED Properly!!!

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Boris you beat me to it.

"NOTHING is more important in a database than integrity." ESquared
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top