SQL group by within sum() - sql

I have the following sql statement which produces the following output (filtered result for 7/8 DueDate)
SELECT
JobType.BillingCategory,
Jobs.DueDate,
Sum(Impressions.PRINTtot) AS SumOfPRINTtot,
Sum(Impressions.PRINTrem) AS SumOfPRINTrem,
Sum(Impressions.CARDtot) AS SumOfCARDtot,
Sum(Impressions.CARDrem) AS SumOfCARDrem,
Sum(Impressions.BOOKtot) AS SumOfBOOKtot,
Sum(Impressions.BOOKrem) AS SumOfBOOKrem
FROM
(
Impressions
INNER JOIN Jobs ON Impressions.JobNo = Jobs.JobNo
)
INNER JOIN JobType ON (Jobs.AccountName = JobType.AccountName)
AND (Jobs.Product = JobType.Product)
GROUP BY
Jobs.DueDate,
JobType.BillingCategory;
I am trying to get all of these results on one line: the identifier would be the DueDate and the sums of the values in the Impressions table would be summed for each BillingCategory. Example below (omitting CARD & BOOK sums just for visual purposes w/ too many columns)

You could use a CASE expression to summarize your data as such. You could modify your query to sum for only that billing category, I have used CARD in the example below to summarize the metrics for Impressions.PRINTtot and SumOfPRINTrem
SELECT
Jobs.DueDate,
Sum(CASE WHEN JobType.BillingCategory='CARD' THEN Impressions.PRINTtot ELSE 0 END) AS SumOfPRINTtotCard,
Sum(CASE WHEN JobType.BillingCategory='CARD' THEN Impressions.PRINTrem ELSE 0 END) AS SumOfPRINTremCard,
....<repeat>
FROM
(
Impressions
INNER JOIN Jobs ON Impressions.JobNo = Jobs.JobNo
)
INNER JOIN JobType ON (Jobs.AccountName = JobType.AccountName)
AND (Jobs.Product = JobType.Product)
GROUP BY
Jobs.DueDate
Edit 1:
Based on the Billing Categories listed in your question
A complete example may look like:
SELECT
Jobs.DueDate,
SUM(CASE WHEN JobType.BillingCategory='CARD' THEN Impressions.PRINTtot ELSE 0 END) AS SumOfPRINTtotCARD,
SUM(CASE WHEN JobType.BillingCategory='CARD' THEN Impressions.PRINTrem ELSE 0 END) AS SumOfPRINTremCARD,
SUM(CASE WHEN JobType.BillingCategory='CARD' THEN Impressions.CARDtot ELSE 0 END) AS SumOfCARDtotCARD,
SUM(CASE WHEN JobType.BillingCategory='CARD' THEN Impressions.CARDrem ELSE 0 END) AS SumOfCARDremCARD,
SUM(CASE WHEN JobType.BillingCategory='CARD' THEN Impressions.BOOKtot ELSE 0 END) AS SumOfBOOKtotCARD,
SUM(CASE WHEN JobType.BillingCategory='CARD' THEN Impressions.BOOKrem ELSE 0 END) AS SumOfBOOKremCARD,
SUM(CASE WHEN JobType.BillingCategory='CARDTIPON' THEN Impressions.PRINTtot ELSE 0 END) AS SumOfPRINTtotCARDTIPON,
SUM(CASE WHEN JobType.BillingCategory='CARDTIPON' THEN Impressions.PRINTrem ELSE 0 END) AS SumOfPRINTremCARDTIPON,
SUM(CASE WHEN JobType.BillingCategory='CARDTIPON' THEN Impressions.CARDtot ELSE 0 END) AS SumOfCARDtotCARDTIPON,
SUM(CASE WHEN JobType.BillingCategory='CARDTIPON' THEN Impressions.CARDrem ELSE 0 END) AS SumOfCARDremCARDTIPON,
SUM(CASE WHEN JobType.BillingCategory='CARDTIPON' THEN Impressions.BOOKtot ELSE 0 END) AS SumOfBOOKtotCARDTIPON,
SUM(CASE WHEN JobType.BillingCategory='CARDTIPON' THEN Impressions.BOOKrem ELSE 0 END) AS SumOfBOOKremCARDTIPON,
SUM(CASE WHEN JobType.BillingCategory='EOB' THEN Impressions.PRINTtot ELSE 0 END) AS SumOfPRINTtotEOB,
SUM(CASE WHEN JobType.BillingCategory='EOB' THEN Impressions.PRINTrem ELSE 0 END) AS SumOfPRINTremEOB,
SUM(CASE WHEN JobType.BillingCategory='EOB' THEN Impressions.CARDtot ELSE 0 END) AS SumOfCARDtotEOB,
SUM(CASE WHEN JobType.BillingCategory='EOB' THEN Impressions.CARDrem ELSE 0 END) AS SumOfCARDremEOB,
SUM(CASE WHEN JobType.BillingCategory='EOB' THEN Impressions.BOOKtot ELSE 0 END) AS SumOfBOOKtotEOB,
SUM(CASE WHEN JobType.BillingCategory='EOB' THEN Impressions.BOOKrem ELSE 0 END) AS SumOfBOOKremEOB,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDE' THEN Impressions.PRINTtot ELSE 0 END) AS SumOfPRINTtotMEMBERGUIDE,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDE' THEN Impressions.PRINTrem ELSE 0 END) AS SumOfPRINTremMEMBERGUIDE,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDE' THEN Impressions.CARDtot ELSE 0 END) AS SumOfCARDtotMEMBERGUIDE,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDE' THEN Impressions.CARDrem ELSE 0 END) AS SumOfCARDremMEMBERGUIDE,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDE' THEN Impressions.BOOKtot ELSE 0 END) AS SumOfBOOKtotMEMBERGUIDE,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDE' THEN Impressions.BOOKrem ELSE 0 END) AS SumOfBOOKremMEMBERGUIDE,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDEHD' THEN Impressions.PRINTtot ELSE 0 END) AS SumOfPRINTtotMEMBERGUIDEHD,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDEHD' THEN Impressions.PRINTrem ELSE 0 END) AS SumOfPRINTremMEMBERGUIDEHD,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDEHD' THEN Impressions.CARDtot ELSE 0 END) AS SumOfCARDtotMEMBERGUIDEHD,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDEHD' THEN Impressions.CARDrem ELSE 0 END) AS SumOfCARDremMEMBERGUIDEHD,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDEHD' THEN Impressions.BOOKtot ELSE 0 END) AS SumOfBOOKtotMEMBERGUIDEHD,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDEHD' THEN Impressions.BOOKrem ELSE 0 END) AS SumOfBOOKremMEMBERGUIDEHD
FROM
(
Impressions
INNER JOIN Jobs ON Impressions.JobNo = Jobs.JobNo
)
INNER JOIN JobType ON (Jobs.AccountName = JobType.AccountName)
AND (Jobs.Product = JobType.Product)
GROUP BY
Jobs.DueDate
With a specific database/tool there may be various functions that may prove useful. However, I find in these cases especially since your billing categories may change over time, a script where you can run anywhere is sometimes helpful. I've included the script I used to generate the code below
var types='CARD,CARDTIPON,EOB,MEMBERGUIDE,MEMBERGUIDEHD'.split(',');
var metrics = metrics='PRINTtot,PRINTrem,CARDtot,CARDrem,BOOKtot,BOOKrem'.split(',');
var metricTemplate="SUM(CASE WHEN JobType.BillingCategory='[TYPE]' THEN Impressions.[METRICNAME] ELSE 0 END) AS SumOf[METRICNAME][TYPE]";
var summary_lines = []
for(var i=0;i < types.length;i++){
for(var j=0;j<metrics.length;j++){
summary_lines.push(metricTemplate.replaceAll('[TYPE]',types[i]).replaceAll('[METRICNAME]',metrics[j]))
}
}
complete_metrics = summary_lines.join(",\n");
console.log(complete_metrics)

