根据列值选择多行

时间:2019-03-28 03:49:25

标签: sql sql-server tsql

我有一张这样的桌子:

+----------+----------+----------+----------+----------+
| COLI_KEY | COLI_DAY | COLI_WEE | COLI_MON | COLI_YEA |
+----------+----------+----------+----------+----------+
|        1 | 1        | NULL     |        1 | NULL     |
|        2 | NULL     | NULL     |        1 | 1        |
|        3 | 1        | 1        |        1 | 1        |
+----------+----------+----------+----------+----------+

我想做的是获取COLI_DAY,COLI_WEE,COLI_MON,COLI_YEA中每个值的每一行。因此,基本上我需要一个SELECT语句,该语句将为我提供所需的结果(每行中仅标记一列-其他列为空):

+----------+----------+----------+----------+----------+
| COLI_KEY | COLI_DAY | COLI_WEE | COLI_MON | COLI_YEA |
+----------+----------+----------+----------+----------+
|        1 | 1        | null     | null     | null     |
|        1 | null     | null     | 1        | null     |
|        2 | null     | null     | 1        | null     |
|        2 | null     | null     | null     | 1        |
|        3 | 1        | null     | null     | null     |
|        3 | null     | 1        | null     | null     |
|        3 | null     | null     | 1        | null     |
|        3 | null     | null     | null     | 1        |
+----------+----------+----------+----------+----------+

用于创建表的脚本:

declare @CACOLI table
(
    COLI_KEY INT,
    COLI_DAY CHAR(1),
    COLI_WEE CHAR(1),
    COLI_MON CHAR(1),
    COLI_YEA CHAR(1)
)

INSERT INTO @CACOLI (COLI_KEY, COLI_DAY, COLI_WEE, COLI_MON, COLI_YEA)
VALUES (1, '1', NULL, '1', NULL),
       (2, NULL, NULL, '1', '1'),
       (3, '1', '1', '1', '1')

3 个答案:

答案 0 :(得分:2)

这是一种简单明了的方法,在该方法中,我为每一列选择一个输出,同时将其他列保持为NULL,然后将它们全部合并。

SELECT
    COLI_KEY, COLI_DAY, NULL AS COLI_WEE, NULL AS COLI_MON, NULL AS COLI_YEA
WHERE 
    COLI_DAY = 1

UNION

SELECT 
    COLI_KEY, NULL AS COLI_DAY, COLI_WEE, NULL AS COLI_MON, NULL AS COLI_YEA
WHERE 
    COLI_WEE = 1

UNION

SELECT
    COLI_KEY, NULL AS COLI_DAY, NULL AS COLI_WEE, COLI_MON, NULL AS COLI_YEA
WHERE 
    COLI_MON = 1

UNION

SELECT
    COLI_KEY, NULL AS COLI_DAY, NULL AS COLI_WEE, NULL AS COLI_MON, COLI_YEA
WHERE 
    COLI_YEA = 1

答案 1 :(得分:2)

我们可以在此处尝试使用日历表,该表代表您可能在预期输出中看到的所有可能的元组。

WITH all_keys AS (
    SELECT 1 AS COLI_KEY UNION ALL
    SELECT 2 UNION ALL
    SELECT 3
),
data AS (
    SELECT 1 AS COLI_DAY, NULL AS COLI_WEE, NULL AS COLI_MON,
        NULL AS COLI_YEA UNION ALL
    SELECT NULL, 1, NULL, NULL UNION ALL
    SELECT NULL, NULL, 1, NULL UNION ALL
    SELECT NULL, NULL, NULL, 1
)

SELECT
    a.COLI_KEY,
    d.COLI_DAY,
    d.COLI_WEE,
    d.COLI_MON,
    d.COLI_YEA
FROM all_keys a
CROSS JOIN data d
INNER JOIN yourTable t
    ON t.COLI_KEY = a.COLI_KEY AND
       (t.COLI_DAY = d.COLI_DAY OR
        t.COLI_WEE = d.COLI_WEE OR
        t.COLI_MON = d.COLI_MON OR
        t.COLI_YEA = d.COLI_YEA);

Demo

答案 2 :(得分:1)

您还可以使用flash<% @posts.each do |post| %> <p> <%= post.content %> </p> <%= link_to "Like", like_path(@user, post_id: post), remote: true, method: :post %> <% end %> 来实现此目标,如下所示。

UNPIVOT

Online Demo

注意:将PIVOT更改为您的实际表名。