同一个表格字段多条件分三列显示

把下面三段写在一个SQL语句里,实现一句SQL,三列显示
表格截图如下图片说明


SELECT 
    sku_p as aa,  
    COUNT(sku_p) as bb,
    amount as cc,
    SUM(amount) as dd

FROM
tongji 
WHERE amount_description='Principal'
GROUP BY sku_p
HAVING COUNT(sku_p) >= 1;

SELECT 
    sku_p as aa,  
    COUNT(sku_p) as bb,
    amount as cc,
    SUM(amount) as dd

FROM
tongji 
WHERE amount_description='FBAPerUnitFulfillmentFee'
GROUP BY sku_p
HAVING COUNT(sku_p) >= 1;

SELECT 
    sku_p as aa,  
    COUNT(sku_p) as bb,
    amount as cc,
    SUM(amount) as dd

FROM
tongji 
WHERE amount_description='Commission'
GROUP BY sku_p
HAVING COUNT(sku_p) >= 1;

用or连接查询条件即可

SELECT 
    sku_p as aa,  
    COUNT(sku_p) as bb,
    amount as cc,
    SUM(amount) as dd,
        (case when amount_description='Principal' then 'Principal' else '' end) as 'Principal',
        (case when amount_description='FBAPerUnitFulfillmentFee' then 'FBAPerUnitFulfillmentFee' else '' end) as 'FBAPerUnitFulfillmentFee',
        (case when amount_description='Commission' then 'Commission' else '' end) as Commission',       

FROM
tongji 

WHERE amount_description='Principal'
OR amount_description='FBAPerUnitFulfillmentFee'
OR amount_description='Commission'

GROUP BY sku_p
HAVING COUNT(sku_p) >= 1;

你要的连接在一起,使用union ALl 不去重数据连接


SELECT 
    sku_p as aa,  
    COUNT(sku_p) as bb,
    amount as cc,
    SUM(amount) as dd

FROM
tongji 
WHERE amount_description='Principal'
GROUP BY sku_p
HAVING COUNT(sku_p) >= 1
 UNION  ALL
SELECT 
    sku_p as aa,  
    COUNT(sku_p) as bb,
    amount as cc,
    SUM(amount) as dd

FROM
tongji 
WHERE amount_description='FBAPerUnitFulfillmentFee'
GROUP BY sku_p
HAVING COUNT(sku_p) >= 1
 UNION  ALL
SELECT 
    sku_p as aa,  
    COUNT(sku_p) as bb,
    amount as cc,
    SUM(amount) as dd

FROM
tongji 
WHERE amount_description='Commission'
GROUP BY sku_p
HAVING COUNT(sku_p) >= 1;