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 |
|
MIB426
Starting Member
3 Posts |
Posted - 2006-05-19 : 11:12:12
|
| TableAID Status Number3 A 11 A 12 B 23 B 12 C 11 C 31 A 12 C 23 B 32 C 11 C 31 A 12 C 23 B 3TableBID Name1 Dog2 Cat3 PigOutput QueryID Name Status=A Status=B Status=C1 Dog 3 2 62 Cat 0 2 43 Pig 1 7 0I want to have query which can give me result as Output QueryConditionStatus= A „³ Sum(Number) when Status = AStatus= B „³ Sum(Number) when Status = BStatus= C „³ Sum(Number) when Status = CPlease let me know in both Query and SP.thanks |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-05-19 : 11:19:56
|
| select t1.ID, t2.name, sum(case when t1.status = 'A' then 1 else 0 end, sum(.....from table1 t1join table2 t2on t1.ID = t2.IDgroup by t1.id, t2.namewill be the same statement in a query or sp.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-05-19 : 11:22:50
|
http://weblogs.sqlteam.com/jeffs/archive/2005/05/02/4842.aspxGo with the flow & have fun! Else fight the flow Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"] |
 |
|
|
MIB426
Starting Member
3 Posts |
Posted - 2006-05-19 : 11:32:24
|
| thank you allProblem is solved. again, thank you very muchMIB426 |
 |
|
|
MIB426
Starting Member
3 Posts |
Posted - 2006-05-19 : 13:35:07
|
| Now i am getting another issue. as resultID Name Status=A Status=B Status=C1 Dog 3 2 62 Cat 0 2 43 Pig 1 7 0now i need extra field which is Status=B minus Status=CID Name Status=A Status=B Status=C Difference(B-C)1 Dog 3 2 6 -42 Cat 0 2 4 -23 Pig 1 7 0 7conditionDifference(B-C) --> Status=B - Status=Cplease let me knowthanks |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-05-19 : 13:48:33
|
Something like this?  --datadeclare @TableA table (ID int, Status char(1), Number int)insert @TableA select 3, 'A', 1union all select 1, 'A', 1union all select 2, 'B', 2union all select 3, 'B', 1union all select 2, 'C', 1union all select 1, 'C', 3union all select 1, 'A', 1union all select 2, 'C', 2union all select 3, 'B', 3union all select 2, 'C', 1union all select 1, 'C', 3union all select 1, 'A', 1union all select 2, 'C', 2union all select 3, 'B', 3declare @TableB table (ID int, Name varchar(10))insert @TableB select 1, 'Dog'union all select 2, 'Cat'union all select 3, 'Pig'--calculationselect a.ID, b.name, sum(case a.status when 'A' then 1 else 0 end) as 'Status=A', sum(case a.status when 'B' then 1 else 0 end) as 'Status=B', sum(case a.status when 'C' then 1 else 0 end) as 'Status=C', sum(case a.status when 'B' then 1 when 'C' then -1 else 0 end) as 'Difference(B-C)'from @tableA a inner join @tableB b on a.ID = b.IDgroup by a.id, b.nameorder by a.id Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-05-19 : 15:18:15
|
| or if you don't want to repeat the codeselect ID, name, [Status=A],[Status=B],[Status=C],[Difference(B-C)] = [Status=B]-[Status=C]from(select a.ID, b.name, sum(case a.status when 'A' then 1 else 0 end) as [Status=A], sum(case a.status when 'B' then 1 else 0 end) as [Status=B], sum(case a.status when 'C' then 1 else 0 end) as [Status=C]from @tableA a inner join @tableB b on a.ID = b.IDgroup by a.id, b.name) aorder by id==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|
|
|