The simplest option is to use your query as a CTE (Common Table Expression) and then you can use it as a base for another query.
For example:
with
q as (
-- your query here
)
select
max(DueDate) as DueDate,
sum(case when BillingCategory = 'CARD' then SumOfPRINTtot else 0 end) as SumOfPRINTtotC,
sum(case when BillingCategory = 'CARD' then SumOfPRINTrem else 0 end) as SumOfPRINTremC,
sum(case when BillingCategory = 'CARDTIPON' then SumOfPRINTtot else 0 end) as SumOfPRINTtotCT,
sum(case when BillingCategory = 'CARDTIPON' then SumOfPRINTrem else 0 end) as SumOfPRINTremCT,
sum(case when BillingCategory = 'EOB' then SumOfPRINTtot else 0 end) as SumOfPRINTtotE,
sum(case when BillingCategory = 'EOB' then SumOfPRINTrem else 0 end) as SumOfPRINTremE,
sum(case when BillingCategory = 'MEMBERGUIDE' then SumOfPRINTtot else 0 end) as SumOfPRINTtotMG,
sum(case when BillingCategory = 'MEMBERGUIDE' then SumOfPRINTrem else 0 end) as SumOfPRINTremMG,
sum(case when BillingCategory = 'MEMBERGUIDEHD' then SumOfPRINTtot else 0 end) as SumOfPRINTtotMGH,
sum(case when BillingCategory = 'MEMBERGUIDEHD' then SumOfPRINTrem else 0 end) as SumOfPRINTremMGH
from q
In some databases you can use the FILTER clause as well. You don't mention which specific database, so this solution will work on virtually all databases.

