Sunday, February 19, 2012

How to Accomplish This

If I have the following Data in a Table:
TranType Qty
1 10
1 20
2 15
3 7
What is the best way to accomplish this?
Type1 Type2 Type3
30 15 7
Thanks,
KFKeith,
if you've fixed trantype ie limited to 3 then following query will solve the purpose otherwise you
will have to
make use of dynamic crosstab using T-SQL.
--sample table and data.
create table #t(TranType int,Qty int)
insert into #t select 1, 10 union all
select 1 ,20 union all
select 2 ,15 union all
select 3 ,7
--Required query.
select sum(case trantype when 1 then qty else 0 end) type1,
sum(case trantype when 2 then qty else 0 end) type2,
sum(case trantype when 3 then qty else 0 end) type3
from #t
Refer to following url for dynamic crosstab solution.
http://www.sqlteam.com/item.asp?ItemID=2955
--
- Vishal|||"Vishal Parkar" wrote
.
.
> Refer to following url for dynamic crosstab solution.
> http://www.sqlteam.com/item.asp?ItemID=2955
Save yourself a lot of time and aggravation
and check out the RAC utility for all kinds of
crosstabs and pivoting:)
RAC v2.2 and QALite @.
www.rac4sql.net

No comments:

Post a Comment