[英]Group by in Oracle SQL
I have many rows with the fields name
, unit
, amount
. 我有很多行,其中包含
name
, unit
, amount
字段。 The rows could be 这些行可能是
name, unit, amount
========
Some name, A, 100
Some name, B, 300
Another name, A, 400
I want to select all the rows but grouped by name. 我想选择所有行,但按名称分组。
The unit can be either A or B. I want an output as: 单位可以是A或B。我希望输出为:
name, A_amount, B_amount
============
Some name, 100, 300
Another name, 400, 0
So depending on the unit the amount should either be in A_amount or B_amount. 因此,根据单位,金额应为A_amount或B_amount。
You need a pivot operation on your base table to achieve this result. 您需要在基表上进行数据透视操作以实现此结果。
SELECT *
FROM (SELECT name, unit, amount FROM table1)
PIVOT (amount AS amount FOR (unit) IN ('A' AS a, 'B' AS b))
ORDER BY name;
This query should work for you. 此查询应该为您工作。 Refer this tutorial to learn more about pivot operations.
请参考本教程,以了解有关数据透视操作的更多信息。
Can you try the following query 你可以尝试以下查询
/ For query 1 / / 查询1 /
Select * from #tblsample A
inner join #tblsample2 B
On A.ProductId=B.ProductId
/ FOR QUERY 2 / / 查询2 /
select * from #tblsample
except
select * from #tblsample2
/ alternate way for query 2 / / 查询2的替代方法 /
select * from #tblsample
where productid NOT IN(
select * from #tblsample2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.