使用数据透视表通过将某些行保持不变将行转换为列

时间:2018-07-20 11:25:56

标签: oracle pivot

我具有如下所示的表数据Table Data,并且我希望数据应表示为

Combined data from another table,我想在合并数据中的数据之前在行中添加一些静态字段,以及如何编写以获取不同的字符串类型的静态行。我为获取合并数据而编写的查询是

SELECT * FROM(SELECT C.NAME,C.BASEAMT FROM CONTRACTOR C)
pivot(SUM(baseamt) FOR name IN('Mothi' AS Mothi,'Rakesh' AS Rakesh))
UNION ALL 
SELECT * FROM(SELECT C.NAME,C.CGSTAMT FROM CONTRACTOR C) 
pivot(SUM(CGSTAMT) FOR name IN('Mothi' AS Mothi,'Rakesh' AS Rakesh))
UNION ALL
SELECT * FROM(SELECT C.NAME,C.SGSTAMT FROM CONTRACTOR C) 
pivot(SUM(SGSTAMT) FOR name IN ('Mothi' AS Mothi,'Rakesh' AS Rakesh))
UNION ALL
SELECT * FROM(SELECT C.NAME,C.IGSTAMT FROM CONTRACTOR C) 
pivot( SUM(IGSTAMT) FOR name IN ('Mothi' AS Mothi,'Rakesh' AS Rakesh))
UNION ALL
SELECT * FROM(SELECT C.NAME,C.CESSAMT FROM CONTRACTOR C) 
pivot( SUM(CESSAMT) FOR name IN ('Mothi' AS Mothi,'Rakesh' AS Rakesh))
UNION ALL
SELECT * FROM(SELECT C.NAME,C.AMTINCLTAX FROM CONTRACTOR C) 
pivot(SUM(AMTINCLTAX) FOR name IN ('Mothi' AS Mothi,'Rakesh' AS Rakesh))
UNION ALL
SELECT * FROM(SELECT C.NAME,C.BASEAMT FROM AFTERNEGOTIATION C) 
pivot(SUM(baseamt) FOR name IN ('Mothi' AS Mothi,'Rakesh' AS Rakesh))
UNION ALL 
SELECT * FROM(SELECT C.NAME,C.CGSTAMT FROM AFTERNEGOTIATION C)
pivot(SUM(CGSTAMT) FOR name IN ('Mothi' AS Mothi,'Rakesh' AS Rakesh ))
UNION ALL
SELECT * FROM(SELECT C.NAME,C.SGSTAMT FROM AFTERNEGOTIATION C) 
pivot(SUM(SGSTAMT) FOR name IN ('Mothi' AS Mothi,'Rakesh' AS Rakesh))
UNION ALL
SELECT * FROM(SELECT C.NAME,C.IGSTAMT FROM AFTERNEGOTIATION C) 
pivot(SUM(IGSTAMT) FOR name IN ('Mothi' AS Mothi,'Rakesh' AS Rakesh))
UNION ALL
SELECT * FROM(SELECT C.NAME,C.CESSAMT FROM AFTERNEGOTIATION C)
pivot(SUM(CESSAMT) FOR name IN ('Mothi' AS Mothi,'Rakesh' AS Rakesh ))
UNION ALL
SELECT * FROM(SELECT C.NAME,C.AMTINCLTAX FROM AFTERNEGOTIATION C) 
pivot(SUM(AMTINCLTAX) FOR name IN ('Mothi' AS Mothi,'Rakesh' AS Rakesh))

如何通过最小的并集获得所需的输出,并根据从第一个表转置的金额,在每行的开头添加诸如基本金额,CGST,SGST,IGST,CESS和Amtincltax的行。 如果我想在数据透视中插入子句,这可能吗?如果是的话,该怎么办。

0 个答案:

没有答案