简体   繁体   English

SQL - GROUP BY和COALESCE的丑陋组合

[英]SQL - Ugly combination of GROUP BY and COALESCE

I have a table with data similar to the following: 我有一个表格,其数据类似于以下内容:

[ID], [State], [foo], [DateCreated], [DateUpdated] [ID],[州],[foo],[DateCreated],[DateUpdated]

The longer I work on this, the uglier my SQL is getting, which tells me I'm probably doing something wrong. 我在这方面工作的时间越长,我的SQL就会变得更加丑陋,这告诉我,我可能做错了什么。 What I want is a unique list of each State so long as foo is always the same for that State (if foo is not the same for all records in that State, I don't want that State at all) . 我想要的是每个国家的唯一清单,只要该国的foo总是相同的(如果foo对于该州的所有记录都不相同,我根本不想要那个国家) Also, I want to COALESCE DateCreated and DateUpdated and want the maximum value for that State. 此外,我想要COALESCE DateCreated和DateUpdated,并希望该状态的最大值。

So given this data: 所以给出这样的数据:

[ID], [State], [foo], [DateCreated], [DateUpdated]
1,  MA, data1,  05/29/2012, 06/02/2012
2,  MA, data1,  05/29/2012, 06/03/2012
3,  RI, data2,  05/29/2012, NULL
4,  RI, data3,  05/29/2012, NULL
5,  NH, data4,  05/29/2012, NULL
6,  NH, data4,  05/29/2012, 06/05/2012

I'd like only these results: 我只想要这些结果:

[State], [foo], [LastUpdated]
MA, data1,  06/03/2012
NH, data4,  06/05/2012

What's the most elegant way to get what I'm after? 得到我所追求的最优雅的方法是什么?

Another one: 另一个:

http://sqlfiddle.com/#!6/fd219/1 http://sqlfiddle.com/#!6/fd219/1

SELECT
  t.State,
  MAX(t.foo),
  MAX( COALESCE( t.DateUpdated, t.DateCreated ))
FROM t
GROUP BY t.State
HAVING COUNT(DISTINCT t.foo) = 1;

A simple Group by with nested queries should suffice: 使用嵌套查询的简单Group by就足够了:

Select State, coalesce(max_created,max_updated) from (
  Select State, min(foo) as min_foo, max(foo) as max_foo, 
    max(DateCreated) as max_created,
    max(DateUpdated) as max_updated
  From Data
  Group by State)
 Where min_foo = max_foo

Not as elegant, but for you poor SQL 2000 souls: 不那么优雅,但对于你可怜的SQL 2000灵魂:

SELECT T1.State, T2.Foo, T1.LastUpdated
FROM (
    SELECT State, MAX([ID]) AS [ID], 
        MAX(COALESCE(DateUpdated, DateCreated)) AS LastUpdated
    FROM YourTable
    GROUP BY State
    HAVING COUNT(DISTINCT Foo) = 1
) T1 
INNER JOIN YourTable T2 ON T1.State = T2.State AND T1.[ID] = T2.[ID]

Assuming you are using SQL Server 2005 or > 假设您使用的是SQL Server 2005或>

Try this: 试试这个:

WITH Data AS
(
    SELECT  *,
        COALESCE([DateCreated], [DateUpdated]) AS LastUpdated,
        ROW_NUMBER() OVER(PARTITION BY State ORDER BY COALESCE([DateCreated], [DateUpdated]) DESC) Position
      FROM <YOUR-TABLE> a
     WHERE NOT EXISTS
     (
        SELECT  1 
            FROM    <YOUR-TABLE> b
         WHERE  a.State = b.State
            AND a.foo <> b.foo
     )
)
SELECT State, foo, LastUpdated
  FROM Data
 WHERE Positon = 1

试试这个:

select state_name,foo,max(dateUpdated) from state where state_name in (select state_name from state group by state_name having count(distinct foo)=1) group by state_name,foo;

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

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