| Author |
Topic |
|
abacusdotcom
Posting Yak Master
133 Posts |
Posted - 2009-05-03 : 21:17:40
|
Guys,How there guys..I got this code by Pesoquote: -- prepare test datadeclare @test table (AircraftNo tinyint, Date datetime, Flight_Hrs smallmoney)insert @testselect 38, '3/1/06', 1 union allselect 100, '8/19/03', 4 union allselect 38, '7/10/06', 2 union allselect 100, '10/04/04', 3 union allselect 38, '5/10/06', 1.5 union allselect 100, '9/17/06', 1.5-- do the workselect t.AircraftNo, t.Date, t.Flight_Hrs, (select sum(x.Flight_Hrs) from @test x where x.AircraftNo = t.AircraftNo and x.date <= t.date) TotalFrom @test torder by t.AircraftNo, t.Date
This gives running total.But I want the running differences.. does that made the delta?or can I get the delta of this table.. Thanks guysI sign for fame not for shame but all the same, I sign my name. |
|
|
aprichard
Yak Posting Veteran
62 Posts |
Posted - 2009-05-04 : 05:06:23
|
| Try this u may get ideadeclare @test table (AircraftNo tinyint, Date datetime, Flight_Hrs smallmoney)insert @testselect 38, '3/1/06', 1 union allselect 100, '8/19/03', 4 union allselect 38, '7/10/06', 2 union allselect 100, '10/04/04', 3 union allselect 38, '5/10/06', 1.5 union allselect 100, '9/17/06', 1.5;WITH Fligt_CTE (AircraftNo,Date,Flight_Hrs, Pre_Flight_Hrs)AS(select t.AircraftNo,t.Date,t.Flight_Hrs,--(select sum(x.Flight_Hrs) from @test x where x.AircraftNo = t.AircraftNo and x.date <= t.date) Total,(select max(x.Flight_Hrs) from @test x where x.AircraftNo = t.AircraftNo and x.date < t.date) PreFlightHrFrom @test t)SELECT AircraftNo, Date,Flight_Hrs,Pre_Flight_Hrs,(select sum( Flight_Hrs- ISNULL(x.Pre_Flight_Hrs,0)) from Fligt_CTE x where x.AircraftNo = t.AircraftNo and x.date <= t.date and Pre_Flight_Hrs is not null ) PreFlightHrFROM Fligt_CTE TORDER BY AircraftNo,Date |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-04 : 06:41:06
|
[code]-- prepare test datadeclare @test table (AircraftNo tinyint, [date]datetime, Flight_Hrs smallmoney)insert @testselect 38, '3/1/06', 1 union allselect 100, '8/19/03', 4 union allselect 38, '7/10/06', 2 union allselect 100, '10/04/04', 3 union allselect 38, '5/10/06', 1.5 union allselect 100, '9/17/06', 1.5-- Query;with data (AircraftNo, [date], Flight_Hrs, row_no)as( select AircraftNo, [date], Flight_Hrs, row_no = row_number() over (partition by AircraftNo order by [date]) from @test)select d1.*, delta = d1.Flight_Hrs - d2.Flight_Hrsfrom data d1 left join data d2 on d1.AircraftNo = d2.AircraftNo and d1.row_no = d2.row_no + 1/*AircraftNo date Flight_Hrs row_no delta ---------- ---------- ------------ -------------------- ------------ 38 2006-03-01 1.0000 1 NULL38 2006-05-10 1.5000 2 .500038 2006-07-10 2.0000 3 .5000100 2003-08-19 4.0000 1 NULL100 2004-10-04 3.0000 2 -1.0000100 2006-09-17 1.5000 3 -1.5000(6 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
abacusdotcom
Posting Yak Master
133 Posts |
Posted - 2009-05-04 : 11:04:11
|
| I bow!!!!!!Thanks peoples..Works like charm!!!I sign for fame not for shame but all the same, I sign my name. |
 |
|
|
|
|
|