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 |
|
wisdomt
Starting Member
5 Posts |
Posted - 2009-07-01 : 11:56:19
|
| Hi,I am new to using group by in Select statements.My sql statement is currentlySELECT SiteID, MAX(POValue) AS POValueFROM dbo.ARQM_Finance_ClientPOsWHERE Stage = 'Construction'GROUP BY SiteIDORDER BY SiteIDwhich correctly returns the highest Construction PO Value for each site. What I can't get my head around is now I include the unique ID field (or any other field) associated with the row with the highest PO value for each site.Hope that makes sense, any help would be greatly welcomed.Thanks,Steve |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-07-01 : 12:01:16
|
| [code]SELECT t.*FROM dbo.ARQM_Finance_ClientPOs tINNER JOIN (SELECT SiteID, MAX(POValue) AS POValueFROM dbo.ARQM_Finance_ClientPOsWHERE Stage = 'Construction'GROUP BY SiteID)t1ON t1.SiteID=t.SiteIDAND t1.POValue = t. POValueORDER BY t.SiteID[/code] |
 |
|
|
wisdomt
Starting Member
5 Posts |
Posted - 2009-07-02 : 04:40:38
|
| Thanks Visakhs, that worked spot on. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-07-02 : 04:55:52
|
[code]SELECT *FROM ( SELECT *, ROW_NUMBER() OVER (PARTITION BY SiteID ORDER BY POValue DESC) AS recID FROM dbo.ARQM_Finance_ClientPOs WHERE Stage = 'Construction' ) AS dWHERE recID = 1ORDER BY SiteID[/code] Microsoft SQL Server MVPN 56°04'39.26"E 12°55'05.63" |
 |
|
|
|
|
|