如何在SQL中获得每月总计

时间:2014-04-10 20:09:33

标签: sql reporting-services ssrs-2008

我有

以下查询获取当天的总数:

SELECT *
FROM [database].[dbo].[table]

以下查询获取本周的总数:

SELECT * , DATENAME(WEEKDAY, Date)
FROM [database].[dbo].[table]
WHERE Date >= cast(dateadd(day,1-datepart(dw, getdate()), getdate()) as date)

如果只从月份的第一个月到月末开始,我怎么能写一个能给出月份总数的查询呢?

该表如下所示,它将继续为每一天添加自己:

enter image description here

所以明天它将是#4,日期将是2014-04-11,依此类推......

我需要查询在我的数据集中为我的SSRS报告调用哪些来创建条形图。

显示图像的原始查询是:

INSERT INTO mydatabase.dbo.TABLE
Select getdate(),
Count([Draw Date]),
(Select Count([Draw Date]) from mydatabase.dbo.[radClinicalInHouseLab]) AS [IN HOUSE],
(Select Count([Column 0]) from mydatabase.dbo.[radRadOrders]) AS [RAD EMR ORDERS],
(Select Count([Value]) from mydatabase.dbo.[radEMROrders]) AS [EMR ORDERS],
(Select Count([Draw Date]) from mydatabase.dbo.[radAccessLabOrders]) AS [ACCESSIONED LAB ORDERS],
(Select Count([Draw Date]) from mydatabase.dbo.[radAPSurgical]) AS [AP SURGICAL],
(Select Count([Draw Date]) from mydatabase.dbo.[radClinicalRefLab]) AS [REF LAB],
(Select Count([Column 0]) from mydatabase.dbo.[radRadResults]) AS [RAD RESULTED],
(Select Count([Draw Date]) from mydatabase.dbo.[radLabResults]) AS [LAB_RESULT], --added 4/4
(Select Count([Draw Date]) from mydatabase.dbo.[radResultedLabOrders]) AS [RESULTED LAB], --4/4
(Select Count([Draw Date]) from mydatabase.dbo.[radAPCytology]) AS [AP CYTOLOGY MOLECULAR],
(Select Count([Draw Date]) from mydatabase.dbo.[radAutoAccessRadOrders]) AS [RAD ACCESSIONED],
(Select Count([Draw Date]) from mydatabase.dbo.[radPendingLabResults]) AS [PENDING LAB], --4/4
((Select Count([Value]) from mydatabase.dbo.[radEMROrders]) - (Select Count([Draw Date]) from mydatabase.dbo.[radAccessLabOrders])) AS [PENDING FOR PROCESSING],
(Select Count([Draw Date]) from mydatabase.dbo.[radChemInHouse]) AS [CHEMISTRY],
(Select Count([Draw Date]) from mydatabase.dbo.[radBloodBank]) AS [BLOOD BANK],
(Select Count([Draw Date]) from mydatabase.dbo.[radHemotology]),
(Select Count([Draw Date]) from mydatabase.dbo.[radMicrobiology]),
(Select Count([Draw Date]) from mydatabase.dbo.[radSerology]),
(Select Count([Draw Date]) from mydatabase.dbo.[radUrinalysis])
FROM mydatabase.dbo.[radAPDERM]

出于某种原因,我无法获得以下查询以显示相同的日期格式:

DECLARE @mydate DATETIME
SELECT @mydate = GETDATE()
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@mydate)-1),@mydate),110) AS Date_Value,
'First Day of Current Month' AS Date_Type
UNION
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@mydate))),DATEADD(mm,1,@mydate)),110) ,
'Last Day of Current Month'

显示:

enter image description here

结果表:

enter image description here

APDERM  IN HOUSE    RAD EMR ORDERS  EMR ORDERS  ACCESSIONED LAB ORDERS  AP SURGICAL REF LAB RAD RESULTED    LAB_RESULT  RESULTED LAB    AP CYTOLOGY MOLECULAR   RAD ACCESSIONED PENDING LAB PENDING FOR PROCESSING  CHEMISTRY   BLOOD BANK  HEMOTOLOGY  MICROBIOLOGY    SEROLOGY    URINALYSIS
31  5122    35404   271265  7165    106 790 24736   61807   0   311 1122    2987    264100  4193    42  971 100 67  389

使用查询:

enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个:

DECLARE @DATE DATE
SET @DATE = GETDATE()
SELECT SUM(APDERM) as total
FROM [database].[dbo].[table]
WHERE [Date] >= CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, @DATE) as DATE) -- the first day of current month
  AND [Date] < CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, DATEADD(m, 1, @DATE)) as DATE) -- the first day of next month

您可以参考the post获取帮助UDF