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 |
MTmace
Starting Member
7 Posts |
Posted - 2008-10-10 : 12:22:16
|
I need to select from a table to get the following resultsMONTH_YEAR FitsCriteria DoesNotFitCriteria TotalRecCount10/2008 1 2 3The source table looks like this;MONTH_YEAR Criteria10/2008 010/2008 110/2008 1I can get the results of each by the followingSELECT MONTH_YEAR, COUNT(Criteria) AS FitsCriteriaFROM SOURCE_TABLE WHERE Criteria = 0GROUP BY MONTH_YEARSELECT MONTH_YEAR, COUNT(Criteria) AS DoesNotFitCriteria FROM SOURCE_TABLE WHERE Criteria = 1GROUP BY MONTH_YEARSELECT MONTH_YEAR, COUNT(Criteria) AS TotalRecCountFROM SOURCE_TABLE GROUP BY MONTH_YEARI am looking to get the expected results without using a temp table if possible.Thanks,MTmace |
|
MTmace
Starting Member
7 Posts |
Posted - 2008-10-10 : 12:41:55
|
Problem solved:select MONTH_YEAR, sum(FitsCriteria), sum(DoesNotFitCriteria) , sum(FitsCriteria)+ sum(DoesNotFitCriteria) total from (SELECT MONTH_YEAR, COUNT(Criteria) AS FitsCriteria, 0 AS DoesNotFitCriteria FROM SOURCE_TABLEWHERE FORWARDED = 0GROUP BY MONTH_YEARunionSELECT MONTH_YEAR, 0 AS FitsCriteria, COUNT(Criteria) AS DoesNotFitCriteria FROM SOURCE_TABLEWHERE Criteria = 1GROUP BY MONTH_YEAR) agroup by MONTH_YEARMTmace |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-10 : 12:50:10
|
you could simply do belowSELECT MONTH_YEAR, COUNT(CASE WHEN Criteria=0 THEN Criteria ELSE NULL END) AS FitsCriteria,COUNT(CASE WHEN Criteria=1 THEN Criteria ELSE NULL END) AS DoesNotFitCriteria,COUNT(Criteria) AS TotalRecCountFROM SOURCE_TABLE GROUP BY MONTH_YEAR |
|
|
|
|
|
|
|