我有以下内容返回给定类型作业的总发票金额。我现在想把它分解成月总数。关于如何处理这个问题的任何提示
SELECT dbo.jm_job_type.job_type_desc, SUM(dbo.jm_invoice.invoice_amount) AS 'inv tot'
FROM dbo.jm_invoice INNER JOIN
dbo.jm_job ON dbo.jm_invoice.job_no = dbo.jm_job.job_no INNER JOIN
dbo.jm_job_type ON dbo.jm_job.job_type_no = dbo.jm_job_type.job_type_no
GROUP BY dbo.jm_job_type.job_type_desc
答案 0 :(得分:1)
在Group By中使用SQL Server datepart函数,如下所示:
GROUP BY
DatePart(yy, dbo.jm_invoice.some_date),
DatePart(mm, dbo.jm_invoice.some_date),
dbo.jm_job_type.job_type_desc
答案 1 :(得分:1)
“楼”到月(最有效的方式只获得月份+年)然后GROUP BY那个“
GROUP BY DATEADD(月,DATEDIFF(月,0,dbo.jm_invoice.YourDate),0)
所以它会是:
SELECT dbo.jm_job_type.job_type_desc,
DATEADD(month,DATEDIFF(month,0, dbo.jm_invoice.YourDate),0) AS MonthYear,
SUM(dbo.jm_invoice.invoice_amount) AS 'inv tot'
FROM dbo.jm_invoice INNER JOIN
dbo.jm_job ON dbo.jm_invoice.job_no = dbo.jm_job.job_no INNER JOIN
dbo.jm_job_type ON dbo.jm_job.job_type_no = dbo.jm_job_type.job_type_no
GROUP BY dbo.jm_job_type.job_type_desc,
DATEADD(month,DATEDIFF(month,0, dbo.jm_invoice.YourDate),0)
答案 2 :(得分:0)
将年份和月份字段添加到SELECT列表中,并将其添加到GROUP BY列表