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 |
|
RaxSuur
Starting Member
7 Posts |
Posted - 2005-08-23 : 07:52:56
|
| HalloI'm wondering, is it posiible to make SQL query, that generates following. Source table:Step_nr--ColA--ColB1--------req1--121--------req1--141--------req2--111--------req2--131--------req2--102--------req1--172--------req1--192--------req1--152--------req2--162--------req2--9Generated table should be like:Step_nr--Sum(req1)--Sum(req2)1--------26---------342--------51---------25Can enyone help me please? |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-08-23 : 08:12:28
|
yes:Select Step_Nr, Req1 = sum(case when colA='req1' then ColB else 0 end), Req1 = sum(case when colA='req1' then ColB else 0 end)From <yourTable>Group By Step_Nr Corey Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now." |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-23 : 08:33:31
|
| and use req2 as column name for the second calculationSelect Step_Nr, Req1 = sum(case when colA='req1' then ColB else 0 end), Req2 = sum(case when colA='req2' then ColB else 0 end)From <yourTable>Group By Step_NrMadhivananFailing to plan is Planning to fail |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-08-23 : 08:39:10
|
hehehe ooops Thanks for error checking me Corey Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now." |
 |
|
|
RaxSuur
Starting Member
7 Posts |
Posted - 2005-08-23 : 12:45:24
|
| Thanks, but I forgot to say that I work in Access. I can't get work this code in Access |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-08-23 : 12:50:35
|
try:Select Step_Nr, Req1 = iif(colA='req1',ColB,0), Req2 = iif(colA='req2',ColB,0)From <yourTable>Group By Step_Nr Corey Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now." |
 |
|
|
RaxSuur
Starting Member
7 Posts |
Posted - 2005-08-23 : 13:09:37
|
| Thank you both very much. It is working fine. Finally I made this way:Select Step_Nr, SUM(iif(colA='req1',ColB,0)) AS Req1, SUM(iif(colA='req2',ColB,0)) AS Req2From <yourTable>Group By Step_Nr |
 |
|
|
|
|
|
|
|