| Author |
Topic |
|
IainMaclean
Starting Member
9 Posts |
Posted - 2009-08-18 : 02:06:18
|
| Hope you can help me. It has to work on SQL2000 for the next couple of months at least.This querySelect Sum(remn_am)as Total_Remuneration, Jurisdiction, orgn_id as OrganisationFrom dbo.iamestab EstablishmentsLeft Outer join dbo.Address AddressesOn establishments.residential_address_id = Addresses.adr_idLeft Outer Join dbo.tblQRegion PostCodeRegionsOn ltrim(rtrim(addresses.post_cd)) = ltrim(rtrim(PostCodeRegions.post_code))where orgn_id in (6818982,4934392,2694595,12672918)Group By Orgn_id, JurisdictionOrder By Orgn_idGives me the following resultsTotal_Remuneration, Jurisdiction, Organisation9265700.00 1_MLBN 26945952025949.00 2_KNX 26945951694823.00 3_GLNG 26945951075561.00 4_BGO 26945952090586.00 5_SPN 26945951125198.00 6_ALBY 269459588497712.00 1_MLBN 49343924151006.00 2_KNX 49343925816064.00 3_GLNG 4934392674358.00 4_BGO 4934392278132.00 5_SPN 4934392193039.00 6_ALBY 4934392169007.00 7_MLDA 4934392552855319.00 1_MLBN 6818982131882373.00 2_KNX 681898299082310.00 3_GLNG 681898251079257.00 4_BGO 681898214736525.00 5_SPN 681898223351836.00 6_ALBY 68189829182427.00 7_MLDA 68189825544298.00 1_MLBN 1267291871069211.00 2_KNX 1267291857078878.00 3_GLNG 1267291818182754.00 4_BGO 126729189244206.00 5_SPN 1267291811160545.00 6_ALBY 12672918113170405.00 7_MLDA 12672918What i really want to be able to do is select only the Jurisdiction which has the highest Total_Remuneration. Eliminating duplicated organisations. i want to add to the query so i only retrieve these results.9265700.00 1_MLBN 269459588497712.00 1_MLBN 4934392552855319.00 1_MLBN 6818982113170405.00 7_MLDA 12672918I suspect i need to do a subquery of some kind. Any advice would be appreciated. |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-08-18 : 02:20:52
|
| SELECT MAX(Total_Remuneration) AS 'Total_Remuneration', OrganisationFROM TABLE GROUP BY Organisation |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-08-18 : 02:22:10
|
| Select Sum(remn_am)as Total_Remuneration, Jurisdiction, orgn_id as Organisationinto #tempFrom dbo.iamestab EstablishmentsLeft Outer join dbo.Address AddressesOn establishments.residential_address_id = Addresses.adr_idLeft Outer Join dbo.tblQRegion PostCodeRegionsOn ltrim(rtrim(addresses.post_cd)) = ltrim(rtrim(PostCodeRegions.post_code))where orgn_id in (6818982,4934392,2694595,12672918)Group By Orgn_id, JurisdictionOrder By Orgn_idselect t.Total_Remuneration,t.Jurisdiction,t.Organisationfrom #temp tinner join (select max(Total_Remuneration) as Total_Remuneration ,Organisation from #temp) s on s.Total_Remuneration = t.Total_Remuneration and s.organisation = t.organisation |
 |
|
|
IainMaclean
Starting Member
9 Posts |
Posted - 2009-08-18 : 02:51:14
|
| I get this errorWarning: Null value is eliminated by an aggregate or other SET operation.(27 row(s) affected)Msg 8118, Level 16, State 1, Line 12Column '#temp.Organisation' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause. |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-08-18 : 03:07:20
|
| select t.Total_Remuneration,t.Jurisdiction,t.Organisationfrom #temp tinner join (select max(Total_Remuneration) as Total_Remuneration ,Organisation from #temp group by orgamisation) s on s.Total_Remuneration = t.Total_Remuneration and s.organisation = t.organisation |
 |
|
|
IainMaclean
Starting Member
9 Posts |
Posted - 2009-08-18 : 03:24:28
|
| Thanks bklr. works. Is there however a way i can do it purely in a query without creating a table? |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-08-18 : 04:24:00
|
| Welcome,use the query instead of the table as subquery |
 |
|
|
IainMaclean
Starting Member
9 Posts |
Posted - 2009-08-18 : 05:08:19
|
| sorry for being a noob but how does one do that? |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-08-18 : 05:20:51
|
| [code]select t.Total_Remuneration,t.Jurisdiction,t.Organisationfrom (Select Sum(remn_am)as Total_Remuneration, Jurisdiction, orgn_id as OrganisationFrom dbo.iamestab EstablishmentsLeft Outer join dbo.Address Addresses On establishments.residential_address_id = Addresses.adr_idLeft Outer Join dbo.tblQRegion PostCodeRegions On ltrim(rtrim(addresses.post_cd)) = ltrim(rtrim(PostCodeRegions.post_code))where orgn_id in (6818982,4934392,2694595,12672918) Group By Orgn_id, Jurisdiction) tinner join (select max(Total_Remuneration) as Total_Remuneration ,Organisation from (Select Sum(remn_am)as Total_Remuneration, Jurisdiction, orgn_id as OrganisationFrom dbo.iamestab EstablishmentsLeft Outer join dbo.Address Addresses On establishments.residential_address_id = Addresses.adr_idLeft Outer Join dbo.tblQRegion PostCodeRegions On ltrim(rtrim(addresses.post_cd)) = ltrim(rtrim(PostCodeRegions.post_code))where orgn_id in (6818982,4934392,2694595,12672918) Group By Orgn_id, Jurisdiction)k group by orgamisation) s on s.Total_Remuneration = t.Total_Remuneration and s.organisation = t.organisation[/code] |
 |
|
|
IainMaclean
Starting Member
9 Posts |
Posted - 2009-08-18 : 19:41:35
|
| Great thanks a lot. |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-08-19 : 01:51:50
|
welcome |
 |
|
|
|