| Author |
Topic |
|
AAAV
Posting Yak Master
152 Posts |
Posted - 2010-03-15 : 14:31:32
|
| Select A.[state],A.[vc_trimid] ,A.[interval], c.RunningFROM (select sum(Sum_of_total_cost) as Running from [Maintenance_work].[dbo].[temp1] B where B.state=A.state and B.vc_trimid=A.vc_trimid and B.interval<=A.interval) Cinner join [Maintenance_work].[dbo].[temp1] A on A.State=C.StateI have state, vc_trimid,interval and sum_of_total in a table.i need state,vc_trimid,interval,sum_total,running total of sum_of_total for that state and vc_trimid with the increment on intervalSample dataAlabama 33950 15000 163.8Alabama 33950 60000 281.734Alabama 33950 105000 141.816Alabama 33950 107500 157.248I want the 5th col 163.8,163.8+281.734,163.8+281.734+141.816 etcHow to correct the query for this? |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-03-15 : 14:36:01
|
If using SQL 2005SelectA.[state],A.[vc_trimid] ,A.[interval], c.Running,SUM(C.Running)over(partition by A.state)as 5th columnFROM (select sum(Sum_of_total_cost) as Running from[Maintenance_work].[dbo].[temp1] B where B.state=A.state and B.vc_trimid=A.vc_trimid and B.interval<=A.interval) Cinner join [Maintenance_work].[dbo].[temp1] A on A.State=C.Stategroup by A.[state],A.[vc_trimid] ,A.[interval], c.Running, PBUH |
 |
|
|
AAAV
Posting Yak Master
152 Posts |
Posted - 2010-03-15 : 14:54:22
|
| SelectA.[state],A.[vc_trimid] ,A.[interval], c.Running,SUM(C.Running)over(partition by A.state) FROM (select sum(Sum_of_total_cost) as Running from[Maintenance_work].[dbo].[temp1] B where B.state=A.state and B.vc_trimid=A.vc_trimid and B.interval<=A.interval) Cinner join [Maintenance_work].[dbo].[temp1] A on A.State=C.StateMsg 4104, Level 16, State 1, Line 1The multi-part identifier "A.state" could not be bound.Msg 4104, Level 16, State 1, Line 1The multi-part identifier "A.vc_trimid" could not be bound.Msg 4104, Level 16, State 1, Line 1The multi-part identifier "A.interval" could not be bound.Msg 207, Level 16, State 1, Line 7Invalid column name 'State'.I dont know why i get this. these columns are there |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-03-15 : 15:02:44
|
Oh sorryselect *,SUM(Running)over(partition by state)as 5th column ( Select A.[state],A.[vc_trimid] , A.[interval], c.Running, FROM ( select sum(Sum_of_total_cost) as Running from [Maintenance_work].[dbo].[temp1] B where B.state=A.state and B.vc_trimid=A.vc_trimid and B.interval<=A.interval) C inner join [Maintenance_work].[dbo].[temp1] A on A.State=C.State)from tgroup by [state],[vc_trimid] ,[interval], Running PBUH |
 |
|
|
AAAV
Posting Yak Master
152 Posts |
Posted - 2010-03-15 : 16:05:26
|
| Select A.[state],A.[vc_trimid] , A.[interval], c.Running FROM [Maintenance_work].[dbo].[temp1] A inner join ( select sum(Sum_of_total_cost) as Running from [Maintenance_work].[dbo].[temp1] B where B.state=A.state and B.vc_trimid=A.vc_trimid and B.interval<=A.interval) C on A.State=C.State I get error ... tried running only this... this too says a.state etc not found (just swapped the join tables to check if the order makes it valid). got it working some other way... will be good if this one works for me |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-16 : 00:47:37
|
| [code]SelectA.[state],A.[vc_trimid] ,A.[interval], c.RunningFROM [Maintenance_work].[dbo].[temp1] A cross apply (select sum(Sum_of_total_cost) as Running from [Maintenance_work].[dbo].[temp1] B where B.state=A.state and B.vc_trimid=A.vc_trimid and B.interval<=A.interval) C[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jet1337
Starting Member
11 Posts |
Posted - 2010-03-16 : 03:03:34
|
| thanks for sharing, working for meASPnix.com |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
AAAV
Posting Yak Master
152 Posts |
Posted - 2010-03-16 : 12:19:42
|
| thanks it worked. Where can i read a good article about the cross apply?My next step is i want to convert the rows with same state vc trim id into one row...ie state, vc_trimid, the interval number as col name and running total as value in the rows. so Alabama 3000 10000 250Alabama 3000 15000 300Alabama 3000 20000 400will becomeAlabama 3000 250 300 400 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
|