我想要一个查询,以在其自己的列中返回3个月,6个月和12个月的结果。
3 | 6 | 12
V1| V2| V3
我有一个查询,它将给我以下任何一个结果:
获得一个值的代码:
SELECT SUM(CONVERT(bigint,R.FileSize)) / (1024*1024) AS '3'
FROM Revisions R
JOIN Documents D ON D.DocumentID = R.DocumentID
WHERE R.Date BETWEEN dateadd(month, -3, GETDATE()) AND getdate()
结果:
3
205
我试图嵌入SQL查询并将其合并。
SELECT b.3, SUM(CONVERT(bigint,R.FileSize)) / (1024*1024) AS '6'
FROM Revisions R2
JOIN Documents D2 ON D2.DocumentID = R2.DocumentID
FULL OUTER JOIN b ON b.3 = 6
(SELECT SUM(CONVERT(bigint,R.FileSize)) / (1024*1024) AS '3'
FROM Revisions R
JOIN Documents D ON D.DocumentID = R.DocumentID
WHERE R.Date BETWEEN dateadd(month, -3, GETDATE()) AND getdate()) b
WHERE R2.Date BETWEEN dateadd(month, -6, GETDATE()) AND getdate();
答案 0 :(得分:1)
我认为您需要条件聚合:
SELECT SUM(CASE WHEN R.Date BETWEEN dateadd(month, -3, GETDATE()) AND getdate()
THEN CONVERT(bigint, R.FileSize)) / (1024*1024)
END) AS filesize_3,
SUM(CASE WHEN R.Date BETWEEN dateadd(month, -6, GETDATE()) AND getdate()
THEN CONVERT(bigint, R.FileSize)) / (1024*1024)
END) AS filesize_6,
SUM(CASE WHEN R.Date BETWEEN dateadd(month, -9, GETDATE()) AND getdate()
THEN CONVERT(bigint, R.FileSize)) / (1024*1024)
END) AS filesize_9
FROM Revisions R JOIN
Documents D
ON D.DocumentID = R.DocumentID