简体   繁体   English

如何在 T-SQL 中使用 group by 和 union

[英]How to use group by with union in T-SQL

How can I using group by with union in T-SQL?如何在 T-SQL 中使用 group by 和 union? I want to group by the first column of a result of union, I wrote the following SQL but it doesn't work.我想按联合结果的第一列进行分组,我写了以下 SQL 但它不起作用。 I just don't know how to reference the specified column (in this case is 1) of the union result.我只是不知道如何引用联合结果的指定列(在本例中为 1)。

SELECT  *
FROM    ( SELECT    a.id ,
                    a.time
          FROM      dbo.a
          UNION
          SELECT    b.id ,
                    b.time
          FROM      dbo.b
        )
GROUP BY 1

You need to alias the subquery.您需要为子查询添加别名。 Thus, your statement should be:因此,您的陈述应该是:

Select Z.id
From    (
        Select id, time
        From dbo.tablea
        Union All
        Select id, time
        From dbo.tableb
        ) As Z
Group By Z.id

GROUP BY 1按 1 分组

I've never known GROUP BY to support using ordinals, only ORDER BY.我从来不知道 GROUP BY 支持使用序数,只有 ORDER BY。 Either way, only MySQL supports GROUP BY's not including all columns without aggregate functions performed on them.无论哪种方式,只有 MySQL 支持 GROUP BY 不包括没有对它们执行聚合函数的所有列。 Ordinals aren't recommended practice either because if they're based on the order of the SELECT - if that changes, so does your ORDER BY (or GROUP BY if supported).也不建议使用序数,因为如果它们基于 SELECT 的顺序 - 如果发生变化,那么您的 ORDER BY(或 GROUP BY 如果支持)也会发生变化。

There's no need to run GROUP BY on the contents when you're using UNION - UNION ensures that duplicates are removed;使用UNION时无需对内容运行GROUP BY - UNION 确保删除重复项; UNION ALL is faster because it doesn't - and in that case you would need the GROUP BY... UNION ALL更快,因为它没有 - 在这种情况下,您将需要 GROUP BY ...

Your query only needs to be:您的查询只需:

SELECT a.id,
       a.time
  FROM dbo.TABLE_A a
UNION
SELECT b.id,
       b.time
  FROM dbo.TABLE_B b

Identifying the column is easy:识别列很容易:

SELECT  *
FROM    ( SELECT    id,
                    time
          FROM      dbo.a
          UNION
          SELECT    id,
                    time
          FROM      dbo.b
        )
GROUP BY id

But it doesn't solve the main problem of this query: what's to be done with the second column values upon grouping by the first?但这并没有解决这个查询的主要问题:在按第一列分组后,第二列值要做什么? Since (peculiarly!) you're using UNION rather than UNION ALL , you won't have entirely duplicated rows between the two subtables in the union, but you may still very well have several values of time for one value of the id, and you give no hint of what you want to do - min, max, avg, sum, or what?!由于(特别是!)您使用的是UNION而不是UNION ALL ,因此您在联合中的两个子表之间不会有完全重复的行,但是对于 id 的一个值,您可能仍然有多个时间值,并且你没有暗示你想要做什么——最小值、最大值、平均值、总和,或者什么?! The SQL engine should give an error because of that (though some such as mysql just pick a random-ish value out of the several, I believe sql-server is better than that).因此,SQL 引擎应该给出一个错误(尽管一些如 mysql 只是从几个中选择一个随机值,我相信 sql-server 比那个更好)。

So, for example, change the first line to SELECT id, MAX(time) or the like!因此,例如,将第一行更改为SELECT id, MAX(time)等!

with UnionTable as  
(
    SELECT a.id, a.time FROM dbo.a
    UNION
    SELECT b.id, b.time FROM dbo.b
) SELECT id FROM UnionTable GROUP BY id

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

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