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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 non subquery .....

Author  Topic 

mangyun
Starting Member

8 Posts

Posted - 2007-12-14 : 03:00:57
--i have this table
declare @a table(
a1 varchar(3),a2 varchar(3),a3 int
)
insert into @a
select 'aa','11',1 union all
select 'aa','11',1 union all
select 'aa','12',1 union all
select 'aa','12',1 union all
select 'aa','12',1 union all
select 'aa','12',1 union all
select 'bb','11',1 union all
select 'bb','12',1 union all
select 'bb','12',1

--known solution
select c1.a1,c1.v11,c2.v12 from
(select a1,sum(a3) v11 from @a where a2='11' group by a1) as c1 left join
(select a1,sum(a3) v12 from @a where a2='12' group by a1) as c2 on c1.a1=c2.a1

is there any other way to acomplish the same result(as the above query) without using subquery ?

regards

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-12-14 : 03:15:27
[code]
select
a1,
sum(case when a2='11' then a3 else 0 end) as v11,
sum(case when a2='12' then a3 else 0 end) as v12
from @a
group by a1
[/code]

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

mangyun
Starting Member

8 Posts

Posted - 2007-12-14 : 03:40:42
:) thanks Madhivanan.
actually i have used case when.... a few times before ....
just got confused :( and unclear mind :)
very much apreciated

mangyun
Go to Top of Page
   

- Advertisement -