ORACLE SQL查询技巧。如何显示一行? SQL

时间:2017-07-19 14:21:08

标签: sql oracle

早上好

我有这样的表

例如

此表名为“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

我怎么能这样编码?

2 个答案:

答案 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';