SQL - Ugly combination of GROUP BY and COALESCE

后端 未结 5 1752
Happy的楠姐
Happy的楠姐 2021-01-18 08:01

I have a table with data similar to the following:

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

The longer I work on this, the uglier my SQL is getti

5条回答
  •  南笙
    南笙 (楼主)
    2021-01-18 08:30

    Another one:

    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;
    

提交回复
热议问题