如何使用GROUP BY

时间:2018-09-18 09:20:08

标签: sql sql-server

我有这张桌子,为简单起见,我只列出了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 

4 个答案:

答案 0 :(得分:1)

使用条件聚合:

   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

答案 1 :(得分:0)

使用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

答案 2 :(得分:0)

您可以将SUM结合使用:

CASE 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,则可以添加0,也可以使用ELSE 0代替COUNT

答案 3 :(得分:0)

您的查询没有为“ 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 ;

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