繁体   English   中英

使用GROUP BY将多个Select语句放入不同的列

[英]Put Several Select Statements into Different Columns with Group BY

我有此表,为简单起见,我仅包含1个客户名称,其中有很多

+--------------+---------------------+---------------+
| customername | customercontactname | statename     |
+--------------+---------------------+---------------+
| IKEA         | Sam                 | Won           |
| IKEA         | Sam                 | Won           |
| IKEA         | Sam                 | Won           |
| IKEA         | Sara                | Won           |
| IKEA         | Sara                | Won           |
| IKEA         | Sara                | Won           |
| IKEA         | Sara                | Won           |
| IKEA         | Amelia              | Lost          |
| IKEA         | Maya                | Won           |
| IKEA         | Maya                | Won           |
+--------------+---------------------+---------------+

我想要这个输出

+--------------+---------------------+---------+----------+
| customername | customercontactname | WonOpps | LostOpps |
+--------------+---------------------+---------+----------+
| IKEA         | Sam                 | 3       | NULL     |
| IKEA         | Sara                | 4       | NULL     |
| IKEA         | Maya                | 2       | NULL     |
| IKEA         | Amelia              | NULL    | 1        |
+--------------+---------------------+---------+----------+

试用(前三行的结果很好,但是Amelia没有显示在我的最终输出中):

SELECT t1.customername, 
       t1.customercontactname, 
       t1.wonopps, 
       t2.lostopps 
FROM   (SELECT customername, 
               customercontactname, 
               Count(*) AS WonOpps 
        FROM   mytable 
        WHERE  statename = 'won' 
        GROUP  BY customername, 
                  customercontactname) t1 
       LEFT JOIN (SELECT customername, 
                         customercontactname, 
                         Count(*) AS LostOpps 
                  FROM   mytable 
                  WHERE  statename = 'lost' 
                  GROUP  BY customername, 
                            customercontactname) t2 
              ON t1.customername = t2.customername 
                 AND t1.customercontactname = t2.customercontactname 

使用条件聚合:

   select customername, customercontactname, count(case when statename='Won' then 1 end ) WonOpps,
   count(case when statename='Lost' then 1 end ) WonLost
   from tablename
   group by customername, customercontactname

使用sum

 with t1 as
     (
       select customername,
       customercontactname,
       sum(case when statename='won' then 1 else 0 end ) as WonOpps,
       sum(case when statename='loss' then 1 else 0 end ) as LostOpps
       from t
       group by customername, customercontactname
) select customername,customercontactname, case when WonOpps>1 then WonOpps else null end as WonOpps,
case when LostOpps>1 then LostOpps else LostOpps end as LostOpps from t1

您可以将SUMCASE WHEN结合使用:

SELECT    customername,
          customercontactname,
          SUM(CASE WHEN statename = 'Won'  THEN 1 END) AS WonOpps,
          SUM(CASE WHEN statename = 'Lost' THEN 1 END) AS WonLost
FROM      mytable
GROUP BY  customername, customercontactname

如果没有记录匹配,则SUM返回NULL ,因为我们未指定ELSE -values。

如果您更愿意看到0 ,则可以添加ELSE 0 ,或者可以使用COUNT而不是SUM

您的查询未提供“ Amelia”行的原因是因为您使用的是LEFT JOIN,并且因为Amelia在statename列中没有任何“ won”值,因此连接左侧为null。 为了很好地解释概念,我给出一个简单的解决方案。

  1. 写一个左联接查询。 它会给您所有WonOpps。 (您当前的查询)
  2. 写一个正确的联接查询。 它将给您所有LossOpps。
  3. 合并以上两个查询。

请参阅以下查询:

select a.customername, a.customercontactname, a.WonOpps, b.LostOpps from
(select customername,customercontactname, count(*) as WonOpps from mytable WHERE  statename = 'won' group by customername,customercontactname) a
 left join
(select customername,customercontactname, count(*) as LostOpps from mytable WHERE  statename = 'lost' group by customername,customercontactname) b
on a.customername = b.customername and a.customercontactname = b.customercontactname 
UNION ALL 
select d.customername, d.customercontactname, c.WonOpps, d.LostOpps  from
(select customername,customercontactname, count(*) as WonOpps from mytable WHERE  statename = 'won' group by customername,customercontactname) c
 right join
(select customername,customercontactname, count(*) as LostOpps from mytable WHERE  statename = 'lost' group by customername,customercontactname) d
on c.customername = d.customername and c.customercontactname = d.customercontactname ;

您可以使用诸如求和,计数,大小写之类的许多其他选项来编写较短的查询,但是该查询将帮助您理解当前工作之上的概念。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM