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 |
subscriber
Starting Member
8 Posts |
Posted - 2006-01-07 : 11:11:29
|
Records in table are so:customer_id process total1 1 102 1 203 1 251 0 53 0 20 i wantto subtract total values which process column value 1 from which process column value 0 for each customer_id and list by customer_id. And i wantto sum(total) value of this query.how can be this sql query?sorry my bad english:( |
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2006-01-07 : 11:29:54
|
[code]create table #x(customer_id int,process int,total int)insert #xselect 1, 1, 10union all select 2, 1, 20union all select 3, 1, 25union all select 1, 0, 5union all select 3, 0, 20--select * from #xselect case grouping(customer_id) when 1 then 'TOTAL' else ltrim(customer_id) end as customer_id ,sum(case process when 1 then total else 0 end) as process1 ,sum(case process when 0 then total else 0 end) as process0 ,sum(case process when 1 then total else 0 end) -sum(case process when 0 then total else 0 end) as process1_minus_process0from #xgroup by customer_idwith rolluporder by grouping(customer_id) ,customer_iddrop table #xcustomer_id process1 process0 process1_minus_process0 ------------ ----------- ----------- ----------------------- 1 10 5 52 20 0 203 25 20 5TOTAL 55 25 30[/code]rockmoose |
 |
|
subscriber
Starting Member
8 Posts |
Posted - 2006-01-07 : 11:55:10
|
select case grouping(customer_id) when 1 then 'TOTAL' else ltrim(customer_id) end as customer_id ,sum(case process when 1 then total else 0 end) as process1 ,sum(case process when 0 then total else 0 end) as process0 ,sum(case process when 1 then total else 0 end) -sum(case process when 0 then total else 0 end) as process1_minus_process0from #xgroup by customer_idwith rolluporder by grouping(customer_id) ,customer_idwhen i write this sqlquery to access database i get error for "groupping"??? |
 |
|
subscriber
Starting Member
8 Posts |
Posted - 2006-01-07 : 12:00:34
|
and i wantto output so :customer_id process1-process0 1 5 2 20 3 5total 55 |
 |
|
subscriber
Starting Member
8 Posts |
Posted - 2006-01-07 : 12:01:27
|
sorry total=30 |
 |
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2006-01-07 : 12:31:10
|
Oh, this is Access.Well then I don't know.Access does not know the "grouping" keyword, and I'm not sure about the "case" keyword either.Post this question in the Access forum...Said and done:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=59939rockmoose |
 |
|
subscriber
Starting Member
8 Posts |
Posted - 2006-01-09 : 17:57:23
|
i think this is possible with iif function that equavalent of case when but how:( |
 |
|
subscriber
Starting Member
8 Posts |
Posted - 2006-01-11 : 05:53:58
|
yes problem solved by me:)query is:select cust_id ,sum(iif(process=1,[total],0)) as process1 ,sum(iif(process=0,[total],0)) as process0 ,sum(iif(process=1,[total],0)) - sum(iif(process=0,[total],0))as process1_minus_process0from tablenamegroup by cust_idhaving sum(iif(process=1,[total],0)) - sum(iif(process=0,[total],0)) >0order by cust_id |
 |
|
|
|
|
|
|