| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2002-05-15 : 10:29:34
|
| Michael writes "Assume:Account level (base) tableHousehold level (aggregate) tableBranch level (further summarized aggregate) table The question is:Show me branches with the corresponding households and each account they own only when -Account Income < 50 AND Household Income > 100 To get this answer, I need to summarize from the account level (all accounts with an Income < 50) and summarize them into households so that I can get a new Household Income value.I cannot just pull the Household Income from the Household aggregate table because that number could include accounts with Income > 50. Once I have the new Household Income values, I need to filter them for only those records that have a value > 100.The trick, though, is that I need to keep the account level detail as well, because I'm trying to produce a report thatlooks like this:Branch #101 Income $100HH #203 Income $ 40Acct #424 Income $ 20Acct #387 Income $ 20HH #498 Income $ 60Acct #539 Income $ 25Acct #499 Income $ 20Acct #638 Income $ 15I am trying to do this through joins as opposed to using temporary tables to store intermediate results. Does anyoneknow how to approach this?Thanks for any insight,Michael" |
|
|
MichaelP
Jedi Yak
2489 Posts |
Posted - 2002-05-15 : 17:48:41
|
| How about something like this:CREATE TABLE #Branch(BranchNum int)CREATE TABLE #HH(HHNum int, BranchNum int)CREATE TABLE #Account(AcctNum int, HHNum int, income money)SET NOCOUNT ONINSERT INTO #Branch(BranchNum) VALUES(1)INSERT INTO #Branch(BranchNum) VALUES(2)INSERT INTO #Branch(BranchNum) VALUES(3)INSERT INTO #HH(HHNum, BranchNum) VALUES(11,1)INSERT INTO #HH(HHNum, BranchNum) VALUES(22,2)INSERT INTO #HH(HHNum, BranchNum) VALUES(33,3)INSERT INTO #HH(HHNum, BranchNum) VALUES(12,1)INSERT INTO #HH(HHNum, BranchNum) VALUES(23,2)INSERT INTO #HH(HHNum, BranchNum) VALUES(34,3)INSERT INTO #Account(AcctNum, HHNum, income) VALUES(111, 11, $10)INSERT INTO #Account(AcctNum, HHNum, income) VALUES(112, 11, $12)INSERT INTO #Account(AcctNum, HHNum, income) VALUES(113, 12, $14)INSERT INTO #Account(AcctNum, HHNum, income) VALUES(114, 12, $16)INSERT INTO #Account(AcctNum, HHNum, income) VALUES(221, 22, $20)INSERT INTO #Account(AcctNum, HHNum, income) VALUES(222, 22, $22)INSERT INTO #Account(AcctNum, HHNum, income) VALUES(223, 23, $24)INSERT INTO #Account(AcctNum, HHNum, income) VALUES(224, 23, $26)INSERT INTO #Account(AcctNum, HHNum, income) VALUES(331, 33, $30)INSERT INTO #Account(AcctNum, HHNum, income) VALUES(332, 33, $32)INSERT INTO #Account(AcctNum, HHNum, income) VALUES(333, 34, $34)INSERT INTO #Account(AcctNum, HHNum, income) VALUES(334, 34, $36)Select b.BranchNum, hh.HHNum, a.AcctNum, a.incomeFROM #Account aINNER JOIN #HH hh on hh.HHNum = a.HHNumINNER JOIN #Branch b ON b.BranchNum = hh.BranchNumSelect b.BranchNum, SUM(a.income) as BranchTotals from #Account aINNER JOIN #HH hh on hh.HHNum = a.HHNumINNER JOIN #Branch b ON b.BranchNum = hh.BranchNumGROUP BY b.BranchNumSelect hh.HHNum, SUM(a.income) as HHTotals from #Account aINNER JOIN #HH hh on hh.HHNum = a.HHNumINNER JOIN #Branch b ON b.BranchNum = hh.BranchNumGROUP BY hh.HHNumDROP TABLE #BranchDROP TABLE #accountDROP TABLE #HH |
 |
|
|
|
|
|