Group By使用基于另一列的聚合

时间:2014-05-12 18:36:58

标签: sql sql-server sql-server-2005 group-by exacttarget

对于每个电子邮件地址包含多行的数据库,我希望按每个电子邮件地址进行分组,并为每个电子邮件地址提供“最新”信息。

Email      Col1    Col2    Col3   Col4    CustomerID
=======    ====    ====    ====   ====    ==========
a@a.com    a       a       a      null    1
a@a.com    null    b       b      null    2
a@a.com    null    null    c      null    3

我想使用最高CustomerID非null 值。对于上述情况,我希望:

Email      Col1    Col2    Col3   Col4
=======    ====    ====    ====   ====
a@a.com    a       b       c      null

我可以为GROUP BY执行每个列的MAX,但它只是按字母顺序排列的最高值,并且不考虑CustomerID

SQL Fiddle

SELECT EmailAddress, MAX(FirstName), MAX(LastName), MAX(Gender), MAX(Birthday), MAX(Country)
FROM CustomerInfo
GROUP BY EmailAddress

此外,这是在精确目标中编程,意味着some SQL keywords are unsupported,最值得注意的是变量,临时表和游标不受支持。

鉴于这些限制,是否有可能获得理想的结果?

1 个答案:

答案 0 :(得分:3)

如果我正确理解您的问题,我认为您需要多次加入该表。这样的事情应该使用common table expression来获取列不是max的每列的null客户ID。然后它连接回自己以获得值:

with cte as (
  select email, 
      max(case when col1 is not null then customerid end) maxcustomerid1,
      max(case when col2 is not null then customerid end) maxcustomerid2,
      max(case when col3 is not null then customerid end) maxcustomerid3,
      max(case when col4 is not null then customerid end) maxcustomerid4
    from yourtable
    group by email
)
select t.email,
  t1.col1,
  t2.col2, 
  t3.col3,
  t4.col4
from cte t
  left join yourtable t1 on t.email = t1.email and t.maxcustomerid1 = t1.customerid
  left join yourtable t2 on t.email = t2.email and t.maxcustomerid2 = t2.customerid
  left join yourtable t3 on t.email = t3.email and t.maxcustomerid3 = t3.customerid
  left join yourtable t4 on t.email = t4.email and t.maxcustomerid4 = t4.customerid