我有一张表格,其中包含以下数据:
DECLARE @Plan_Summary TABLE (Col1 INT,Col2 INT,Col3 INT)
INSERT INTO @Plan_Summary VALUES (20,10,10)
INSERT INTO @Plan_Summary VALUES (20,10,10)
INSERT INTO @Plan_Summary VALUES (20,10,10)
INSERT INTO @Plan_Summary VALUES (8,10,10)
INSERT INTO @Plan_Summary VALUES (8,10,10)
INSERT INTO @Plan_Summary VALUES (8,10,10)
INSERT INTO @Plan_Summary VALUES (8,10,10)
INSERT INTO @Plan_Summary VALUES (4,10,10)
INSERT INTO @Plan_Summary VALUES (4,10,10)
INSERT INTO @Plan_Summary VALUES (4,10,10)
INSERT INTO @Plan_Summary VALUES (4,10,10)
INSERT INTO @Plan_Summary VALUES (4,10,10)
INSERT INTO @Plan_Summary VALUES (-10,10,10)
INSERT INTO @Plan_Summary VALUES (-20,10,10)
INSERT INTO @Plan_Summary VALUES (-2,10,10)
INSERT INTO @Plan_Summary VALUES (-7,10,10)
INSERT INTO @Plan_Summary VALUES (-15,10,10)
我必须将这些值绘制成具有以下要求的条形图:
对于Col1
对于Col2和col3总是红色。
我在数据集中使用了以下代码:
SELECT SUM(CASE WHEN Col1 BETWEEN 15 AND 25 THEN 1 ELSE 0 END) AS Green,
SUM(CASE WHEN Col1 BETWEEN 6 AND 14 THEN 1 ELSE 0 END) AS Blue,
SUM(CASE WHEN Col1 BETWEEN 0 AND 5 THEN 1 ELSE 0 END) AS Yellow,
SUM(CASE WHEN Col1 < 0 THEN 1 ELSE 0 END) AS Red,
COUNT(Col2) AS Col2,COUNT(Col3) AS Col3
INTO #TMP
FROM @Plan_Summary
SELECT Val,Col
FROM (SELECT Green,Blue,Yellow,Red,Col2,Col3
FROM #TMP) CSP
UNPIVOT (VAL FOR COL IN(Green,Blue,Yellow,Red,Col2,Col3)) AS CSP
我在SSRS中添加了堆积柱形图类型 - 但问题是如何将所有绿色,蓝色,黄色,红色列合并到一个堆叠列中并确保它们获得正确的颜色?
感谢。
答案 0 :(得分:0)
在这种情况下,请不要UNPIVOT
您的数据。就这样做:
SELECT SUM(CASE WHEN Col1 BETWEEN 15 AND 25 THEN 1 ELSE 0 END) AS Green,
SUM(CASE WHEN Col1 BETWEEN 6 AND 14 THEN 1 ELSE 0 END) AS Blue,
SUM(CASE WHEN Col1 BETWEEN 0 AND 5 THEN 1 ELSE 0 END) AS Yellow,
SUM(CASE WHEN Col1 < 0 THEN 1 ELSE 0 END) AS Red,
COUNT(Col2) AS Col2,COUNT(Col3) AS Col3
FROM @Plan_Summary
然后,将每列添加为图表中的值。见下文: