with totals as
(
Select month(edate) as [month],
Sum(BillAmount) as bamt,
Sum(AdultsQty+ChildQty) as qty
from CouponData
Where edate >= dateadd(year,-1,getdate())
group by month(edate)
)
select [Month],Isnull(bAmt,0) as Collection, qty as PaxTotal
from totals
order by [Month];
Чтобы иметь все месяцы:
WITH totals
AS (SELECT MONTH(edate) AS [month],
SUM(BillAmount) AS bamt,
SUM(AdultsQty + ChildQty) AS qty
FROM CouponData
WHERE edate >= DATEADD(YEAR, -1, GETDATE())
GROUP BY MONTH(edate))
SELECT m.mno AS [Month],
ISNULL(bamt, 0) AS Collection,
ISNULL(qty, 0) AS PaxTotal
FROM
(
VALUES
(1),(2),(3),
(4),(5),(6),
(7),(8),(9),
(10),(11),(12)
) m (mno)
LEFT JOIN totals t
ON t.month = m.mno
ORDER BY m.mno;