具有多个记录的相同 ID 到单行
Same ID with multiple records to single row
我有两个table。
Table这个样子
source
ID
Type_ID
Error_info
ABC
100
1
country column missing
ABC
100
2
conversion factor missing decimals
BCA
200
1
error value in height column
BCA
200
2
convertion factor should be 0.001
Table B长这样
source
ID
Type_1_ID
Error_info_1
Type_2_ID
Error_info_2
ABC
100
BCA
200
我想根据来源和 ID 列加入这两个 table。如果您看到 table A,您可以看到 ID 100 和 200 我有两条记录,但区别在于 Type_ID 列和 Error_info 列。我希望这两个记录像下面的单行一样 table.
source
ID
Type_1_ID
Error_info_1
Type_2_ID
Error_info_2
ABC
100
1
country column missing
2
conversion factor missing decimals
BCA
200
1
error value in height column
2
convertion factor should be 0.001
有没有办法实现这个。我尝试使用 case 语句,显然它没有用。任何建议肯定会帮助我。
如果您只想查看 table B 使用table A 中建议的输出,则使用数据透视查询:
SELECT
source,
ID,
1 AS Type_1_ID
MAX(CASE WHEN Type_ID = 1 THEN Error_info END) AS Error_info_1,
2 AS Type_2_ID,
MAX(CASE WHEN Type_ID = 2 THEN Error_info END) AS Error_info_2
FROM yourTable
GROUP BY
source,
ID;
您的数据
declare @a table(
source VARCHAR(100) NOT NULL,
ID INTEGER NOT NULL,
Type_ID INTEGER NOT NULL,
Error_info VARCHAR(max) NOT NULL
);
INSERT INTO @a(source, ID, Type_ID, Error_info)
VALUES
(
'ABC', 100, 1, 'country column missing'
),
(
'ABC', 100, 2, 'conversion factor missing decimals'
),
(
'BCA', 200, 1, 'error value in height column'
),
(
'BCA', 200, 2, 'convertion factor should be 0.001'
);
使用multiple pivot
如下
SELECT
source,
ID,
max(Type_1_ID) Type_1_ID,
max(Error_info_1) Error_info_1,
max(Type_2_ID) Type_2_ID,
max(Error_info_2) Error_info_2
FROM
(
SELECT
source,
ID,
Type_ID,
CONCAT('Error_info_', Type_ID) AS Col1,
CONCAT('Type_', Type_ID, '_ID') AS Col2,
Error_info
FROM
@a
) Src PIVOT (
MAX(Error_info) FOR Col1 IN ([Error_info_1], [Error_info_2])
) Pvt1 PIVOT (
max(Type_ID) FOR Col2 IN ([Type_1_ID], [Type_2_ID])
) AS pv2
group by
source,
ID
我有两个table。
Table这个样子
source | ID | Type_ID | Error_info |
---|---|---|---|
ABC | 100 | 1 | country column missing |
ABC | 100 | 2 | conversion factor missing decimals |
BCA | 200 | 1 | error value in height column |
BCA | 200 | 2 | convertion factor should be 0.001 |
Table B长这样
source | ID | Type_1_ID | Error_info_1 | Type_2_ID | Error_info_2 |
---|---|---|---|---|---|
ABC | 100 | ||||
BCA | 200 |
我想根据来源和 ID 列加入这两个 table。如果您看到 table A,您可以看到 ID 100 和 200 我有两条记录,但区别在于 Type_ID 列和 Error_info 列。我希望这两个记录像下面的单行一样 table.
source | ID | Type_1_ID | Error_info_1 | Type_2_ID | Error_info_2 |
---|---|---|---|---|---|
ABC | 100 | 1 | country column missing | 2 | conversion factor missing decimals |
BCA | 200 | 1 | error value in height column | 2 | convertion factor should be 0.001 |
有没有办法实现这个。我尝试使用 case 语句,显然它没有用。任何建议肯定会帮助我。
如果您只想查看 table B 使用table A 中建议的输出,则使用数据透视查询:
SELECT
source,
ID,
1 AS Type_1_ID
MAX(CASE WHEN Type_ID = 1 THEN Error_info END) AS Error_info_1,
2 AS Type_2_ID,
MAX(CASE WHEN Type_ID = 2 THEN Error_info END) AS Error_info_2
FROM yourTable
GROUP BY
source,
ID;
您的数据
declare @a table(
source VARCHAR(100) NOT NULL,
ID INTEGER NOT NULL,
Type_ID INTEGER NOT NULL,
Error_info VARCHAR(max) NOT NULL
);
INSERT INTO @a(source, ID, Type_ID, Error_info)
VALUES
(
'ABC', 100, 1, 'country column missing'
),
(
'ABC', 100, 2, 'conversion factor missing decimals'
),
(
'BCA', 200, 1, 'error value in height column'
),
(
'BCA', 200, 2, 'convertion factor should be 0.001'
);
使用multiple pivot
如下
SELECT
source,
ID,
max(Type_1_ID) Type_1_ID,
max(Error_info_1) Error_info_1,
max(Type_2_ID) Type_2_ID,
max(Error_info_2) Error_info_2
FROM
(
SELECT
source,
ID,
Type_ID,
CONCAT('Error_info_', Type_ID) AS Col1,
CONCAT('Type_', Type_ID, '_ID') AS Col2,
Error_info
FROM
@a
) Src PIVOT (
MAX(Error_info) FOR Col1 IN ([Error_info_1], [Error_info_2])
) Pvt1 PIVOT (
max(Type_ID) FOR Col2 IN ([Type_1_ID], [Type_2_ID])
) AS pv2
group by
source,
ID