약간의 시간이 계속되지만 여기 간다 :SQL 열은
아픈 당신이 내 데이터 집합 보여 예제 데이터를 통해 : 판매, 제품 및 고객에 대한 질의가 있습니다. 그것은 다음과 같이 보인다 :
customer_type | region | product_name | date | sale_value
smallstore | north | chair | 2016-01-01 | 22482
mediumstore | south | table | 2016-02-02 | 50582
bigstore | east | desktop | 2016-02-02 | 83737
smallstore | north | chair | 2015-01-01 | 23828
mediumstore | south | coach | 2015-02-02 | 93833
bigstore | east | desktop | 2015-02-02 | 83282
쿼리는 다음과 같이 보일 것이다 :
나는 다음 결과 얻을 필요가select
c.customer_type,
c.region,
p.product_name,
s.date,
sum(s.sale_value)
from
sales s
inner join products p on p.id = s.product_id
inner join customers c on c.id = s.customer_id
group by c.customer_type, c.region, p.product_name, s.date,
: 즉 그래서
customer_type | region | product_name | date | sales2015 | sales2016
smallstore | north | chair | 2015-01-01 | 23828 | 22482
mediumstore | south | table | 2016-02-02 | 0 | 50582
mediumstore | south | coach | 2015-02-02 | 93833 | 0
bigstore | east | desktop | 2015-02-02 | 83282 | 83737
을 : 내가 얻을 필요 각 CUSTOMER_TYPE, 지역 및 제품 _는, 2015 년 매출은 내가 얻을 수있는 쿼리를 변경할 수있는 방법 2016 년
에 비해 그거? 나는 형태
select
c.customer_type,
c.region,
p.product_name,
s.date,
(select sum(sale_value) from ... where date between 2015-01-01 and 2015-12-31 group by ...) as sales2015,
(select sum(sale_value) from ... where date between 2016-01-01 and 2016-12-31 group by ...) as sales2016,
from
sales s
inner join products p on p.id = s.product_id
inner join customers c on c.id = s.customer_id
group by c.customer_type, c.region, p.product_name, s.date,
의 서브 쿼리의 다른 변화를 시도했지만 메인 쿼리와 하위 쿼리에 가입 할 ID가 없기 때문에 나는 선택 사이의 크로스 제품을 얻고있다. 나는 또한 1 년 또는 다른 한 해 동안 누락 된 가치를 관리하는 방법을 모른다.
당신의 도움이 당신에게 사전에 대단히 감사합니다. 대신 하위 쿼리의
나는 테이블의 열'sales' 표시되지 않습니다. – Tenzin
'sale_value'입니다. 죄송합니다 –