我有以下代码生成类似于我的数据。这里的发布(PivotWithoutAggregateFunction)表明,对于非数字值,使用CASE语句而不是PIVOT更好。如果这不是真的那么我想现在是时候解决它了!
DECLARE @QA1 TABLE (SID varchar(7), FormID varchar(max), DateExam date, Present varchar(3))
INSERT INTO @QA1 VALUES(1, 'Form1', '20110101', 'Yes')
INSERT INTO @QA1 VALUES(2, 'Form1', '20110201', 'Yes')
INSERT INTO @QA1 VALUES(3, 'Form1', '20110301', 'Yes')
INSERT INTO @QA1 VALUES(4, 'Form1', '20110401', 'Yes')
INSERT INTO @QA1 VALUES(5, 'Form1', '20110122', 'Yes')
INSERT INTO @QA1 VALUES(1, 'Form2', '20110222', 'Yes')
INSERT INTO @QA1 VALUES(2, 'Form2', '20110322', 'Yes')
INSERT INTO @QA1 VALUES(3, 'Form2', '20110422', 'Yes')
INSERT INTO @QA1 VALUES(1, 'Form3', '20110128', 'Yes')
INSERT INTO @QA1 VALUES(1, 'Form4', '20110228', 'Yes')
INSERT INTO @QA1 VALUES(5, 'Form5', '20110328', 'Yes')
INSERT INTO @QA1 VALUES(5, 'Form6', '20111228', 'Yes')
INSERT INTO @QA1 VALUES(4, 'Form2', '20111228', 'Yes')
SELECT SID,
MIN(CASE FormID WHEN 'Form1' THEN Present END) AS 'First',
MIN(CASE FormID WHEN 'Form2' THEN Present END) AS 'Second',
MIN(CASE FormID WHEN 'Form3' THEN Present END) AS 'Third',
MIN(CASE FormID WHEN 'Form4' THEN Present END) AS 'Fourth',
MIN(CASE FormID WHEN 'Form5' THEN Present END) AS 'Fifth',
MIN(CASE FormID WHEN 'Form6' THEN Present END) AS 'Sixth'
FROM @QA1
GROUP BY SID
ORDER BY SID
但是,行/列中的输出为NULL,它没有任何形式,而正确的形式对我来说更难阅读。
SID First Second Third Fourth Fifth Sixth
1 Yes Yes Yes Yes NULL NULL
2 Yes Yes NULL NULL NULL NULL
3 Yes Yes NULL NULL NULL NULL
4 Yes Yes NULL NULL NULL NULL
5 Yes NULL NULL NULL Yes Yes
我如何改变我的CASE,以便我得到任何东西(即'')或至少'不'?
答案 0 :(得分:2)
您应该能够将COALESCE包裹在有问题的MIN周围,例如:
COALESCE(MIN(CASE FormID WHEN 'Form1' THEN Present END), 'No') AS 'First',
我不确定SQL Server会对它有多高兴,但那是非常标准的SQL。
在之后应用NULL调整 MIN可能是一个更好的调用,而不是试图选择一个安全值放入MIN。
答案 1 :(得分:1)
SELECT SID,
isnull(MIN(CASE FormID WHEN 'Form1' THEN Present END),'') AS 'First',
isnull(MIN(CASE FormID WHEN 'Form2' THEN Present END),'') AS 'Second',
isnull(MIN(CASE FormID WHEN 'Form3' THEN Present END),'') AS 'Third',
isnull(MIN(CASE FormID WHEN 'Form4' THEN Present END),'') AS 'Fourth',
isnull(MIN(CASE FormID WHEN 'Form5' THEN Present END),'') AS 'Fifth',
isnull(MIN(CASE FormID WHEN 'Form6' THEN Present END),'') AS 'Sixth'
或
SELECT SID,
isnull(MIN(CASE FormID WHEN 'Form1' THEN Present END),'No') AS 'First',
isnull(MIN(CASE FormID WHEN 'Form2' THEN Present END),'No') AS 'Second',
isnull(MIN(CASE FormID WHEN 'Form3' THEN Present END),'No') AS 'Third',
isnull(MIN(CASE FormID WHEN 'Form4' THEN Present END),'No') AS 'Fourth',
isnull(MIN(CASE FormID WHEN 'Form5' THEN Present END),'No') AS 'Fifth',
isnull(MIN(CASE FormID WHEN 'Form6' THEN Present END),'No') AS 'Sixth'
答案 2 :(得分:0)
这是对PIVOT操作员的完美查询。
SELECT
SID,
COALESCE([Form1],'No') AS [First],
COALESCE([Form2],'No') AS [Second],
COALESCE([Form3],'No') AS [Third],
COALESCE([Form4],'No') AS [Fourth],
COALESCE([Form5],'No') AS [Fifth],
COALESCE([Form6],'No') AS [Sixth]
FROM (
SELECT SID, FormID, Present FROM @QA1
) S
PIVOT (
MIN(Present)
FOR FormID IN ([Form1],[Form2],[Form3],[Form4],[Form5],[Form6])
) AS P
ORDER BY SID;