集团通过与UNION [英] Group by with UNION

查看:155
本文介绍了集团通过与UNION的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code不正确的分组,它仍然不组[Reden uitstroom2]和[Reden uitstroom3]与[Reden uitstroom1。计工作正常,但在它的表现重复[Reden Uitstroom1]。

例如:

Reden uitstroom1 = 1个A / 2X乙
Reden uitstroom2 = 1个A / 1X乙

Aantal Uitstroom 2014年 - Reden Uitstroom1
1 - 一个
1 - 一个
2 - B
1 - B

应该是:

Aantal Uitstroom 2014年 - Reden Uitstroom1
2 - 一个
3 - B

我似乎无法改变[Reden Uitstroom1]到[Reden Uitstroom]因为那时它返回,而不是A / B号...

SELECT COUNT(Hertoetsing。[Reden uitstroom1])AS [Aantal Uitstroom 2014年],                    (Hertoetsing。[Reden uitstroom1])AS [Reden Uitstroom1] 从Klantinformatie INNER JOIN Hertoetsing     ON Klantinformatie.KlantID = Hertoetsing.Klantid WHERE(((年(Hertoetsing。[基准uitstroom1]))= 2014年)) GROUP BY Hertoetsing。[Reden uitstroom1] UNION ALL SELECT COUNT(Hertoetsing。[Reden uitstroom2])AS [Aantal Uitstroom 2014年],                    (Hertoetsing。[Reden uitstroom2])AS [Reden Uitstroom1] 从Klantinformatie INNER JOIN Hertoetsing     ON Klantinformatie.KlantID = Hertoetsing.Klantid WHERE(((年(Hertoetsing。[基准uitstroom2]))= 2014年)) GROUP BY Hertoetsing。[Reden uitstroom2] UNION ALL SELECT COUNT(Hertoetsing。[Reden uitstroom3])AS [Aantal Uitstroom 2014年],                    (Hertoetsing。[Reden uitstroom3])AS [Reden Uitstroom1] 从Klantinformatie INNER JOIN Hertoetsing     ON Klantinformatie.KlantID = Hertoetsing.Klantid WHERE(((年(Hertoetsing。[基准uitstroom3]))= 2014年)) GROUP BY Hertoetsing [Reden uitstroom3]。

解决方案

您需要包装到另外的查询 SELECT ,即

  SELECT
    SUM([Aantal Uitstroom 2014年])AS [Aantal Uitstroom 2014年],
    [Reden Uitstroom]
从
(
     [UNION查询到这里]

)
GROUP BY [Reden Uitstroom]
 

没有 SELECT 包装,所有它做的是附加的每一个人计数的结果,因为没有总和或分组适用于它。

My code is not grouping properly, it still doesn't group [Reden uitstroom2] and [Reden uitstroom3] with [Reden uitstroom1]. The count works properly but its showing duplicates in [Reden Uitstroom1].

For Example:

Reden uitstroom1 = 1x A / 2x B
Reden uitstroom2 = 1x A / 1x B

Aantal Uitstroom 2014 - Reden Uitstroom1
1 - A
1 - A
2 - B
1 - B

Which Should be:

Aantal Uitstroom 2014 - Reden Uitstroom1
2 - A
3 - B

I can't seem to change [Reden Uitstroom1] into [Reden Uitstroom] because then it returns numbers instead of A / B...

SELECT        Count(Hertoetsing.[Reden uitstroom1]) AS [Aantal Uitstroom 2014],
                   (Hertoetsing.[Reden uitstroom1]) AS [Reden Uitstroom1]

FROM          Klantinformatie     

INNER JOIN    Hertoetsing 
    ON        Klantinformatie.KlantID=Hertoetsing.Klantid

WHERE         (((Year(Hertoetsing.[Datum uitstroom1]))=2014))

GROUP BY      Hertoetsing.[Reden uitstroom1]

UNION ALL

SELECT        Count(Hertoetsing.[Reden uitstroom2]) AS [Aantal Uitstroom 2014],
                   (Hertoetsing.[Reden uitstroom2]) AS [Reden Uitstroom1]

FROM          Klantinformatie 

INNER JOIN    Hertoetsing 
    ON        Klantinformatie.KlantID=Hertoetsing.Klantid

WHERE         (((Year(Hertoetsing.[Datum uitstroom2]))=2014))

GROUP BY      Hertoetsing.[Reden uitstroom2]

UNION ALL

SELECT        Count(Hertoetsing.[Reden uitstroom3]) AS [Aantal Uitstroom 2014],
                   (Hertoetsing.[Reden uitstroom3]) AS [Reden Uitstroom1]

FROM          Klantinformatie 

INNER JOIN    Hertoetsing 
    ON        Klantinformatie.KlantID=Hertoetsing.Klantid

WHERE         (((Year(Hertoetsing.[Datum uitstroom3]))=2014))

GROUP BY      Hertoetsing.[Reden uitstroom3];

解决方案

You need to wrap the query in another SELECT, i.e.

SELECT 
    Sum ([Aantal Uitstroom 2014]) AS [Aantal Uitstroom 2014], 
    [Reden Uitstroom]
FROM
(
     [UNION query goes here]

)
GROUP BY [Reden Uitstroom]

Without the SELECT wrapper, all it's doing is appending the results of each individual Count as there is no summation or grouping applied to it.

这篇关于集团通过与UNION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