Related

Count the number of column having non zero values

I have the following script,
SELECT COUNT(*) AS Total,
SUM(CASE WHEN TypeId ='4' THEN 1 ELSE 0 END) AS 'TotalCount1',
SUM(CASE WHEN TypeId ='6' THEN 1 ELSE 0 END) AS 'TotalCount2',
SUM(CASE WHEN TypeId ='1' THEN 1 ELSE 0 END) AS 'TotalCount3',
SUM(CASE WHEN TypeId ='10' THEN 1 ELSE 0 END) AS 'TotalCount4',
SUM(CASE WHEN TypeId ='5' THEN 1 ELSE 0 END) AS 'TotalCount5',
SUM(CASE WHEN TypeId ='8' THEN 1 ELSE 0 END) AS 'TotalCount6'
FROM [Party]
Please refer the screenshot as the output of the above script.
What I want:
I want a column after the Total as the total number of the column having nonzero values.
Like in the picture the values should be 2 as TotalCount1 and Totalcount3 have non zero values.
SELECT COUNT(*) AS Total,
...
...
CASE ( WHEN SUM(CASE WHEN TypeId ='4' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='6' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='1' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='10' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='5' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='8' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END)
as SumOfNonZeros
FROM [Party]
Or maybe simpler
SELECT COUNT(*) AS Total,
COUNT(CASE WHEN TypeId ='4' THEN 1 END) AS 'TotalCount1',
COUNT(CASE WHEN TypeId ='6' THEN 1 END) AS 'TotalCount2',
COUNT(CASE WHEN TypeId ='1' THEN 1 END) AS 'TotalCount3',
COUNT(CASE WHEN TypeId ='10' THEN 1 END) AS 'TotalCount4',
COUNT(CASE WHEN TypeId ='5' THEN 1 END) AS 'TotalCount5',
COUNT(CASE WHEN TypeId ='8' THEN 1 END) AS 'TotalCount6',
COUNT( DISTINCT CASE WHEN TypeId IN ('4', '6', '1', '10', '5', '8')
THEN TypeId
END ) as CountOfNonZeros
FROM [Party]
Using CASE expression over the selecting result, you can do this:
SELECT (
CASE WHEN TotalCount1 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount2 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount3 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount4 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount5 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount6 > 0 THEN 1 ELSE 0 END) AS Result
FROM (
SELECT COUNT(*) AS Total,
SUM(CASE WHEN TypeId ='4' THEN 1 ELSE 0 END) AS 'TotalCount1',
SUM(CASE WHEN TypeId ='6' THEN 1 ELSE 0 END) AS 'TotalCount2',
SUM(CASE WHEN TypeId ='1' THEN 1 ELSE 0 END) AS 'TotalCount3',
SUM(CASE WHEN TypeId ='10' THEN 1 ELSE 0 END) AS 'TotalCount4',
SUM(CASE WHEN TypeId ='5' THEN 1 ELSE 0 END) AS 'TotalCount5',
SUM(CASE WHEN TypeId ='8' THEN 1 ELSE 0 END) AS 'TotalCount6'
FROM [Party]
) A

How to get Total of columns vertically in sql

I have got the total horizontally, I want to achieve the last row which is in blue color, I have used this query
SELECT
DepartmentName,
SUM(CASE WHEN Risk = 'High' THEN 1 ELSE 0 END) High,
SUM(CASE WHEN Risk = 'Medium' THEN 1 ELSE 0 END) Medium,
SUM(CASE WHEN Risk = 'Low' THEN 1 ELSE 0 END) Low,
SUM(CASE WHEN Risk = 'High' THEN 1 ELSE 0 END)
+ SUM(CASE WHEN Risk = 'Medium' THEN 1 ELSE 0 END)
+ SUM(CASE WHEN Risk = 'Low' THEN 1 ELSE 0 END) Total
FROM
TableName
GROUP BY
DepartmentName
How can I get the last row?
This can be done with Grouping sets
SELECT COALESCE(DepartmentName, 'Total'),
SUM(CASE WHEN Risk = 'High' THEN 1 ELSE 0 END) High,
SUM(CASE WHEN Risk = 'Medium' THEN 1 ELSE 0 END) Medium,
SUM(CASE WHEN Risk = 'Low' THEN 1 ELSE 0 END) Low,
SUM(CASE WHEN Risk = 'High' THEN 1 ELSE 0 END)
+ SUM(CASE WHEN Risk = 'Medium' THEN 1 ELSE 0 END)
+ SUM(CASE WHEN Risk = 'Low' THEN 1 ELSE 0 END) Total
FROM TableName
GROUP BY GROUPING SETS ((DepartmentName), ())
Same that could be also achieve by using ROLLUP
SELECT
COALESCE(DepartmentName, 'Total') DepartmentName,
SUM(CASE WHEN Risk = 'High' THEN 1 ELSE 0 END) High,
SUM(CASE WHEN Risk = 'Medium' THEN 1 ELSE 0 END) Medium,
SUM(CASE WHEN Risk = 'Low' THEN 1 ELSE 0 END) Low,
SUM(CASE WHEN Risk IN ('High', 'Medium', 'Low') THEN 1 ELSE 0 END) Total
FROM table
GROUP BY DepartmentName with ROLLUP

How to do mathematical comparision with SQL CASE statement

I have a student table around 100k records and I have two types of data in it: student name and level type with selection values primary, secondary, intermediate & university
I want to filter out the student from this table, whose have count > 0, in all level primary, secondary, intermediate & university
I was able to find the sum for each student in each level using the following query
SELECT
student_id,
SUM(CASE WHEN lev_type = 'primary' THEN 1 ELSE 0 END) AS primary,
SUM(CASE WHEN lev_type = 'secondary' THEN 1 ELSE 0 END) AS secondary,
SUM(CASE WHEN lev_type = 'intermediate' THEN 1 ELSE 0 END) AS intermediate,
SUM(CASE WHEN level_type = 'university' THEN 1 ELSE 0 END) AS university
FROM
student_details
GROUP BY
student_id
and I am getting a result like (note that my result is 92242 row(s))
attendee_id primary secondary intermediate uni
student1 0 1 1 2
student2 0 1 1 0
student3 88 209 92 32
student4 0 1 1 0
student5 0 1 1 0
How to filter out student3 from this result?
You can simply add a where statement as follows:
SELECT student_id,
SUM(case when lev_type = 'primary' then 1 else 0 end) as primary,
SUM(case when lev_type = 'secondary' then 1 else 0 end) as secondary ,
SUM(case when lev_type = 'intermediate' then 1 else 0 end) as intermediate ,
SUM(case when lev_type = 'university' then 1 else 0 end) as university
FROM student_details
GROUP BY student_id
WHERE primary = 0 OR secondary = 0 OR intermediate = 0 OR university = 0
HAVING might get you what you want. For example:
SELECT student_id,
sum(case when lev_type = 'primary' then 1 else 0 end) as primary,
sum(case when lev_type = 'secondary' then 1 else 0 end) as secondary ,
sum(case when lev_type = 'intermediate' then 1 else 0 end) as intermediate ,
sum(case when level_type = 'university' then 1 else 0 end) as university
from student_details
group by student_id
-- Put the criteria here by which you want to filter
having sum(case when lev_type = 'primary' then 1 else 0 end) = 0
and sum(case when lev_type = 'secondary' then 1 else 0 end) = 0
and sum(case when lev_type = 'intermediate' then 1 else 0 end) = 0
and sum(case when level_type = 'university' then 1 else 0 end) = 0

Full outer join with "case when" and subquery

I have a full outer join query with case when and sub query in oracle. What I am trying to accomplish is aggregating current year data and previous year data from the same table in order to compare them. However my FULL OUTER JOIN is acting as an inner join not returning the null values from both the current year and the previous year.
Here is my code:
SELECT
SQ1.CHANNEL,
SQ1.SHORT,
SQ1.NAME,
SQ1.RDC,
SQ1.CY_APPROVED_COUNT,
SQ2.PY_APPROVED_COUNT,
SQ1.CY_APPROVED_VOLUME,
SQ2.PY_APPROVED_VOLUME,
SQ1.CY_DECLINED_COUNT,
SQ2.PY_DECLINED_COUNT,
SQ1.CY_DECLINED_VOLUME,
SQ2.PY_DECLINED_VOLUME,
SQ1.CY_RETURNED_COUNT,
SQ2.PY_RETURNED_COUNT,
SQ1.CY_RETURNED_VOLUME,
SQ2.PY_RETURNED_VOLUME
FROM ( SELECT
CHANNEL,
SHORT,
NAME,
RDC,
SUM (CASE WHEN STATUS = 'Approved' THEN APP_COUNTS ELSE 0 END) AS CY_APPROVED_COUNT,
SUM (CASE WHEN STATUS = 'Approved' THEN PROJ_VOL ELSE 0 END) AS CY_APPROVED_VOLUME,
SUM (CASE WHEN STATUS = 'Declined' THEN APP_COUNTS ELSE 0 END) AS CY_DECLINED_COUNT,
SUM (CASE WHEN STATUS = 'Declined' THEN PROJ_VOL ELSE 0 END) AS CY_DECLINED_VOLUME,
SUM (CASE WHEN STATUS = 'Returned' THEN APP_COUNTS ELSE 0 END) AS CY_RETURNED_COUNT,
SUM (CASE WHEN STATUS = 'Returned' THEN PROJ_VOL ELSE 0 END) AS CY_RETURNED_VOLUME
FROM WFRT_MSP_SP_MTD
WHERE PERIOD >= TO_DATE('2016/02/01', 'yyyy/mm/dd')
AND PERIOD <= TO_DATE('2016/02/13','yyyy/mm/dd')
AND CHANNEL = 'MSP'
AND RDC = 'BASE'
GROUP BY
CHANNEL,
SHORT,
NAME,
RDC
) SQ1
-- NOT CORRECTLY SHOWING NULL VALUES
FULL OUTER JOIN
( SELECT
CHANNEL,
SHORT,
NAME,
RDC,
SUM (CASE WHEN STATUS = 'Approved' THEN APP_COUNTS ELSE 0 END) AS PY_APPROVED_COUNT,
SUM (CASE WHEN STATUS = 'Approved' THEN PROJ_VOL ELSE 0 END) AS PY_APPROVED_VOLUME,
SUM (CASE WHEN STATUS = 'Declined' THEN APP_COUNTS ELSE 0 END) AS PY_DECLINED_COUNT,
SUM (CASE WHEN STATUS = 'Declined' THEN PROJ_VOL ELSE 0 END) AS PY_DECLINED_VOLUME,
SUM (CASE WHEN STATUS = 'Returned' THEN APP_COUNTS ELSE 0 END) AS PY_RETURNED_COUNT,
SUM (CASE WHEN STATUS = 'Returned' THEN PROJ_VOL ELSE 0 END) AS PY_RETURNED_VOLUME
FROM WFRT_MSP_SP_MTD
WHERE PERIOD >= TO_DATE('2015/02/01', 'yyyy/mm/dd')
AND PERIOD <= TO_DATE('2015/02/13','yyyy/mm/dd')
AND CHANNEL = 'MSP'
AND RDC = 'BASE'
GROUP BY
CHANNEL,
SHORT,
NAME,
RDC
) SQ2
ON sq1.short = sq2.short
;
Please help if you can.
Just use conditional aggregation:
SELECT CHANNEL, SHORT, NAME, RDC,
SUM(CASE WHEN this_year = 1 AND STATUS = 'Approved' THEN APP_COUNTS ELSE 0 END) AS CY_APPROVED_COUNT,
SUM(CASE WHEN this_year = 1 AND STATUS = 'Approved' THEN PROJ_VOL ELSE 0 END) AS cY_APPROVED_VOLUME,
SUM(CASE WHEN this_year = 1 AND STATUS = 'Declined' THEN APP_COUNTS ELSE 0 END) AS CY_DECLINED_COUNT,
SUM(CASE WHEN this_year = 1 AND STATUS = 'Declined' THEN PROJ_VOL ELSE 0 END) AS CY_DECLINED_VOLUME,
SUM(CASE WHEN this_year = 1 AND STATUS = 'Returned' THEN APP_COUNTS ELSE 0 END) AS CY_RETURNED_COUNT,
SUM(CASE WHEN this_year = 1 AND STATUS = 'Returned' THEN PROJ_VOL ELSE 0 END) AS CY_RETURNED_VOLUME,
SUM(CASE WHEN prev_year = 1 AND STATUS = 'Approved' THEN APP_COUNTS ELSE 0 END) AS PY_APPROVED_COUNT,
SUM(CASE WHEN prev_year = 1 AND STATUS = 'Approved' THEN PROJ_VOL ELSE 0 END) AS PY_APPROVED_VOLUME,
SUM(CASE WHEN prev_year = 1 AND STATUS = 'Declined' THEN APP_COUNTS ELSE 0 END) AS PY_DECLINED_COUNT,
SUM(CASE WHEN prev_year = 1 AND STATUS = 'Declined' THEN PROJ_VOL ELSE 0 END) AS PY_DECLINED_VOLUME,
SUM(CASE WHEN prev_year = 1 AND STATUS = 'Returned' THEN APP_COUNTS ELSE 0 END) AS PY_RETURNED_COUNT,
SUM(CASE WHEN prev_year = 1 AND STATUS = 'Returned' THEN PROJ_VOL ELSE 0 END) AS PY_RETURNED_VOLUME
FROM (SELECT msm.*,
(CASE WHEN PERIOD >= DATE '2015-02-01' AND
PERIOD <= '2015-02-13'
THEN 1 ELSE 0
END) as prev_year,
(CASE WHEN PERIOD >= DATE '2016-02-01' AND
PERIOD <= '2016-02-13'
THEN 1 ELSE 0
END) as this_year
FROM WFRT_MSP_SP_MTD msm
) msm
WHERE CHANNEL = 'MSP' AND RDC = 'BASE'
GROUP BY CHANNEL, SHORT, NAME, RDC;

Sum data for many different results for same field

I am trying to find a better way to write this sql server code 2008. It works and data is accurate. Reason i ask is that i will be asked to do this for several other reports going forward and want to reduce the amount of code to upkeep going forward.
How can i take a field where i sum for the yes/no/- (dash) in each field without doing an individual sum as i have in code. Each table is a month of detail data which i sum using in a CTE. i changed the table name for each month and Union All to put data together. Is there a better way to do this. This is a small sample of code. Thanks for the help.
WITH H AS (
SELECT 'August' AS Month_Name
, SUM(CASE WHEN G.FFS = '-' THEN 1 ELSE 0 END) AS FFS_Dash
, SUM(CASE WHEN G.FFS = 'Yes' THEN 1 ELSE 0 END) AS FFS_Yes
, SUM(CASE WHEN G.FFS = 'No' THEN 1 ELSE 0 END) AS FFS_No
, SUM(CASE WHEN G.DNA = '-' THEN 1 ELSE 0 END) AS DNA_Dash
, SUM(CASE WHEN G.DNA = 'Yes' THEN 1 ELSE 0 END) AS DNA_Yes
, SUM(CASE WHEN G.DNA = 'No' THEN 1 ELSE 0 END) AS DNA_No
FROM table08 G )
, G AS (
SELECT 'July' AS Month_Name
, SUM(CASE WHEN G.FFS = '-' THEN 1 ELSE 0 END) AS FFS_Dash
, SUM(CASE WHEN G.FFS = 'Yes' THEN 1 ELSE 0 END) AS FFS_Yes
, SUM(CASE WHEN G.FFS = 'No' THEN 1 ELSE 0 END) AS FFS_No
, SUM(CASE WHEN G.DNA = '-' THEN 1 ELSE 0 END) AS DNA_Dash
, SUM(CASE WHEN G.DNA = 'Yes' THEN 1 ELSE 0 END) AS DNA_Yes
, SUM(CASE WHEN G.DNA = 'No' THEN 1 ELSE 0 END) AS DNA_No
FROM table07 G )
select * from H
UNION ALL
select * from G
How about:
SELECT Month_Name,
SUM(CASE WHEN G.FFS = '-' THEN 1 ELSE 0 END) AS FFS_Dash,
SUM(CASE WHEN G.FFS = 'Yes' THEN 1 ELSE 0 END) AS FFS_Yes,
SUM(CASE WHEN G.FFS = 'No' THEN 1 ELSE 0 END) AS FFS_No,
SUM(CASE WHEN G.DNA = '-' THEN 1 ELSE 0 END) AS DNA_Dash,
SUM(CASE WHEN G.DNA = 'Yes' THEN 1 ELSE 0 END) AS DNA_Yes,
SUM(CASE WHEN G.DNA = 'No' THEN 1 ELSE 0 END) AS DNA_No
FROM ((select 'July' as Month_Name, G.*
from table07 G
) union all
(select 'August', H.*
from table08 H
)
) gh
GROUP BY Month_Name;
However, having tables with the same structure is usually a sign of poor database design. You should have a single table with a column representing the month.