Question

I am having trouble combining these two queries into one. I tried to make the second one a sub query but kept getting an error due to multiple results being returned.

SELECT DATENAME(mm, vn.Created) AS CreatedMnth, Count(vn.ncmr) AS CreateCt
FROM viewNCMRs vn
GROUP BY DATENAME(mm, vn.Created)

SELECT DATENAME(mm, vn.DateClosed) AS ClosedMnth, Count(vn.ncmr) AS CloseCt 
FROM viewNCMRs vn
WHERE vn.DateClosed IS NOT NULL
GROUP BY DATENAME(mm, vn.DateClosed)

Any help would be greatly appreciated

Edited to add expected table of results:

_year _Month CreateCt CloseCt
2008___1_____150____149
2008___2_____105____106

etc..

Was it helpful?

Solution 3

;WITH CreateDate AS 
(
    SELECT DATEPART(mm, vn.Created) AS CreatedMnth, 
           Count(vn.ncmr) AS CreateCt,
           DATEPART(YY,vn.Created) AS yr
    FROM viewNCMRs vn
    GROUP BY DATEPART(YY,vn.Created),
             DATEPART(mm, vn.Created)
)
,
CloseDate AS 
(
    SELECT DATEPART(mm, vn.DateClosed) AS ClosedMnth, 
           Count(vn.ncmr) AS CloseCt,
           DATEPART(YY,vn.DateClosed) AS yr
    FROM viewNCMRs vn
    WHERE vn.DateClosed IS NOT NULL
    GROUP BY DATEPART(YY,vn.DateClosed),
             DATEPART(mm, vn.DateClosed)
)
,
AllMonths AS
(
    SELECT 1 AS _Month

    UNION ALL

    SELECT _Month + 1
    FROM AllMonths
    WHERE _Month < 12
)
,
ALLYears AS 
(
    SELECT DATEPART(YY,vn.Created) Yr
    FROM viewNCMRs vn

    UNION 

    SELECT DATEPART(YY,vn.DateClosed) Yr
    FROM viewNCMRs vn

)
SELECT AY.Yr _Year,
       AM._Month,
       ISNULL(CD.CreateCt,0) CreateCt,
       ISNULL(ClD.CloseCt,0) CloseCt
FROM ALLYears AY
CROSS JOIN AllMonths AM
LEFT JOIN CreateDate CD
ON AY.Yr = CD.yr
AND AM._Month = CD.CreatedMnth
LEFT JOIN CloseDate ClD
ON AY.Yr = ClD.yr
AND AM._Month = ClD.ClosedMnth
WHERE CD.CreateCt IS NOT NULL
OR  ClD.CloseCt IS NOT NULL

Try this query. It will give you the expected result. WHERE clause will filter out all the rows where CreateCt and CloseCt count is Zero.

OTHER TIPS

SELECT  DATENAME(MONTH, Created) AS CreatedMnth
      , Count(ncmr)           AS CreateCt
      , Count(CASE WHEN DateClosed IS NOT NULL THEN 1 ELSE NULL END) AS CloseCt 
FROM  viewNCMRs 
GROUP BY DATENAME(MONTH, Created)

Both these queries cannot be put in one single query as the group by clause is on 2 different columns. You can do following stuff:

;WITH CreateDate AS 
(
    SELECT DATENAME(mm, vn.Created) AS CreatedMnth, Count(vn.ncmr) AS CreateCt
    FROM viewNCMRs vn
    GROUP BY DATENAME(mm, vn.Created)
)
,
CloseDate AS 
(
    SELECT DATENAME(mm, vn.DateClosed) AS ClosedMnth, Count(vn.ncmr) AS CloseCt 
    FROM viewNCMRs vn
    WHERE vn.DateClosed IS NOT NULL
    GROUP BY DATENAME(mm, vn.DateClosed)
)
,
AllMonths AS
(
    SELECT 1 AS _Month

    UNION ALL

    SELECT _Month + 1
    FROM AllMonths
    WHERE _Month < 12
)
SELECT AM._Month,
       CD.CreateCt,
       ClD.CloseCt
FROM AllMonths AM
LEFT JOIN CreateDate CD
ON AM._Month = CD.CreatedMnth
LEFT JOIN CloseDate ClD
ON AM._Month = ClD.ClosedMnth
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top