| Author |
Topic |
|
dainova
Starting Member
31 Posts |
Posted - 2012-08-07 : 03:08:08
|
Hi allI'm looking (and learning-)) how to achieve in SQLServer the following task: I need to select max(Amount) for entries grouped by (Term+Param1+Param2) so I expect to get300.1.0.120300.1.1.520300.1.3.620 for this table belowTerm Param1 Param2 Param3 Param4 Amount300 1 0 3 1 120300 1 0 3 1 075300 1 1 4 2 340300 1 1 5 3 520300 1 3 2 4 620 Is there any kind of analytical func in Sqlserver ?TxDai |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2012-08-07 : 03:25:13
|
| This will do...CREATE TABLE TEST(TERM INT, Param1 INT, Param2 INT, Param3 INT, Param4 INT, Amount BIGINT)INSERT INTO TEST SELECT 300,1,0,3,1,120 UNION ALLSELECT 300,1,0,3,1,075 UNION ALLSELECT 300,1,1,4,2,340 UNION ALLSELECT 300,1,1,5,3,520 UNION ALLSELECT 300,1,3,2,4,620SELECT TERM, Param1, Param2, MAX(Amount) AmountFROM TESTGROUP BY TERM, Param1, Param2------------------------------------------------The answer is always no till than you don't ask. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-07 : 09:53:55
|
| [code]SELECT TERM,Param1,Param2,AmountFROM(SELECT ROW_NUMBER() OVER (PARTITION BY TERM,Param1,Param2 ORDER BY Amount DESC) AS Seq,*FROM table)tWHERE Seq=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|