0
聚合需要我有一個表是這樣的:子查詢在SQL
Id Date Price Item Type
1 2009-09-21 25 1 M
2 2009-08-31 16 2 T
1 2009-09-23 21 1 M
2 2009-09-03 12 3 T
我嘗試接收ID和的總和價格MULT物品列的輸出用於type =「M」,並用相同的邏輯的另一列用於type =「T」
只有這樣,如何做到這一點對我來說是使用多CTE但它是一種複雜的和大:
with cte as (
select distinct a.id, a.date
sum(price*a.item) as numm
from table a
where a.type='M'
group by a.id),
crx as (
select cte.id, cte.numm, sum(a.price*a.item) as numm_1 from cte
join table a on a.id=cte.id and a.date=cte.date
where a.type='T'
group by cte.id)
select * from crx
具有一定的感覺,它可以做的更好(例如使用子查詢)-asking y你怎麼能這樣做。
p.s.
SQLlite的東西將不勝感激!
謝謝!
這並不在SQLite的工作。 –