tangrongjie
tangrongjie
采纳率80%
2018-12-20 09:39 阅读 602

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

5

把下面三段写在一个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;

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    aaether _ether 2018-12-20 01:58

    用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;
    
    点赞 评论 复制链接分享
  • it_zhangwei 路漫漫兮其修远兮 2018-12-20 03:26

    你要的连接在一起,使用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;
    
    
    点赞 评论 复制链接分享

相关推荐