我写了这样的查询
SELECT
c.DivisionNameEn,
a.LF_CompanyDivisionID,
0 as code,
SUM(a.totInvoiceAmnt) AS Sales,
'Sales' Salesperson,
sum(a.totInvoiceAmnt) as Sum ,
sum(a.totInvoiceAmnt) /sum(a.totInvoiceAmnt) *100 as MonthPercent ,
YEAR(a.TransactionDate) [Year],
MONTH(a.TransactionDate) [Month],
CASE
WHEN datepart(month,a.TransactionDate) = 1 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 1 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 2 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 2 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 3 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 4 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 5 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 6 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 7 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 8 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 9 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 10 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 10 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 11 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 12 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
ELSE 0
END as MonthTotal
from POS_salesHeader a
INNER JOIN dbo.STR_CompanyDivision AS c ON c.id = a.LF_CompanyDivisionID
--WHERE YEAR(a.TransactionDate) = YEAR(GETDATE()) AND MONTH(a.TransactionDate) = MONTH(GETDATE()) /*To filter by month*/
GROUP BY YEAR(a.TransactionDate), MONTH(a.TransactionDate) , c.DivisionNameEn ,LF_CompanyDivisionID
它是这样给我的
我想要一行给我一个月总计totInvoiceAmnt,直到现在年份总计totInvoiceAmnt例如,如果今天它的4月15日它给了我总的totInvoiceAmnt只有四月的列名为MonthsTotal而在另一列它给我直到现在每年totInvoiceAmnt从1月份到2月份+ 3月份+ 4月份的总数