早上好
我有这样的表
例如
此表名为“note”
--no-interaction
我想展示
id------alloc_code------Note_type-------Note
1 AA ss hello
2 AA sc good
3 BB ss something
4 BB sc anything
我怎么能这样编码?
答案 0 :(得分:1)
一种方法是条件聚合:
select alloc_code,
'SS' as Note_Type, max(case when Note_Type = 'ss' then Note end) as ss_note,
'SC' as Note_Type, max(case when Note_Type = 'sc' then Note end) as sc_note
from note n
group by alloc_code;
答案 1 :(得分:1)
您可以在笔记表上执行SELF JOIN
并使用WHERE
子句过滤note_types ss和sc:
SELECT
n1.alloc_code,
'SS' AS Note_type,
n1.Note,
'SC' AS Note_type,
n2.Note
FROM note AS n1
LEFT JOIN note AS n2 ON n1.alloc_code=n2.alloc_code
WHERE n1.Note_type='ss'
AND n2.Note_type='sc';