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 |
BenSwitzer
Yak Posting Veteran
72 Posts |
Posted - 2007-09-27 : 13:46:04
|
Hello I have data that looks like this:Grp Code PG1 185 81 185 91 200 41 200 51 200 6I've been trying to create a query that outputs:Grp Code PG1 185 891 200 456I have not been able to come with anything that creates this output. Any ideas?Thanks |
|
Kristen
Test
22859 Posts |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-27 : 14:10:48
|
[code]-- Prepare sample dataDECLARE @Sample TABLE (Grp INT, Code INT, PG INT)INSERT @SampleSELECT 1, 185, 8 UNION ALLSELECT 1, 185, 9 UNION ALLSELECT 1, 200, 4 UNION ALLSELECT 1, 200, 5 UNION ALLSELECT 1, 200, 6-- Show the expected outputSELECT Grp, Code, SUM(xPG) AS PGFROM ( SELECT s1.Grp, s1.Code, s1.PG * POWER(10, (SELECT COUNT(*) FROM @Sample AS s2 WHERE s2.Grp = s1.Grp AND s2.Code = s1.Code AND s2.PG > s1.PG)) AS xPG FROM @Sample AS s1 ) AS dGROUP BY Grp, CodeORDER BY Grp, Code[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
BenSwitzer
Yak Posting Veteran
72 Posts |
Posted - 2007-09-27 : 15:10:07
|
Kristen, thanks for the reference. I will look at the solutions. Paco, I get an arithmetic error when trying to use this for my table.(12 row(s) affected)Server: Msg 232, Level 16, State 3, Line 1Arithmetic overflow error for type int, value = 10000000000.000000.ThanksI expect that I somehow forgot about cursor :+) |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-28 : 03:10:54
|
If you have posted better sample data, you would not have gotten this error.The works for the sample data posted above.Also you don't provide information about there can be two or more PG for any combination of Grp and Code. E 12°55'05.25"N 56°04'39.16" |
 |
|
BenSwitzer
Yak Posting Veteran
72 Posts |
Posted - 2007-09-28 : 08:01:54
|
THanks Madhivanan That did the trick. Sorry Peso for the lack of information. Thanks for your help :+) |
 |
|
|
|
|
|
|