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 |
|
david_reinjal
Starting Member
36 Posts |
Posted - 2007-07-16 : 07:19:08
|
| hi guys,I have a table which has 4 columns A,B,C,D. it is somewhat like this:A B CS U 8S U 10S U 3s V 14S V 36T U 25T U 34T U 9T V 5T V 1This is the way i have in my database. I need to check which is the max in column C for values in Column A and Column B. First i need to start from column A. If it is S, then i need to see column B, if it is U then max[Column C] for only U & S. then when it becomes V, I need to get max [Column C] for only V & S. So on and so forth. So finally my output should be:S U 10S V 36T U 34T V 5How can i do this?regards,David |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-07-16 : 07:41:45
|
| Select columns from table T where colc=(select max(colc) as colc from table where cola=T.cola and colb=T.colb)MadhivananFailing to plan is Planning to fail |
 |
|
|
david_reinjal
Starting Member
36 Posts |
Posted - 2007-07-16 : 08:03:04
|
| thanks dude..i will just try it. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-16 : 11:31:58
|
Based on your original sample data, this will doSELECT A, B, MAX(C)FROM Table1GROUP BY A, BORDER BY A, B Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-16 : 11:48:47
|
| [code]-- Create sample dataDECLARE @Sample TABLE (A CHAR(1), B CHAR(1), C TINYINT)INSERT @Sample (A, B, C)SELECT 'S', 'U', 8 UNION ALLSELECT 'S', 'U', 10 UNION ALLSELECT 'S', 'U', 3 UNION ALLSELECT 's', 'V', 14 UNION ALLSELECT 'S', 'V', 36 UNION ALLSELECT 'T', 'U', 25 UNION ALLSELECT 'T', 'U', 34 UNION ALLSELECT 'T', 'U', 9 UNION ALLSELECT 'T', 'V', 5 UNION ALLSELECT 'T', 'V', 1-- MadhivananSELECT t.A, t.B, t.CFROM @Sample AS tWHERE t.c = (SELECT MAX(x.C) AS c FROM @Sample AS x WHERE x.A = t.A AND x.B = t.B)ORDER BY t.A, t.B-- Peso 1SELECT A, B, MAX(C) AS cFROM @SampleGROUP BY A, BORDER BY A, B-- Peso 2SELECT d.A, d.B, d.CFROM ( SELECT A, B, C, ROW_NUMBER() OVER (PARTITION BY A, B ORDER BY C DESC) AS RecID FROM @Sample ) AS dWHERE d.RecID = 1ORDER BY d.A, d.B[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-16 : 11:51:29
|
It is an attempt to imitate SQL Server 2005 ROW_NUMBER() functionality in SQL Server 2000 Peter LarssonHelsingborg, Sweden |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-07-17 : 00:10:28
|
quote: Originally posted by Peso It is an attempt to imitate SQL Server 2005 ROW_NUMBER() functionality in SQL Server 2000 Peter LarssonHelsingborg, Sweden
Yes it is MadhivananFailing to plan is Planning to fail |
 |
|
|
david_reinjal
Starting Member
36 Posts |
Posted - 2007-07-17 : 00:18:58
|
| thanks peso. it worked. |
 |
|
|
|
|
|
|
|