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 |
|
shagil.a.gopinath
Starting Member
14 Posts |
Posted - 2010-04-25 : 07:33:45
|
| Hi,This is my requirement , from below table i need to get the sum of the value in a single line and the condition is data value is greater than 20(Sum2) and data value is lesser than 20(Sum1).pk value data categorya 10 15 AAb 20 19 AAc 15 21 CCd 10 22 CCoutput must be like thissum1 sum2 30 25 Can i get this output using select query with out using store procedure or cursorPlease help me Thanks & Regardsshagil |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-04-25 : 07:36:28
|
select sum(case when category = 'aa' then value else 0 end) as sum1,sum(case when category = 'cc' then value else 0 end) as sum2from yourtablenamehere N 56°04'39.26"E 12°55'05.63" |
 |
|
|
shagil.a.gopinath
Starting Member
14 Posts |
Posted - 2010-04-25 : 07:43:34
|
| it works fine, thanks for your help PesoThanks & Regardsshagil |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-04-25 : 07:55:48
|
| try this alsocreate nonclustered index ix_0 on table_name (category) include (value);with c1 as (select sum(value) as sum1, k=1from table_namewhere category = 'aa'),c2 as(select sum(value) as sum2, k=1from table_namewhere category = 'cc')select sum1, sum2from c1 inner join c2on c1.k = c2.k; |
 |
|
|
shagil.a.gopinath
Starting Member
14 Posts |
Posted - 2010-04-25 : 08:24:12
|
| Hi,I have tried , its also working fine. anyway i could learn two method for the same requirement. Thanks a lot |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-04-25 : 08:38:47
|
| Alsocreate function dbo.CrossTab (@category char(2)) returns table asreturn(select sum(value) as sumfrom table_namewhere category = @category);GOselect c1.sum, c2.sumfrom dbo.CrossTab('aa') as c1cross join dbo.CrossTab('cc') as c2EDIT: croos -> cross |
 |
|
|
|
|
|