我试图让这个查询返回每年的记录,而不是返回表中所有内容的单个记录。
SELECT
JAN
, FEB
, MAR
, APR
, MAY
, JUN
, JUL
, AUG
, SEP
, OCT
, NOV
, [DEC]
,JAN + FEB + MAR + APR + MAY + JUN + JUL + AUG + SEP + OCT + NOV + [DEC] AS TOTAL
FROM
(SELECT DISTINCT
(SELECT COUNT(*) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 1) AS JAN
, (SELECT COUNT(*) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 2) AS FEB
, (SELECT COUNT(*) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 3) AS MAR
, (SELECT COUNT(*) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 4) AS APR
, (SELECT COUNT(*) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 5) AS MAY
, (SELECT COUNT(*) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 6) AS JUN
, (SELECT COUNT(*) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 7) AS JUL
, (SELECT COUNT(*) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 8) AS AUG
, (SELECT COUNT(*) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 9) AS SEP
, (SELECT COUNT(*) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 10) AS OCT
, (SELECT COUNT(*) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 11) AS NOV
, (SELECT COUNT(*) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 12) AS [DEC]
FROM RET.tbl_Record)x
这会返回1条记录(我知道这是假设的)但我希望它能够返回每年的记录。我只是不确定如何做到这一点。
修改
SELECT
JAN
, FEB
, MAR
, APR
, MAY
, JUN
, JUL
, AUG
, SEP
, OCT
, NOV
, [DEC]
,JAN + FEB + MAR + APR + MAY + JUN + JUL + AUG + SEP + OCT + NOV + [DEC] AS TOTAL
FROM
(SELECT
(SELECT COUNT(dt_updated) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 1) AS JAN
, (SELECT COUNT(dt_updated) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 2) AS FEB
, (SELECT COUNT(dt_updated) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 3) AS MAR
, (SELECT COUNT(dt_updated) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 4) AS APR
, (SELECT COUNT(dt_updated) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 5) AS MAY
, (SELECT COUNT(dt_updated) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 6) AS JUN
, (SELECT COUNT(dt_updated) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 7) AS JUL
, (SELECT COUNT(dt_updated) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 8) AS AUG
, (SELECT COUNT(dt_updated) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 9) AS SEP
, (SELECT COUNT(dt_updated) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 10) AS OCT
, (SELECT COUNT(dt_updated) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 11) AS NOV
, (SELECT COUNT(dt_updated) FROM RET.tbl_Record WHERE MONTH(dt_updated) = 12) AS [DEC]
FROM RET.tbl_Record
GROUP BY YEAR(dt_updated))x
现在返回3条记录,这是我正在寻找的正确记录数量但是每条记录返回相同的值(它记录每条记录中的所有三年)
答案 0 :(得分:3)
例如,这将解决2011年,2012年和2013年的所有月份,重复性较低的代码。
DECLARE @years TABLE(y INT);
INSERT @years SELECT 2011 UNION ALL SELECT 2012 UNION ALL SELECT 2013;
;WITH m(m) AS
(
SELECT TOP (12) ROW_NUMBER() OVER (ORDER BY [object_id]) - 1
FROM sys.all_objects ORDER BY [object_id]
),
dates(y,m) AS
(
SELECT y.y, DATEADD(MONTH, m.m, DATEADD(YEAR, y.y - 1900, 0)) FROM m
CROSS JOIN @years AS y
),
s([YEAR],m,c) AS
(
SELECT d.y, LEFT(UPPER(DATENAME(MONTH, d.m)),3), COUNT(r.dt_updated)
FROM dates AS d LEFT OUTER JOIN RET.tbl_Record AS r
ON r.dt_updated >= d.m AND r.dt_updated < DATEADD(MONTH, 1, d.m)
GROUP BY d.y, DATENAME(MONTH, d.m)
),
n AS
(
SELECT * FROM s PIVOT (MAX(c) FOR m IN
(JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,[DEC])) AS p
)
SELECT *,Total = JAN+FEB+MAR+APR+MAY+JUN+JUL+AUG+SEP+OCT+NOV+[DEC] FROM n
ORDER BY [YEAR];
需要解决不同年份?没问题,只需将硬编码插入更改为@years
即可。
需要它是动态的吗?再一次,没问题;这将解决表中发现的每一年:
INSERT @years SELECT DISTINCT YEAR(dt_updated) FROM RET.tbl_Record;
需要它更具活力(例如表中最近三年):
INSERT @years SELECT DISTINCT TOP (3) YEAR(dt_updated)
FROM RET.tbl_Record ORDER BY YEAR(dt_updated) DESC;
平均而言,抱歉,你是独自一人(你在游戏中改变了对我的要求)。我的建议:在您的报告工具和/或表示层中执行此操作。
答案 1 :(得分:0)
或许:
GROUP BY YEAR(dt_updated)
答案 2 :(得分:0)
我希望有一种更有效的方法来完成任务,但看起来解决方案是:
SELECT
JAN.dtyear AS [YEAR]
, ISNULL(JAN.TOTAL, 0) AS JAN
, ISNULL(FEB.TOTAL, 0) AS FEB
, ISNULL(MAR.TOTAL, 0) AS MAR
, ISNULL(APR.TOTAL, 0) AS APR
, ISNULL(MAY.TOTAL, 0) AS MAY
, ISNULL(JUN.TOTAL, 0) AS JUN
, ISNULL(JUL.TOTAL, 0) AS JUL
, ISNULL(AUG.TOTAL, 0) AS AUG
, ISNULL(SEP.TOTAL, 0) AS SEP
, ISNULL(OCT.TOTAL, 0) AS OCT
, ISNULL(NOV.TOTAL, 0) AS NOV
, ISNULL([DEC].TOTAL, 0) AS [DEC]
, ISNULL(JAN.TOTAL, 0) + ISNULL(FEB.TOTAL, 0) + ISNULL(MAR.TOTAL, 0) + ISNULL(APR.TOTAL, 0) + ISNULL(MAY.TOTAL, 0) + ISNULL(JUN.TOTAL, 0) + ISNULL(JUL.TOTAL, 0) + ISNULL(AUG.TOTAL, 0) + ISNULL(SEP.TOTAL, 0) +
ISNULL(OCT.TOTAL, 0) + ISNULL(NOV.TOTAL, 0) + ISNULL([DEC].TOTAL, 0) AS TOTAL
FROM
(SELECT COUNT(RET_record_id) as TOTAL, YEAR(dt_updated) AS dtyear FROM RET.tbl_Record WHERE MONTH(dt_updated) = 1 GROUP BY YEAR(dt_updated)) JAN
LEFT OUTER JOIN
(SELECT COUNT(RET_record_id) as TOTAL, YEAR(dt_updated) AS dtyear FROM RET.tbl_Record WHERE MONTH(dt_updated) = 2 GROUP BY YEAR(dt_updated)) FEB
ON JAN.dtyear = FEB.dtyear
LEFT OUTER JOIN
(SELECT COUNT(RET_record_id) AS TOTAL, YEAR(dt_updated) AS dtyear FROM RET.tbl_Record WHERE MONTH(dt_updated) = 3 GROUP BY YEAR(dt_updated)) MAR
ON JAN.dtyear = MAR.dtyear AND FEB.dtyear = MAR.dtyear
LEFT OUTER JOIN
(SELECT COUNT(RET_record_id) AS TOTAL, YEAR(dt_updated) AS dtyear FROM RET.tbl_Record WHERE MONTH(dt_updated) = 4 GROUP BY YEAR(dt_updated)) APR
ON JAN.dtyear = APR.dtyear
LEFT OUTER JOIN
(SELECT COUNT(RET_record_id) AS TOTAL, YEAR(dt_updated) AS dtyear FROM RET.tbl_Record WHERE MONTH(dt_updated) = 5 GROUP BY YEAR(dt_updated)) MAY
ON JAN.dtyear = MAY.dtyear
LEFT OUTER JOIN
(SELECT COUNT(RET_record_id) AS TOTAL, YEAR(dt_updated) AS dtyear FROM RET.tbl_Record WHERE MONTH(dt_updated) = 6 GROUP BY YEAR(dt_updated)) JUN
ON JAN.dtyear = JUN.dtyear
LEFT OUTER JOIN
(SELECT COUNT(RET_record_id) AS TOTAL, YEAR(dt_updated) AS dtyear FROM RET.tbl_Record WHERE MONTH(dt_updated) = 7 GROUP BY YEAR(dt_updated)) JUL
ON JAN.dtyear = JUL.dtyear
LEFT OUTER JOIN
(SELECT COUNT(RET_record_id) AS TOTAL, YEAR(dt_updated) AS dtyear FROM RET.tbl_Record WHERE MONTH(dt_updated) = 8 GROUP BY YEAR(dt_updated)) AUG
ON JAN.dtyear = AUG.dtyear
LEFT OUTER JOIN
(SELECT COUNT(RET_record_id) AS TOTAL, YEAR(dt_updated) AS dtyear FROM RET.tbl_Record WHERE MONTH(dt_updated) = 9 GROUP BY YEAR(dt_updated)) SEP
ON JAN.dtyear = SEP.dtyear
LEFT OUTER JOIN
(SELECT COUNT(RET_record_id) AS TOTAL, YEAR(dt_updated) AS dtyear FROM RET.tbl_Record WHERE MONTH(dt_updated) = 10 GROUP BY YEAR(dt_updated)) OCT
ON JAN.dtyear = OCT.dtyear
LEFT OUTER JOIN
(SELECT COUNT(RET_record_id) AS TOTAL, YEAR(dt_updated) AS dtyear FROM RET.tbl_Record WHERE MONTH(dt_updated) = 11 GROUP BY YEAR(dt_updated)) NOV
ON JAN.dtyear = NOV.dtyear
LEFT OUTER JOIN
(SELECT COUNT(RET_record_id) AS TOTAL, YEAR(dt_updated) AS dtyear FROM RET.tbl_Record WHERE MONTH(dt_updated) = 12 GROUP BY YEAR(dt_updated)) [DEC]
ON JAN.dtyear = [DEC].dtyear