could anyone help me in using union all or other method to combine the below 3 queries??
WITH a AS (
SELECT
clientid,
DATEPART(year, row_date) AS 'Year',
DATEPART(month, row_date) AS 'Month',
value ,
CASE metricid WHEN 16 THEN 'FCR' ELSE 'Cases' END AS metric
FROM XXXXXX AS V
WHERE metricid IN (16, 11)
AND row_date BETWEEN '2012-01-01' AND '2014-10-01'
AND value IS NOT NULL)
, b AS (
SELECT clientid ,
Year ,
Month ,
value AS 'Cases',
metric
FROM a
WHERE metric = 'cases')
, c AS (
SELECT clientid ,
Year ,
Month ,
value AS 'FCR',
metric
FROM a
WHERE metric = 'FCR')
, d AS (
SELECT b.YEAR, b.MONTH, c.FCR, b.Cases
FROM b INNER JOIN c
ON c.clientid = b.clientid
AND c.[YEAR] = b.[year]
AND c.[month] = b.[month]
WHERE c.fcr <> 0 AND b.cases <> 0
)
,E AS (
SELECT [Year], [Month], SUM(FCR) AS FCR, SUM(Cases) AS Cases
FROM d
GROUP BY [Year], [Month])
select YEAR, MONTH,
CASE MONTH
WHEN 1 THEN 'Jan'
WHEN 2 THEN 'Feb'
WHEN 3 THEN 'Mar'
WHEN 4 THEN 'Apr'
WHEN 5 THEN 'May'
WHEN 6 THEN 'Jun'
WHEN 7 THEN 'Jul'
WHEN 8 THEN 'Aug'
WHEN 9 THEN 'Sep'
WHEN 10 THEN 'Oct'
WHEN 11 THEN 'Nov'
WHEN 12 THEN 'Dec'
END AS MonthName
,e.FCR AS FCRCases
,e.Cases AS TotalCases
,CASE
WHEN [month] IN (11, 12, 1) THEN 1
WHEN [month] IN (2, 3, 4) THEN 2
WHEN [month] IN (5, 6, 7) THEN 3
WHEN [month] IN (8, 9, 10) THEN 4
END AS 'Quarter'
--,CONVERT(DECIMAL(18, 2),(e.FCR/e.Cases)*100) AS FCRRaw
from e
order by YEAR, MONTH
**************2nd query*************
WITH a AS (
SELECT
clientid,
DATEPART(year, row_date) AS 'Year',
DATEPART(month, row_date) AS 'Month',
value,
CASE metricid WHEN 56 THEN 'numerator' ELSE 'denominator' END AS metric
FROM XXXXXXX.[Values] AS V
WHERE metricid IN (56, 10)
--WHERE metricid IN (11,16)
AND row_date BETWEEN '2013-10-01' AND '2014-02-01'
AND value IS NOT NULL)
, b AS (
SELECT clientid ,
Year ,
Month ,
value AS 'numerator',
metric
FROM a
WHERE metric = 'numerator')
, c AS (
SELECT clientid ,
Year ,
Month ,
value AS 'denominator',
metric
FROM a
WHERE metric = 'denominator')
, d AS (
SELECT b.YEAR, b.MONTH, c.denominator, b.numerator
FROM b INNER JOIN c
ON c.clientid = b.clientid
AND c.[YEAR] = b.[year]
AND c.[month] = b.[month]
WHERE c.denominator <> 0 AND b.numerator <> 0
)
, e AS (
SELECT [Year], [Month], SUM(numerator) AS numerator, SUM(denominator) AS denominator
FROM d
GROUP BY [Year], [Month]
)
SELECT *,
CASE
WHEN [month] IN (11, 12, 1) THEN 1
WHEN [month] IN (2, 3, 4) THEN 2
WHEN [month] IN (5, 6, 7) THEN 3
WHEN [month] IN (8, 9, 10) THEN 4
END AS 'Quarter'
FROM e
ORDER BY 1,2
******************3rd query**************
WITH a AS (
SELECT --L.[LocationGroupId],
--T.locationid,
--T.AccountId,
TR.datestamp,
/*Convert(NVARCHAR, DatePArt(year, TR.datestamp)) + '-' + Convert(NVARCHAR, DatePArt(month, TR.datestamp)) + '-01'*/
TR.Period AS ValueDate,
CASE WHEN TR.TargetResultState = 0 THEN 0 WHEN TR.TargetResultState = 1 THEN 1 WHEN TR.TargetResultState = 2 THEN 1 ELSE 0 END AS Met,
CASE WHEN CONVERT(DATE, Convert(NVARCHAR, DatePArt(year, TR.datestamp)) + '-' + Convert(NVARCHAR, DatePArt(month, TR.datestamp)) + '-01') > T.Startdate AND CONVERT(DATE, Convert(NVARCHAR, DatePArt(year, TR.datestamp))+ '-' + Convert(NVARCHAR, DatePArt(month, TR.datestamp)) + '-01') < T.Enddate THEN 1 ELSE 0 END AS ActiveTarget
FROM XXXXXX AS TR
INNER JOIN dbo.Target T ON TR.TargetID = T.ID
--INNER JOIN dbo.Location L ON T.Locationid = L.Id
WHERE --locationid <> - 1 AND
TR.Period IN ('201306', '201307', '201308', '201309', '201310', '201311', '201312', '201401'))
select ValueDate, SUM(Met) AS Met, Count(ActiveTarget) AS ActiveTargets,
right(ValueDate,2) as Month
,left(ValueDate,4) as Year
,CASE
WHEN right(ValueDate,2) IN (11, 12, 1) THEN 1
WHEN right(ValueDate,2) IN (2, 3, 4) THEN 2
WHEN right(ValueDate,2) IN (5, 6, 7) THEN 3
WHEN right(ValueDate,2) IN (8, 9, 10) THEN 4
END AS 'Quarter'
from a
group by ValueDate
order by ValueDate