首先,我要非常感谢所有参与这个论坛的人。在 [INSERT PROJECT NAME HERE] 到期前一天凌晨 3 点,大量的知识和示例成为了一个了不起的资源,我走到了墙边……哈哈。
我创建了一个简单的查询,作为按月和季度划分的数据的常规报告运行,它会提取正确的值。但是我在订购时遇到了麻烦。这就是我希望查询结果返回的内容:
Month DM TM FI GR NM Total
JAN 0 0 50000 0 0 50000
FEB 0 0 535000 0 620043.77 1255043.77
MAR 0 0 614146 5611 3016237.91 3637788.91
Q1 0 0 1199146 5611 3636281.68 4942832.68
APR 0 0 1230059 6395 1278525.79 2578589.79
MAY 0 45796 101160 15473.18 1357877.7 1932405.88
JUN 213716.34 141050 746302.28 25007.68 2552277.35 3963233.35
Q2 213716.34 186846 2077521.28 46875.86 5188680.84 8474229.02
JUL 258362.92 49541 359178.5 19674.33 803987.99 1928917.74
AUG 62750.35 9159 151561.28 15020.88 1248746.28 1533091.93
SEP 262246.37 136064 198365.99 37397.73 3271355.71 4017822.94
Q3 583359.64 194764 709105.77 72092.94 5324089.98 7479832.61
OCT 198565.1 189847 626951.11 29517 978947.85 2048681.2
NOV 22391 53652 155140.84 11444.75 470482.95 714747.68
Q4 220956.1 243499 782091.95 40961.75 1449430.8 2763428.88
但是,这是我卡住的地方:
Month DM TM FI GR NM Total
APR 0.00 0.00 1230059.00 6395.00 1275636.61 2575700.61
AUG 62750.35 9159.00 151561.28 15020.88 1248286.28 1532631.93
FEB 0.00 0.00 535000.00 0.00 587337.00 1222337.00
JAN 0.00 0.00 50000.00 0.00 0.00 50000.00
JUL 258362.92 49541.00 359178.50 19674.33 803417.96 1933347.71
JUN 213716.34 141050.00 746302.28 25007.68 2551383.23 3962339.23
MAR 0.00 0.00 614146.00 5611.00 2920193.41 3541744.41
MAY 0.00 45796.00 101160.00 15473.18 1356653.55 1931181.73
NOV 26213.00 61683.00 130270.90 13099.75 496460.05 729362.84
OCT 198565.10 189847.00 626951.11 29517.00 978797.85 2043531.20
Q1 0.00 0.00 1199146.00 5611.00 3507530.41 4814081.41
Q2 213716.34 186846.00 2077521.28 46875.86 5183673.39 8469221.57
Q3 583359.64 194764.00 709105.77 72092.94 5322894.95 7483637.58
Q4 224778.10 251530.00 757222.01 42616.75 1475257.90 2772894.04
SEP 262246.37 136064.00 198365.99 37397.73 3271190.71 4017657.94
这是让我按字母顺序排列的结果的查询:
SELECT
CASE DATEPART(mm, dtDate)
WHEN 01 THEN 'JAN' WHEN 02 THEN 'FEB' WHEN 03 THEN 'MAR'
WHEN 04 THEN 'APR' WHEN 05 THEN 'MAY' WHEN 06 THEN 'JUN'
WHEN 07 THEN 'JUL' WHEN 08 THEN 'AUG' WHEN 09 THEN 'SEP'
WHEN 10 THEN 'OCT' WHEN 11 THEN 'NOV' WHEN 12 THEN 'DEC'
END
AS 'Month',
SUM(CASE WHEN sAccount = 'DM' AND sChannel = 'DM' THEN mAmount
ELSE 0 END) AS 'DM',
SUM(CASE WHEN sAccount = 'DM' AND sChannel = 'TM' THEN mAmount
ELSE 0 END) AS 'TM',
SUM(CASE WHEN sAccount = 'FI' AND sChannel = 'FI' THEN mAmount
ELSE 0 END) AS 'FI',
SUM(CASE WHEN sAccount = 'FI' AND sChannel = 'GR' THEN mAmount
ELSE 0 END) AS 'GR',
SUM(CASE WHEN sAccount = 'NM' THEN mAmount
ELSE 0 END) AS 'NM',
SUM(mAmount) AS 'Total'
FROM Contrib
WHERE iDeleted = '0'
GROUP BY DATEPART(mm, dtDate)
UNION
SELECT
CASE DATEPART(qq, dtDate)
WHEN 01 THEN 'Q1'
WHEN 02 THEN 'Q2'
WHEN 03 THEN 'Q3'
WHEN 04 THEN 'Q4'
END
AS 'Month',
SUM(CASE WHEN sAccount = 'DM' AND sChannel = 'DM' THEN mAmount
ELSE 0 END) AS 'DM',
SUM(CASE WHEN sAccount = 'DM' AND sChannel = 'TM' THEN mAmount
ELSE 0 END) AS 'M',
SUM(CASE WHEN sAccount = 'FI' AND sChannel = 'FI' THEN mAmount
ELSE 0 END) AS 'FI',
SUM(CASE WHEN sAccount = 'FI' AND sChannel = 'GR' THEN mAmount
ELSE 0 END) AS 'GR',
SUM(CASE WHEN sAccount = 'NM' THEN mAmount
ELSE 0 END) AS 'NM',
SUM(mAmount) AS 'Total'
FROM Contrib
WHERE iDeleted = '0'
GROUP BY DATEPART(qq, dtDate)
我尝试了多种方法将 order by 子句添加到查询中,但都没有成功(通常得到 Msg 104,Level 16...“如果语句包含 UNION,则 ORDER BY 项必须出现在选择列表中, INTERSECT 或 EXCEPT 运算符”)。我怀疑我的问题是由于联合和 2 个不同的 datepart 函数(月与季度)造成的。
我仍然很年轻,所以请告诉我是否有更有效的方法来组织这个查询,或者如果你认为你知道我可能会遇到我的障碍。.
非常感谢!
拍