Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
I have a typical requirement. My aim is to find out the race of the person by giving input as firstname, lastname and zipcode.Tbl_ZipcodeZip Blac Asian White965 12.3 3.4 2.00876 23.1 4.6 7.80Tbl_LNameLname pctBlac pctAsian pctwhiteLee 40.0 2.00 1.10Sam 2.46 1.90 3.21Tbl_FName Fname pctBlac pctAsian pctWhiteBret 2.50 2.34 1.01Oram 1.90 5.8 0.13I have 3 different tables Tbl_Zipcode, Tbl_LName, Tbl_Fname. if i have data like 'Sam','Oram','965'. If i query individually on each table i will get data like thisSelect * from Tbl_Zipcode where Zip = 965Select * from Tbl_Lname where Zip = 'Sam'Select * from Tbl_Fname where Zip = 'Oram'Zip Blac Asian White965 12.3 3.4 2.00Lname pctBlac pctAsian pctwhiteSam 2.46 1.90 3.21Fname pctBlac pctAsian pctWhiteOram 1.90 5.8 0.13Now i want to sum the values of corresponding columns. i.e Tbl_Lname.Blac + Tbl_Fname.Blac + Tbl_Zipcode.Blaci.e Blac is (2.46 + 1.90 + 12.3 = 16.66). Similarly for other columns. White( 3.21 + 0.13 + 7.80 = 11.14)Asian (1.90 + 5.8 + 4.6 = 12.3)Now among the three i need to get largest value Here i.e Blac = 16.66. I need this as Final out put.So what ever the input i give it has to select rows form individaul tables and need to sum corresponding columns and the final greatest value should be taken as outputG. Satish
madhivanan
Premature Yak Congratulator
22864 Posts
Posted - 2009-07-22 : 08:44:40
try
select top 1 sum(balc)+sum(asian)+sum(white) as summation from( Select zip,blac,asian,white from Tbl_Zipcode where Zip = 965 union all Select lname,blac,asian,white from Tbl_Lname where Zip = 'Sam' union all Select fname,blac,asian,white from Tbl_Fname where Zip = 'Oram') as tgroup by ziporder by summation desc
MadhivananFailing to plan is Planning to fail
satish.gorijala
Posting Yak Master
182 Posts
Posted - 2009-07-22 : 10:16:29
Thank You. Its working fine..
quote:Originally posted by madhivanan try
select top 1 sum(balc)+sum(asian)+sum(white) as summation from( Select zip,blac,asian,white from Tbl_Zipcode where Zip = 965 union all Select lname,blac,asian,white from Tbl_Lname where Zip = 'Sam' union all Select fname,blac,asian,white from Tbl_Fname where Zip = 'Oram') as tgroup by ziporder by summation desc
MadhivananFailing to plan is Planning to fail
G. Satish
madhivanan
Premature Yak Congratulator
22864 Posts
Posted - 2009-07-23 : 02:24:54
You are welcome MadhivananFailing to plan is Planning to fail