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 |
crackerbox
Starting Member
21 Posts |
Posted - 2007-02-21 : 15:51:10
|
I have two tables that have dates in both tables. I need to add up all the data in table 1 that has a date later than or equal to the date in table 2. The tables are joined with the Aircraft no. Here's some sample data. declare @t1 table (Aircraft int, dt datetime, hours int)insert @t1select 38, '4/19/06', .5 union allSelect 38, '4/21/06', 1 union allselect 38, '5/01/06', 2 union allselect 38, '6/19/06', 4 union allselect 40, '1/20/06', 15 union allselect 40, '8/1/06', 35declare @t2 table (aircraft int, dt datetime)insert @t2select 38, '4/20/06' union allselect 40, '1/20/06'This is what I'm looking for (I also need to include the date from table 2 and the latest date from table 1:Aircraft dt dt2 Hours38 6/19/06 4/20/06 740 8/1/06 1/20/06 45 Any help would be appreciated.Thanks |
|
PurpleSun
Yak Posting Veteran
50 Posts |
Posted - 2007-02-21 : 16:58:05
|
Hope this will helpselect t1.aircraft, max(t1.dt), sum(t1.hours) as hours, t2.dtfrom t1 inner join t2 on t1.aircraft=t2.aircraft group by t1.aircraft, t2.dthaving max(t1.dt) >= t2.dt |
 |
|
crackerbox
Starting Member
21 Posts |
Posted - 2007-02-21 : 17:16:28
|
That worked great! You're my hero.Thanks a bunch. |
 |
|
|
|
|
|
|