SQL select查询两行组合

时间:2017-07-26 04:42:16

标签: sql

我有两个名为A,B的表。我编写SQL查询以从A,B中检索数据。它如下:

SELECT  A.No ,
    A.ItemCode, 
        case when B.Year = 1 then SUM(B.Sale_Amount)  end as "1Year",
        case when B.Year = 2 then SUM(B.Sale_Amount)  end as "2Year",
        case when B.Year = 3 then SUM(B.Sale_Amount)  end as "3Year"
FROM  A  
LEFT OUTER JOIN  B ON B.ID  = A.CODE    
GROUP BY
A."No",
A."ItemCode",
B."Year"

这给出了以下输出:

result

但我需要将这三行选择为两行,如下所示:

reslut wanted

1 个答案:

答案 0 :(得分:0)

这是正确答案

retrieveHumidDataAssign()

FROM A
左外联接B B.ID = A.CODE
GROUP BY A.No,A.ItemCode