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 |
|
iglesia777
Starting Member
6 Posts |
Posted - 2007-01-08 : 12:06:00
|
| I am trying to get the SUM of Field1, Field2 by City_Name. Not much choice about the table structure as it is Census data.tbl_Cities:City_IDCity_Nametbl_Blocks2000:Block_IDCity_IDtbl_Blocks1990:Block_IDCity_IDtbl_Info2000:Block_IDField1tbl_Info1990:Block_IDField1Desired result:City_Name | SUM(tbl_Info2000.Field1) | SUM (tbl_Info1990.Field1)City1 | 1000 | 950City2 | 2000 | 1500Here is my SQL:SELECT c.City_Name, Sum(i2000.Field1), Sum(i1990.Field1)FROM tbl_Cities c INNER JOIN tbl_Blocks2000 b2000ON c.City_ID = b2000.City_ID INNER JOIN tbl_Info2000 i2000 ON b2000.Block_ID = i2000.Block_ID INNER JOIN tbl_Blocks1990 b1990ON c.City_ID = b1990.City_ID INNER JOIN tbl_Info1990 i1990ON b1990.Block_ID = i1990.Block_IDGROUP BY c.City_NameThe SQL is putting it in the format I want, but the totals are way too high. Am I using the right kind of JOINs, missing parenthesis? Is what I'm trying to do possible? Let me know if I need to explain what blocks are or anything else.Thanks a bunch, |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-08 : 12:27:26
|
| Give us some sample data for all tables to test our queries on.Peter LarssonHelsingborg, Sweden |
 |
|
|
iglesia777
Starting Member
6 Posts |
Posted - 2007-01-08 : 15:13:11
|
| Field names have changed slightly for an easier example (replaced cities with counties).tbl_CountiesCountyID, CountyName121,Muskegon County123,Newaygo County139,Ottawa Countytbl_Blocks1990CountyID, BlockID139,26139020100146139,26139020100148139,26139020100149121,26121000100303121,26121000100304121,26123970100101123,26123970100119123,26123970100121123,26123970100141tbl_Blocks2000CountyID, BlockID139,26139020100126139,26139020100128139,26139020100129121,26121000100207121,26121000100208121,26121000100209123,26123970100126123,26123970100127123,26123970100113tbl_Info1990BlockID, Field126121000100303,526121000100304,4626123970100101,2326123970100119,426123970100121,1626123970100141,8226139020100146,726139020100148,5526139020100149,23tbl_Info2000BlockID, Field126139020100126,2426139020100128,1426139020100129,3526121000100207,526121000100208,6526121000100209,2426123970100126,626123970100127,7026123970100113,18Thanks again. |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-01-08 : 15:45:07
|
| You cannot join multiple transactional tables all in a single SELECT without first grouping them by a common key, unless you UNION them all together.See:http://weblogs.sqlteam.com/jeffs/archive/2006/06/19/10270.aspxfor general information for this, which hopefully will guide you in the right direction.- Jeff |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-08 : 16:52:16
|
Try thisSELECT c.CountyID, c.CountyName, MAX(a.Blocks) AS '1990', MAX(b.Blocks) AS '2000'FROM tbl_Counties AS cLEFT JOIN ( SELECT b.CountyID, SUM(i.Field1) AS Blocks FROM tbl_Blocks1990 AS b LEFT JOIN tbl_Info1990 AS i ON i.BlockID = b.BlockID GROUP BY b.CountyID ) AS a ON a.CountyID = c.CountyIDLEFT JOIN ( SELECT b.CountyID, SUM(i.Field1) AS Blocks FROM tbl_Blocks2000 AS b LEFT JOIN tbl_Info2000 AS i ON i.BlockID = b.BlockID GROUP BY b.CountyID ) AS b ON b.CountyID = c.CountyIDGROUP BY c.CountyID, c.CountyNameORDER BY c.CountyID, c.CountyName Peter LarssonHelsingborg, Sweden |
 |
|
|
iglesia777
Starting Member
6 Posts |
Posted - 2007-01-08 : 17:14:46
|
| Hi Peter,I get an error when I try your SQL. Looks good, but I found another way. It's still FAR from ideal, but it works.Select CountyID, CountyName, SUM(Sum2000) as Sum2000, SUM(Sum1990) as Sum1990 FROM( SELECT c.CountyID, c.CountyName, Sum(tbl_Info2000.Field1) AS Sum2000, 0 as Sum1990 FROM tbl_Counties c JOIN tbl_Blocks2000 ON w.CountyID = tbl_Blocks2000.CountyID JOIN tbl_Info2000 ON tbl_Blocks2000.BlockID = tbl_Info2000.BlockID GROUP BY c.CountyID, c.CountyName UNION SELECT c.CountyID, c.CountyName, 0 as Sum2000, Sum(tbl_Info1990.Field1) AS Sum1990 FROM tbl_Counties c JOIN tbl_Blocks1990 ON c.CountyID = tbl_Blocks1990.CountyID JOIN tbl_Info1990 ON tbl_Blocks1990.BlockID = tbl_Info1990.BlockID GROUP BY c.CountyID, c.CountyName) tmpGroup By CountyID, CountyName |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-08 : 17:27:40
|
| Which error did you get?Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|
|