如何合并来自同一个 table 的两行数据?
How to combine two rows of data from the same table?
我有一个 table 文件如下:
document_table
- document_id
- employee_id
- 类型(值可以是"transcript"或"testimonial")
- 状态(值只能是"ready to print"
示例数据如下。
doc_id employee_id type status
--------------------------------------------------
1 S1234 transcript ready to print
2 S1234 testimonial ready to print
3 S2345 transcript ready to print
我希望结果 table 如下所示。
Result_table:
- employee_id
- 成绩单
- 见证
我想编写一个 sql 查询,根据 employee_id 并根据文档类型是成绩单还是证明书
将单个文档记录合并为一个
doc_id transcript testimonial
--------------------------------
S1234 TRUE TRUE
S2345 TRUE FALSE
我正在使用 MS Access 2010。
我该如何实现?
使用 SQL
,一种方法是在内部查询中使用 union
将 transcript
和 testimonial
数据分隔在单独的列中,然后使用 max
和 case
在外部查询中获得如下所示的所需结果。
SELECT
employee_id,
CASE
WHEN MAX(transcript) = 'transcript' THEN 'TRUE'
ELSE 'FALSE'
END AS transcript,
CASE
WHEN MAX(testimonial) = 'testimonial' THEN 'TRUE'
ELSE 'FALSE'
END AS testimonial
FROM (SELECT
employee_id,
type AS transcript,
'' AS testimonial
FROM t1
WHERE type = 'transcript'
UNION ALL
SELECT
employee_id,
'' AS transcript,
type AS testimonial
FROM t1
WHERE type = 'testimonial') t
GROUP BY employee_id;
结果:
employee_id transcript testimonial
---------------------------------------
S1234 TRUE TRUE
S2345 TRUE FALSE
你可以查看演示here
最简单的方法,虽然只有 return TRUE
有数据的地方,FALSE
只是空字段:
TRANSFORM First("TRUE") AS S
SELECT document.employee_id
FROM document
GROUP BY document.employee_id
PIVOT document.type In ("testimonial","transcript");
不要认为 Access SQL 识别 CASE
结构,所以 zarruq 的替代版本答案:
SELECT
employee_id,
IIf(Max(transcript)="transcript","TRUE","FALSE") AS tran,
IIf(Max(testimonial)="testimonial", "TRUE","FALSE") AS test
FROM (SELECT
employee_id,
type AS transcript,
'' AS testimonial
FROM document
WHERE type = 'transcript'
UNION ALL
SELECT
employee_id,
'' AS transcript,
type AS testimonial
FROM document
WHERE type = 'testimonial')
GROUP BY employee_id;
我有一个 table 文件如下:
document_table
- document_id
- employee_id
- 类型(值可以是"transcript"或"testimonial")
- 状态(值只能是"ready to print"
示例数据如下。
doc_id employee_id type status
--------------------------------------------------
1 S1234 transcript ready to print
2 S1234 testimonial ready to print
3 S2345 transcript ready to print
我希望结果 table 如下所示。
Result_table:
- employee_id
- 成绩单
- 见证
我想编写一个 sql 查询,根据 employee_id 并根据文档类型是成绩单还是证明书
将单个文档记录合并为一个doc_id transcript testimonial
--------------------------------
S1234 TRUE TRUE
S2345 TRUE FALSE
我正在使用 MS Access 2010。 我该如何实现?
使用 SQL
,一种方法是在内部查询中使用 union
将 transcript
和 testimonial
数据分隔在单独的列中,然后使用 max
和 case
在外部查询中获得如下所示的所需结果。
SELECT
employee_id,
CASE
WHEN MAX(transcript) = 'transcript' THEN 'TRUE'
ELSE 'FALSE'
END AS transcript,
CASE
WHEN MAX(testimonial) = 'testimonial' THEN 'TRUE'
ELSE 'FALSE'
END AS testimonial
FROM (SELECT
employee_id,
type AS transcript,
'' AS testimonial
FROM t1
WHERE type = 'transcript'
UNION ALL
SELECT
employee_id,
'' AS transcript,
type AS testimonial
FROM t1
WHERE type = 'testimonial') t
GROUP BY employee_id;
结果:
employee_id transcript testimonial
---------------------------------------
S1234 TRUE TRUE
S2345 TRUE FALSE
你可以查看演示here
最简单的方法,虽然只有 return TRUE
有数据的地方,FALSE
只是空字段:
TRANSFORM First("TRUE") AS S
SELECT document.employee_id
FROM document
GROUP BY document.employee_id
PIVOT document.type In ("testimonial","transcript");
不要认为 Access SQL 识别 CASE
结构,所以 zarruq 的替代版本答案:
SELECT
employee_id,
IIf(Max(transcript)="transcript","TRUE","FALSE") AS tran,
IIf(Max(testimonial)="testimonial", "TRUE","FALSE") AS test
FROM (SELECT
employee_id,
type AS transcript,
'' AS testimonial
FROM document
WHERE type = 'transcript'
UNION ALL
SELECT
employee_id,
'' AS transcript,
type AS testimonial
FROM document
WHERE type = 'testimonial')
GROUP BY employee_id;