简体   繁体   English

分组,多个计数,多个表SQL Server

[英]Group by, multiple count, multiple tables SQL Server

I have two tables as following: 我有两个表,如下所示:

First table: 2016 第一张桌子:2016年

Os          |   Count
------------+----------
Windows 7         8
Windows 7         9
Windows 7        20
Windows 8        30
Linux            15

Second table: 2017 第二表:2017年

Os          |   Count
------------+----------
Windows 7        35
Windows 7        11
Windows 8        10
Windows 8         8
Linux            10
Ubuntu            3

I try by myself but my challenge is to use GROUP BY function in meanwhile I'm counting two fields from two different tables, I got always error. 我自己尝试,但是我的挑战是在同时计算两个不同表中的两个字段时使用GROUP BY函数,但总是出错。

Thank you in advance, 先感谢您,

Update: 更新:

I'm sorry I did a mistake in explaining my request: what I need is to write a query to get a result like this: 抱歉,我在解释我的请求时犯了一个错误:我需要写一个查询来获得如下结果:

    --OS--         --2016--    --2017--
    ------------------------------------
    Windows 7        37             46
    Windows 8        30             18
    Linux            15             10
    Ubuntu           0              3

You can do simple group by as below: 您可以按以下方式进行简单分组:

select a.OS, sum(count) from (
    select * from your2016table
        union all 
    select * from your2017table
    ) a
    group by a.OS

For that you can use year as group by and pivot 为此,您可以将year作为分组依据并进行透视

;with cte as (
select a.OS, a.[year], SumCt = sum(count) from (
    select *, 2016 as [year] from your2016table
        union all 
    select *, 2017 as [year] from your2017table
    ) a
    group by a.OS, a.[year]
)
select * from cte 
pivot (max(sumct) for [year] in ([2016], [2017])) p

Without a UNION ALL you can do this: 没有UNION ALL您可以执行以下操作:

SELECT
    COALESCE( [2016].[OS], [2017].[OS] ) AS [OS],
    ( [2016].[Count] + [2017].[Count] ) AS [Count]
FROM
    [2016]
    FULL OUTER JOIN [2017] ON [2016].[OS] = [2017].[OS]

This also eliminates the need for GROUP BY too, assuming the OS columns contain no duplicates. 假设OS列不包含重复项,这也消除了对GROUP BY的需要。

Duplicates would need to be eliminated through GROUP BY (rather than SELECT DISTINCT ), but the same structure applies: 需要通过GROUP BY (而不是SELECT DISTINCT )消除重复项,但是适用相同的结构:

SELECT
    COALESCE( [2016-D].[OS], [2017].[OS] ) AS [OS],
    ( [2016-D].[Count] + [2017].[Count] ) AS [Count]
FROM
    (
        SELECT [OS], SUM( [Count] ) FROM [2016] GROUP BY [OS]
    ) AS [2016-D]
    FULL OUTER JOIN
    (
        SELECT [OS], SUM( [Count] ) FROM [2017] GROUP BY [OS]
    ) AS [2017-D]
        ON [2016].[OS] = [2017].[OS]

...but then it gets quite unwieldy, and the UNION ALL approach becomes simpler! ...但是随后变得非常笨拙,并且UNION ALL方法变得更加简单!

Update: 更新:

The OP has amended their question to say they want separate columns - so this is where using an OUTER JOIN becomes the ideal solution: OP修改了他们的问题,说他们想要单独的列-因此在这里使用OUTER JOIN成为理想的解决方案:

SELECT
    COALESCE( [2016-D].[OS], [2017-D].[OS] ) AS [OS],
    [2016-D].[Count] AS [2016],
    [2017-D].[Count] AS [2017]
FROM
    (
        SELECT
            [OS],
            SUM( [Count] ) AS [Count]
        FROM
            [2016]
        GROUP BY
            [OS]
    ) AS [2016-D]
    FULL OUTER JOIN
    (
        SELECT
            [OS],
            SUM( [Count] ) AS [Count]
        FROM
            [2017]
        GROUP BY
            [OS]
    ) AS [2017-D]
        ON [2016].[OS] = [2017].[OS]
ORDER BY
    [OS]

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

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