SELECT Count(*) AS MonthTotal
FROM CRMProjects
WHERE CreatedDate between '01 May 2016' and '31 May 2016'
SELECT Count(*) AS YearTotal
FROM CRMProjects
WHERE CreatedDate between '01 Jan 2016' and '31 Dec 2016'
SELECT Count(*) AS MonthNew
FROM CRMProjects
WHERE CreatedDate between '01 May 2016' and '31 May 2016'
AND SystemType = 'O'
SELECT Count(*) AS YearClosed
FROM CRMProjects
WHERE CreatedDate between '01 Jan 2016' and '31 Dec 2016'
AND SystemType = 'C'
它只填充表中的月份,它不会填充其他部分,因为Visual Studio不允许对一个数据集使用多个select语句。
答案 0 :(得分:0)
我会建议这样的事情:
SELECT 'Month Total' AS Label, Count(*) AS Value
FROM CRMProjects
WHERE CreatedDate between '01 May 2016' and '31 May 2016'
UNION ALL
SELECT 'Year Total' AS Label, Count(*) AS Value
FROM CRMProjects
WHERE CreatedDate between '01 Jan 2016' and '31 Dec 2016'
UNION ALL
SELECT 'Month New' AS Label, Count(*) AS Value
FROM CRMProjects
WHERE CreatedDate between '01 May 2016' and '31 May 2016'
AND SystemType = 'O'
UNION ALL
SELECT 'Year Closed' AS Label, Count(*) AS Value
FROM CRMProjects
WHERE CreatedDate between '01 Jan 2016' and '31 Dec 2016'
AND SystemType = 'C'
答案 1 :(得分:0)
您可以使用SUM(CASE)
作为SUMIF
的一种......
SELECT
SUM(CASE WHEN CreatedDate between '01 May 2016' and '31 May 2016' THEN 1 END) AS MonthTotal,
SUM(1) AS YearTotal,
SUM(CASE WHEN CreatedDate between '01 May 2016' and '31 May 2016' AND SystemType = 'O' THEN 1 END) AS MonthNew,
SUM(CASE WHEN SystemType = 'C' THEN 1 END) AS YearClosed
FROM
CRMProjects
WHERE
CreatedDate between '01 Jan 2016' and '31 Dec 2016'
这确保您只扫描一次表格的相关部分,一次性汇总结果。
答案 2 :(得分:0)
将每个计数选为自己的列:
Select
(SELECT Count(*)
FROM CRMProjects
WHERE CreatedDate between '01 May 2016' and '31 May 2016') AS MonthTotal,
(SELECT Count(*)
FROM CRMProjects
WHERE CreatedDate between '01 Jan 2016' and '31 Dec 2016') AS YearTotal,
(SELECT Count(*)
FROM CRMProjects
WHERE CreatedDate between '01 May 2016' and '31 May 2016'
AND SystemType = 'O') AS MonthNew,
(SELECT Count(*)
FROM CRMProjects
WHERE CreatedDate between '01 Jan 2016' and '31 Dec 2016'
AND SystemType = 'C') AS YearClosed