vendredi 1 juillet 2016

UNION ALL - Prevent Combining of Colimns

I tried the following but i ended up with a single column Amount2 containing SUM's from Amount1 too.

SELECT 
    YEAR(createdDate) as Year, 
    MONTH(createdDate) AS Month, 
    Sum(GrandTotal) AS Amount1
FROM 
    Quotes
WHERE createdDate BETWEEN @DateFrom AND @DateTo
GROUP BY YEAR(createdDate), MONTH(createdDate)
--ORDER BY YEAR(createdDate), MONTH(createdDate)

UNION ALL

SELECT 
    YEAR(createdDate) as Year, 
    MONTH(createdDate) AS Month, 
    Sum(GrandTotal) AS Amount2
FROM 
    Quotes
WHERE createdDate BETWEEN @DateFrom AND @DateTo
AND orderDate IS NOT NULL
GROUP BY YEAR(createdDate), MONTH(createdDate)
ORDER BY YEAR(createdDate), MONTH(createdDate);

However i want to keep both Amount1 and Amount2 columns/amounts.

Aucun commentaire:

Enregistrer un commentaire