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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Max and grouping

Author  Topic 

duhaas
Constraint Violating Yak Guru

310 Posts

Posted - 2008-08-06 : 21:35:39
Not sure why after using the MAX function I still get two results back from the following:

    SELECT          STR(bd.ID_BB_COMPANY) AS ID_BB_COMPANY,
max(bd.CUR_MKT_CAP) AS CUR_MKT_CAP,
max(CONVERT(bigint,bp.TOTAL_VOTING_SHARES_VALUE)) AS TOTAL_VOTING_SHARES_VALUE,
bd.CNTRY_OF_DOMICILE AS CNTRY_OF_DOMICILE
FROM dbo.BloombergEquityPricingData bd
LEFT JOIN BloombergEquityData bp
ON bd.id_cusip = bp.id_cusip
WHERE bd.EXCH_CODE = 'US' and bd.ID_BB_COMPANY = '100177'
--AND bd.ID_BB_PRIM_SECURITY_FLAG = 'Y'
--AND bp.ID_BB_PRIM_SECURITY_FLAG = 'Y'
--AND bd.CUR_MKT_CAP IS NOT NULL
group by bd.ID_BB_COMPANY,bd.CUR_MKT_CAP,bd.CNTRY_OF_DOMICILE,bp.TOTAL_VOTING_SHARES_VALUE


here are the results
100177 0 0 US
100177 43917.32 627659264 US

Why dont I just get the bottom row back when I am specifying MAX?

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-08-06 : 23:03:14
Don't put the columns you are aggregating in the GROUP BY clause

Be One with the Optimizer
TG
Go to Top of Page

duhaas
Constraint Violating Yak Guru

310 Posts

Posted - 2008-08-07 : 11:59:29
Thanks as always, that worked.
Go to Top of Page
   

- Advertisement -