T-SQL合并两个COUNT和GROUP BY查询

时间:2016-08-04 13:59:38

标签: sql-server tsql

我写了以下2个查询:

SELECT g.Name As 'Group Name' ,COUNT(*) As 'Tickets Closed', CAST(t.ResolvedAt AS DATE) As 'Date Closed' FROM Tickets t
JOIN Groups g ON g.ID = t.AssignedGroup
GROUP BY CAST(t.ResolvedAt AS DATE), g.Name
ORDER BY CAST(t.ResolvedAt AS DATE) DESC

SELECT g.Name As 'Group Name', COUNT(*) As 'Tickets Open', CAST(t.Created AS DATE) As 'Date Open' FROM Tickets t
JOIN Groups g ON g.ID = t.AssignedGroup
GROUP BY CAST(t.Created AS DATE), g.Name
ORDER BY CAST(t.Created AS DATE) DESC

这两个都在做我想要的,它们每个都有以下布局:

| Group Name | Tickets Open | Date Open |
-----------------------------------------
| Foo        | 13           | 2016-01-02|
| Bar        | 9            | 2016-01-02|
| Foo        | 70           | 2016-01-01|
| Bar        | 34           | 2016-01-01|

获取已关闭票证的查询显然是相同的布局,只是不同的数字(组名称仍然相同),我一直在尝试合并2个查询以返回以下布局中的数据:

| Group Name | Tickets Open | Tickets Closed | Date      |
----------------------------------------------------------
| Foo        | 13           | 5              | 2016-01-02|
| Bar        | 9            | 8              | 2016-01-02|
| Foo        | 70           | 15             | 2016-01-01|
| Bar        | 34           | 12             | 2016-01-01|

我已尝试使用UNION关键字,但我无法使用GROUP BY关键字,我不断收到有关未包含在聚合函数或GROUP BY子句中的列的错误。

如果有人知道如何以上述格式返回数据,我们将非常感激。

谢谢。

5 个答案:

答案 0 :(得分:0)

最简单的方法是为每个人使用CTE,然后加入群组和日期。我使用了完整的连接,因此您不会排除仅打开或仅关闭的行。

此外,UNION在垂直方向上不是水平组合,以至于如果您有两组具有相同格式的数据,则UNION将是每个组的所有行的组合。你将保持相同的3列格式,并将所有行放在一个较大的集合中,但我不认为在你的例子判断之后你就是这样。

WITH OPENCTE AS
(
    SELECT g.Name As 'Group Name', COUNT(*) As 'Tickets Open', CAST(t.Created AS DATE) As 'Date Open' FROM Tickets t
    JOIN Groups g ON g.ID = t.AssignedGroup
    GROUP BY CAST(t.Created AS DATE), g.Name
)
, CLOSECTE AS
(
SELECT g.Name As 'Group Name' ,COUNT(*) As 'Tickets Closed', CAST(t.ResolvedAt AS DATE) As 'Date Closed' FROM Tickets t
JOIN Groups g ON g.ID = t.AssignedGroup
GROUP BY CAST(t.ResolvedAt AS DATE), g.Name
)
SELECT 'Group Name'
    ,'Tickets Open'
    ,'Tickets Closed'
    ,'Date Closed'
FROM OPENCTE AS O
    FULL JOIN CLOSECTE AS C
        ON O.[Group Name] = C.[Group Name]
            AND O.[Date Open] = C.[Date Closed]

答案 1 :(得分:0)

我看到关闭的日期对于打开和关闭都是相同的。这个查询建立在假设在两个票证关闭和打开的每一天都有一个条目的情况下构建。如果是这样,你可以进行连接像下面你可以做一个完整的加入

;with(opentickets)
as
(
SELECT g.Name As 'GroupName', COUNT(*) As 'TicketsOpen', CAST(t.Created AS DATE) As 'DateOpen' FROM Tickets t
JOIN Groups g ON g.ID = t.AssignedGroup
GROUP BY CAST(t.Created AS DATE), g.Name
)
,closedtickets
as
(
SELECT g.Name As 'GroupName' ,COUNT(*) As 'TicketsClosed', CAST(t.ResolvedAt AS DATE) As 'DateClosed' FROM Tickets t
JOIN Groups g ON g.ID = t.AssignedGroup
GROUP BY CAST(t.ResolvedAt AS DATE), g.Name
)
select 
c.GroupName,c.ticketsopen as ticketsopen,c.TicketsClosed as ticketsclosed,c.dateclosed
 from
opentickets o
 join
closedtickets c
on c.GroupName=o.GroupName
and o.dateopen=c.dateclosed

答案 2 :(得分:0)

简单的方法是将两个查询放在CTE中,然后加入它们:

;WITH [Open] AS (
    SELECT  g.Name As 'Group Name' ,
            COUNT(*) As 'Tickets Closed', 
            CAST(t.ResolvedAt AS DATE) As 'Date Closed',
            CAST(t.ResolvedAt AS DATE) as [oDate]
    FROM Tickets t
    JOIN Groups g ON g.ID = t.AssignedGroup
    GROUP BY CAST(t.ResolvedAt AS DATE), g.Name
), [Close] AS (
    SELECT  g.Name As 'Group Name', 
            COUNT(*) As 'Tickets Open', 
            CAST(t.Created AS DATE) As 'Date Open'
    FROM Tickets t
    JOIN Groups g ON g.ID = t.AssignedGroup
    GROUP BY CAST(t.Created AS DATE), g.Name
)

SELECT *
FROM [Open] o
LEFT JOIN [Close] c
    ON c.[Group Name] = o.[Group Name] 
ORDER BY [oDate] DESC

答案 3 :(得分:0)

我认为你的游戏在同一天开始和结束:

SELECT c.Group_Name, o.Tickets_Open, c.Tickets_Closed, c.Game_Date
FROM (
SELECT g.Name As Group_Name ,COUNT(*) As Tickets_Closed, CAST(t.ResolvedAt AS DATE) As Game_Date FROM Tickets t
JOIN Groups g ON g.ID = t.AssignedGroup
GROUP BY CAST(t.ResolvedAt AS DATE), g.Name) AS C
INNER JOIN (
    SELECT g.Name As Group_Name, COUNT(*) As Tickets_Open, CAST(t.Created AS DATE) As Game_Date FROM Tickets t
    JOIN Groups g ON g.ID = t.AssignedGroup
    GROUP BY CAST(t.Created AS DATE), g.Name
) as o ON c.Group_Name = o.Group_Name and c.Game_Date = o.Game_Date
ORDER BY o.Tickets_Open, c.Tickets_Closed, c.Group_Name

答案 4 :(得分:0)

    public partial class Project
{
    public Project()
    {
        this.Bugs = new HashSet<Bug>();
        this.Components = new HashSet<Component>();
        this.Users = new HashSet<User>();
    }

    public int ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string Status { get; set; }
    public Nullable<System.DateTime> CreationDate { get; set; }

    public virtual ICollection<Bug> Bugs { get; set; }
    public virtual ICollection<Component> Components { get; set; }
    public virtual ICollection<User> Users { get; set; }
}