我是SQL新手,我正在努力完成oracle数据库数据的特定输出。
我需要将一些行转换为列,并将其与我需要的一些选定列放在一起。 问题是我必须在很多表格上完成这项任务,而我却找不到办法。我猜你作为专业人士必须每天处理这样的事情,所以也许你可以帮我解决这个问题。这会让我很开心。
这是我在db中的表:
PRODUCTTXTID PRODUCTID COMID TXTNUMMER LANG TXT
0740000000005D 0740000000002P 254 1 DE German text1 of this article
0740000000005I 0740000000002P 254 1 IT Italian text1 of this article
0740000000005F 07400000000EPF 254 2 DE German text2 of this article
07400000000F03 074000000007L8 254 1 DE German text1 of this article
0740000000005D 074000000007L8 254 2 IT Italian text2 of this article
07400000000F05 074000000007L9 254 1 DE German text1 of this article
0740000000001S 0740000000000X 254 1 DE German text1 of this article
0740000000001T 0740000000000X 254 1 IT Italian text1 of this article
0740000000001U 0740000000000Y 254 2 DE German text2 of this article
0740000000001V 0740000000000Y 254 1 IT Italian text1 of this article
07400000000F07 074000000007LA 254 1 DE German text1 of this article
我需要像这样把它拿出来:
PRODUCTID TXT1DE TXT1IT TXT2DE TXT2IT
0740000000002P German text1 of this article Italian text1 of this article German text2 of this article Italian text2 of this article
0740000000002A German text1 of this article Italian text1 of this article German text2 of this article Italian text2 of this article
07400000000EPF German text1 of this article Italian text1 of this article German text2 of this article Italian text2 of this article
074000000007L7 German text1 of this article Italian text1 of this article German text2 of this article Italian text2 of this article
074000000007L8 German text1 of this article Italian text1 of this article German text2 of this article Italian text2 of this article
074000000007L9 German text1 of this article Italian text1 of this article German text2 of this article Italian text2 of this article
0740000000000E German text1 of this article Italian text1 of this article German text2 of this article Italian text2 of this article
0740000000000X German text1 of this article Italian text1 of this article German text2 of this article Italian text2 of this article
0740000000000Y German text1 of this article Italian text1 of this article German text2 of this article Italian text2 of this article
0740000000000A German text1 of this article Italian text1 of this article German text2 of this article Italian text2 of this article
074000000007LA German text1 of this article Italian text1 of this article German text2 of this article Italian text2 of this article
最好的问候, 亚光
答案 0 :(得分:0)
如果您在TXTNUMMER和LANG中限制了不同的值,并且您的Oracle版本为11g或更新版本,那么简单的PIVOT应该可以满足您的需求。您只需要在PIVOT的IN子句中对可能的值进行硬编码。例如,关于样本数据:
WITH TST AS
(SELECT '0740000000002P' PRODUCTID, 254 COMID, 1 TXTNUMMER, 'DE' LANG, 'German text1 of this article' TXT FROM DUAL UNION ALL
SELECT '0740000000002P' PRODUCTID, 254 COMID, 1 TXTNUMMER, 'IT' LANG, 'Italian text1 of this article' TXT FROM DUAL UNION ALL
SELECT '07400000000EPF' PRODUCTID, 254 COMID, 2 TXTNUMMER, 'DE' LANG, 'German text2 of this article' TXT FROM DUAL UNION ALL
SELECT '074000000007L8' PRODUCTID, 254 COMID, 1 TXTNUMMER, 'DE' LANG, 'German text1 of this article' TXT FROM DUAL UNION ALL
SELECT '074000000007L8' PRODUCTID, 254 COMID, 2 TXTNUMMER, 'IT' LANG, 'Italian text2 of this article' TXT FROM DUAL)
SELECT * FROM
(SELECT PRODUCTID, 'TXT' || TXTNUMMER || LANG LANGID, TXT FROM TST)
PIVOT
(MAX(TXT) FOR LANGID IN ('TXT1DE','TXT1IT','TXT2DE','TXT2IT'))
否则,如果未预定义语言数,则没有通用解决方案,因为Oracle不喜欢结果集中未知数量的列。但是,一些pl / sql函数,它将生成临时表并使用动态sql填充数据应该可以解决问题。