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 |
nt4vn
Yak Posting Veteran
98 Posts |
Posted - 2011-08-12 : 08:35:06
|
Hello everyone,I would like to join two sub query to get total result, but I am not sure how to make it work...First sub query to get for non-compliantselect id, acct, year, sum(amt) as amtpaidfrom table1where am_liability>0and year >2000 and year <2012AND (am_balance >0 OR am_int_total>0)group by id, acct, yearSecond is sub query to get for complianceselect id, acct, year, sum(amt) as amtpaidfrom table1where am_liability>0and year >2000 and year <2012AND am_balance =0 OR am_int_total=0group by id, acct, yearFinal result I want to show total count for non-compliant, total coutn for compliant, and total amt paid. How would I do it...Thanks, |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-08-12 : 09:21:43
|
[code]-- solution #1--First sub query to get for non-compliantselect 'non-compliant' as What, id, acct, [year], sum(amt) as amtpaidfrom table1where am_liability>0and year >2000 and year <2012AND (am_balance >0 OR am_int_total>0)group by id, acct, yearUNION ALL--Second is sub query to get for complianceselect 'compliance' as What,id, acct, [year], sum(amt) as amtpaidfrom table1where am_liability>0and year >2000 and year <2012AND am_balance =0 OR am_int_total=0group by id, acct, year-- end of solution #1--or maybe-- solution #2select * from(--First sub query to get for non-compliantselect 'non-compliant' as What, id, acct, [year], sum(amt) as amtpaidfrom table1where am_liability>0and year >2000 and year <2012AND (am_balance >0 OR am_int_total>0)group by id, acct, yearUNION ALL--Second is sub query to get for complianceselect 'compliance' as What,id, acct, [year], sum(amt) as amtpaidfrom table1where am_liability>0and year >2000 and year <2012AND am_balance =0 OR am_int_total=0group by id, acct, year) as dtorder by acct,What[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
nt4vn
Yak Posting Veteran
98 Posts |
Posted - 2011-08-12 : 09:34:08
|
If I union all, it only show noncompliant count...but I want to see both....thanks,quote: Originally posted by webfred
-- solution #1--First sub query to get for non-compliantselect 'non-compliant' as What, id, acct, [year], sum(amt) as amtpaidfrom table1where am_liability>0and year >2000 and year <2012AND (am_balance >0 OR am_int_total>0)group by id, acct, yearUNION ALL--Second is sub query to get for complianceselect 'compliance' as What,id, acct, [year], sum(amt) as amtpaidfrom table1where am_liability>0and year >2000 and year <2012AND am_balance =0 OR am_int_total=0group by id, acct, year-- end of solution #1--or maybe-- solution #2select * from(--First sub query to get for non-compliantselect 'non-compliant' as What, id, acct, [year], sum(amt) as amtpaidfrom table1where am_liability>0and year >2000 and year <2012AND (am_balance >0 OR am_int_total>0)group by id, acct, yearUNION ALL--Second is sub query to get for complianceselect 'compliance' as What,id, acct, [year], sum(amt) as amtpaidfrom table1where am_liability>0and year >2000 and year <2012AND am_balance =0 OR am_int_total=0group by id, acct, year) as dtorder by acct,What No, you're never too old to Yak'n'Roll if you're too young to die.
|
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-08-12 : 09:41:13
|
Please show us the statement that you have executed.Also please show us the results of each query without UNION. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
nt4vn
Yak Posting Veteran
98 Posts |
Posted - 2011-08-12 : 12:10:11
|
Am I right if I redo query like this:select year, month,FREQ, count(distinct a.id_acct) as 'Total Accounts',count(case when (a.am_balance >0 or a.am_int_total >0) then 1 else 0 end) as 'Non Compliant',count(case when (a.am_balance=0 and a.am_int_total=0) then 1 else 0 end) as 'Compliant'sum(amt) as Amtpaidfrom table1 awhere a.am_liability > 0 AND year >2000 AND year <2012group by year,month, freqorder by year, monthquote: Originally posted by webfred Please show us the statement that you have executed.Also please show us the results of each query without UNION. No, you're never too old to Yak'n'Roll if you're too young to die.
|
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-08-12 : 14:22:50
|
instead of COUNT(CASE ...) use SUM(CASE ...) No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-08-12 : 14:24:54
|
and: this should have been my advice instead of doing some stuff with UNION.Looks good  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|
|