以特定方式将 SQL 服务器中的行组合成列
Combine rows into columns in SQL Server in a particular way
我不知道如何描述我的问题,但我会尽力而为。我在 SQL Server 2014 中工作。我已经尽可能地简化了问题,因为我正在处理敏感信息。
我目前有一个查询 returns 来自 table 个测试答案的以下内容:
test_id
question_id
is_checked
1
1
TRUE
1
2
TRUE
1
3
FALSE
1
4
FALSE
2
1
FALSE
2
2
FALSE
2
3
FALSE
2
4
TRUE
3
1
FALSE
3
2
FALSE
3
3
FALSE
3
4
FALSE
每个测试只有 4 yes/no 个问题(而且这不太可能永远改变)。对于每个测试,可以将一个或多个问题标记为是。以上...
- 测试 1 的问题 1 和 2 为是,其余为否。
- 测试 2 第 4 题为是,其余为否。
- 测试 3 的所有问题都标记为否。
我希望我的结果如下所示:
test_id
question_1
question_2
question_3
question_4
1
TRUE
TRUE
FALSE
FALSE
2
FALSE
FALSE
FALSE
TRUE
3
FALSE
FALSE
FALSE
FALSE
我尝试使用 PIVOT
但没有成功。如有任何帮助,我们将不胜感激,我很乐意提供更多信息。
编辑:
我尝试使用 PIVOT
(请原谅我可能糟糕的格式):
SELECT *
FROM (
SELECT test_id, question_id, is_checked FROM example_table
) as sourcetable
pivot(
any(is_checked)
for question_id
in (question_1, question_2, question_3, question_4)
) as pivottable
根据以上内容填充示例table:
CREATE TABLE example_table (test_id int, question_id int, is_checked bit);
INSERT INTO example_table (test_id, question_id, is_checked)
VALUES
('1', '1', '1'),
('1', '2', '1'),
('1', '3', '0'),
('1', '4', '0'),
('2', '1', '0'),
('2', '2', '0'),
('2', '3', '0'),
('2', '4', '1'),
('3', '1', '0'),
('3', '2', '0'),
('3', '3', '0'),
('3', '4', '0');
最后,我的SQL服务器版本是SQLServer 2014,我之前把SQLServer 17放在上面,现在改正了。
最终编辑:
列is_checked
在我的系统中是bit
类型,但一定是有人设置了查询时输出TRUE
和FALSE
。在下面的答案中,我将 is_checked
替换为 CAST(is_checked AS INT)
并且有效。
请尝试以下解决方案。
SQL
-- DDL and sample data population, start
DECLARE @tbl TABLE (test_ID INT, question_id INT, is_checked VARCHAR(5));
INSERT INTO @tbl (test_ID, question_id, is_checked) VALUES
(1, 1, 'TRUE'),
(1, 2, 'TRUE'),
(1, 3, 'FALSE'),
(1, 4, 'FALSE'),
(2, 1, 'FALSE'),
(2, 2, 'FALSE'),
(2, 3, 'FALSE'),
(2, 4, 'TRUE'),
(3, 1, 'FALSE'),
(3, 2, 'FALSE'),
(3, 3, 'FALSE'),
(3, 4, 'FALSE');
-- DDL and sample data population, end
SELECT test_ID
, MAX(IIF(question_id = 1, is_checked, '')) AS question_1
, MAX(IIF(question_id = 2, is_checked, '')) AS question_2
, MAX(IIF(question_id = 3, is_checked, '')) AS question_3
, MAX(IIF(question_id = 4, is_checked, '')) AS question_4
FROM @tbl
GROUP BY test_ID
ORDER BY test_ID;
输出
+---------+------------+------------+------------+------------+
| test_ID | question_1 | question_2 | question_3 | question_4 |
+---------+------------+------------+------------+------------+
| 1 | TRUE | TRUE | FALSE | FALSE |
| 2 | FALSE | FALSE | FALSE | TRUE |
| 3 | FALSE | FALSE | FALSE | FALSE |
+---------+------------+------------+------------+------------+
我不知道如何描述我的问题,但我会尽力而为。我在 SQL Server 2014 中工作。我已经尽可能地简化了问题,因为我正在处理敏感信息。
我目前有一个查询 returns 来自 table 个测试答案的以下内容:
test_id | question_id | is_checked |
---|---|---|
1 | 1 | TRUE |
1 | 2 | TRUE |
1 | 3 | FALSE |
1 | 4 | FALSE |
2 | 1 | FALSE |
2 | 2 | FALSE |
2 | 3 | FALSE |
2 | 4 | TRUE |
3 | 1 | FALSE |
3 | 2 | FALSE |
3 | 3 | FALSE |
3 | 4 | FALSE |
每个测试只有 4 yes/no 个问题(而且这不太可能永远改变)。对于每个测试,可以将一个或多个问题标记为是。以上...
- 测试 1 的问题 1 和 2 为是,其余为否。
- 测试 2 第 4 题为是,其余为否。
- 测试 3 的所有问题都标记为否。
我希望我的结果如下所示:
test_id | question_1 | question_2 | question_3 | question_4 |
---|---|---|---|---|
1 | TRUE | TRUE | FALSE | FALSE |
2 | FALSE | FALSE | FALSE | TRUE |
3 | FALSE | FALSE | FALSE | FALSE |
我尝试使用 PIVOT
但没有成功。如有任何帮助,我们将不胜感激,我很乐意提供更多信息。
编辑:
我尝试使用 PIVOT
(请原谅我可能糟糕的格式):
SELECT *
FROM (
SELECT test_id, question_id, is_checked FROM example_table
) as sourcetable
pivot(
any(is_checked)
for question_id
in (question_1, question_2, question_3, question_4)
) as pivottable
根据以上内容填充示例table:
CREATE TABLE example_table (test_id int, question_id int, is_checked bit);
INSERT INTO example_table (test_id, question_id, is_checked)
VALUES
('1', '1', '1'),
('1', '2', '1'),
('1', '3', '0'),
('1', '4', '0'),
('2', '1', '0'),
('2', '2', '0'),
('2', '3', '0'),
('2', '4', '1'),
('3', '1', '0'),
('3', '2', '0'),
('3', '3', '0'),
('3', '4', '0');
最后,我的SQL服务器版本是SQLServer 2014,我之前把SQLServer 17放在上面,现在改正了。
最终编辑:
列is_checked
在我的系统中是bit
类型,但一定是有人设置了查询时输出TRUE
和FALSE
。在下面的答案中,我将 is_checked
替换为 CAST(is_checked AS INT)
并且有效。
请尝试以下解决方案。
SQL
-- DDL and sample data population, start
DECLARE @tbl TABLE (test_ID INT, question_id INT, is_checked VARCHAR(5));
INSERT INTO @tbl (test_ID, question_id, is_checked) VALUES
(1, 1, 'TRUE'),
(1, 2, 'TRUE'),
(1, 3, 'FALSE'),
(1, 4, 'FALSE'),
(2, 1, 'FALSE'),
(2, 2, 'FALSE'),
(2, 3, 'FALSE'),
(2, 4, 'TRUE'),
(3, 1, 'FALSE'),
(3, 2, 'FALSE'),
(3, 3, 'FALSE'),
(3, 4, 'FALSE');
-- DDL and sample data population, end
SELECT test_ID
, MAX(IIF(question_id = 1, is_checked, '')) AS question_1
, MAX(IIF(question_id = 2, is_checked, '')) AS question_2
, MAX(IIF(question_id = 3, is_checked, '')) AS question_3
, MAX(IIF(question_id = 4, is_checked, '')) AS question_4
FROM @tbl
GROUP BY test_ID
ORDER BY test_ID;
输出
+---------+------------+------------+------------+------------+
| test_ID | question_1 | question_2 | question_3 | question_4 |
+---------+------------+------------+------------+------------+
| 1 | TRUE | TRUE | FALSE | FALSE |
| 2 | FALSE | FALSE | FALSE | TRUE |
| 3 | FALSE | FALSE | FALSE | FALSE |
+---------+------------+------------+------------+------------+