SQL pivot列null(包含代码/结果)

时间:2016-01-03 20:53:05

标签: sql sql-server

我一直在使用SQL Fiddle,并且非常接近完成 - 我只想获得一些帮助来获取列数据(我认为列varchar标题有两个单词(例如:High Value)抛出它关闭。

这是我挣扎的问题:

SELECT Velocity
  , [Very High]
  , High
  , [Medium]
  , [Low]
  , [Very Low]
  , [Total]
FROM (
  SELECT CASE 
      WHEN GROUPING(velocity) = 0 THEN CAST(velocity AS CHAR(7))
      ELSE 'Total'
    END AS velocity
    , CASE 
      WHEN GROUPING(volume) = 0 THEN CAST(volume AS CHAR(7))
      ELSE 'Total'
    END AS volume
    , SUM(Sales) AS Sales
  FROM test1
  GROUP BY velocity, volume
  WITH CUBE
  ) AS s
PIVOT(SUM(Sales) FOR volume IN ([Very High], [High], [Medium], [Low], [Very Low], [Total])) AS p;

我希望看到这个结果: Result wanted here:

添加SQL Fiddle

1 个答案:

答案 0 :(得分:1)

我在SQL查询中发现了错误。您需要更改CAST(volume AS CHAR(7)) to CAST(volume AS CHAR(12))否则您的内部临时表值就像"非常嗨"和"非常"。