简体   繁体   English

如何为以下结果表编写此表的查询

[英]How do I write the query for this table for the following resulting table

I am not the most experienced SQL user, and I was tasked with writing a SQL procedure, and I just would like some help in writing a SQL query that will help in returning the following resulting table.我不是最有经验的 SQL 用户,我的任务是编写 SQL 过程,我只是想在编写 SQL 查询方面得到一些帮助,这将有助于返回以下结果表。

The original tables, I will call them here, S and A.原始表,我在这里称它们为 S 和 A。

S looks like the following below, which are the columns: S 如下所示,它们是列:

"SetID, Name, OwnerID, Tag, ExpirationDate, CreatedOn, ModifiedOn" “SetID、名称、OwnerID、标记、到期日期、创建时间、修改时间”

A looks like the following below, which are the columns: A 如下所示,它们是列:

"AFID, AFSetID, TypeId, AFGroupID, CreatedOn, ModifiedOn" “AFID、AFSetID、TypeId、AFGroupID、CreatedOn、ModifiedOn”

Write now my query looks like the following below.现在写我的查询如下所示。 Don't mind the complicated Join statements and the Where Clause, they are correct, and there is nothing wrong there since that was old code.不要介意复杂的 Join 语句和 Where 子句,它们是正确的,并且没有任何问题,因为那是旧代码。

I need help I believe in my SELECT statement, and the Group By Clause is definitely wrong.我需要帮助我相信我的 SELECT 声明,Group By Clause 肯定是错误的。

        SELECT s.AFSetID, s.Tag, s.Name, s.ModifiedOn, a.AFGroupID, a.AFID
        FROM  dbo.TAvoidFavorSet s with (nolock, forceseek)
        INNER JOIN @owners o ON o.OwnerId = s.OwnerID
        LEFT OUTER JOIN TAvoidFavor a WITH(NOLOCK) ON a.AFSetID = s.AFSetID AND ((@primaryOnly = 1 AND (a.GridLevel = 0 OR a.PrimaryAFID IS NULL)) OR (@primaryOnly = 0))
        LEFT OUTER JOIN Lookup_AvoidFavorType t ON t.TypeID = a.TypeID   
        LEFT OUTER JOIN dbo.TAvoidFavorPLID p WITH(NOLOCK) ON p.AFID = a.AFID
        WHERE s.AFSetID = COALESCE(@afSetID, s.AFSetID)
        GROUP BY s.AFSetID, a.AFGroupID, s.ModifiedOn, s.Tag, s.Name, a.AFID

And the returned table looks something like this.返回的表看起来像这样。

AFSetID, Tag, Name, ModifiedOn, AFGroupID, AFID
1         x    A     blahblah     100       5
1         x    A     blahblah     200       15
1         x    A     blahblah     300       25
1         x    A     blahblah     400       35
1         x    A     blahblah     500       45
2         y    B     blahblah     150       16
2         y    B     blahblah     250       24
2         y    B     blahblah     250       543
2         y    B     blahblah     450       234
3         z    C     blahblah     425       23
3         z    C     blahblah     425       66
3         z    C     blahblah     322       97

I want a resulting table that looks something like the following below.我想要一个如下所示的结果表。 I want to group them by the AFsetID, and the count of the distinct AFGroupID per AFsetID should be in the AFDistinctGroupCount column.我想按 AFsetID 对它们进行分组,每个 AFsetID 的不同 AFGroupID 的计数应该在 AFDistinctGroupCount 列中。 And AFIDCount holds a non-distinct count of AFID per AFsetID. AFIDCount 为每个 AFsetID 保存一个非不同的 AFID 计数。

AFSetID, Tag, Name, ModifiedOn, AFDistinctGroupCount, AFIDCount
1         x    A     blah blah          5                 5
2         y    B     blah blah          3                 4
3         z    C     blahblah          2                 3

Anybody who wants to help I would appreciate it alot.任何想帮助我的人都会非常感激。 I don't believe it is just adding some COUNT operation in the select statement cause I tried various angles of that.我不相信它只是在 select 语句中添加了一些 COUNT 操作,因为我尝试了各种角度。 I think this requires a double select query, and that has me lost.我认为这需要双重 select 查询,这让我迷路了。

Thanks谢谢

You must remove a.AFGroupID and a.AFID from the GROUP BY clause and aggregate on them:您必须从 GROUP BY 子句中删除a.AFGroupIDa.AFID并聚合它们:

SELECT s.AFSetID, s.Tag, s.Name, s.ModifiedOn, 
       COUNT(DISTINCT a.AFGroupID) AFDistinctGroupCount, 
       COUNT(a.AFID) AFIDCount
FROM  dbo.TAvoidFavorSet s with (nolock, forceseek)
INNER JOIN @owners o ON o.OwnerId = s.OwnerID
LEFT OUTER JOIN TAvoidFavor a WITH(NOLOCK) ON a.AFSetID = s.AFSetID AND ((@primaryOnly = 1 AND (a.GridLevel = 0 OR a.PrimaryAFID IS NULL)) OR (@primaryOnly = 0))
LEFT OUTER JOIN Lookup_AvoidFavorType t ON t.TypeID = a.TypeID   
LEFT OUTER JOIN dbo.TAvoidFavorPLID p WITH(NOLOCK) ON p.AFID = a.AFID
WHERE s.AFSetID = COALESCE(@afSetID, s.AFSetID)
GROUP BY s.AFSetID, s.ModifiedOn, s.Tag, s.Name

I believe a COUNT(distinct a.AFGroupID) and COUNT(a.AFID) would make it work.我相信 COUNT(distinct a.AFGroupID) 和 COUNT(a.AFID) 会让它起作用。 See code below:请参阅下面的代码:

SELECT s.AFSetID, s.Tag, s.Name, s.ModifiedOn, 
COUNT(distint a.AFGroupID), COUNT(a.AFID)
    FROM  dbo.TAvoidFavorSet s with (nolock, forceseek)
    INNER JOIN @owners o ON o.OwnerId = s.OwnerID
    LEFT OUTER JOIN TAvoidFavor a WITH(NOLOCK) ON a.AFSetID = s.AFSetID AND ((@primaryOnly = 1 AND (a.GridLevel = 0 OR a.PrimaryAFID IS NULL)) OR (@primaryOnly = 0))
    LEFT OUTER JOIN Lookup_AvoidFavorType t ON t.TypeID = a.TypeID   
    LEFT OUTER JOIN dbo.TAvoidFavorPLID p WITH(NOLOCK) ON p.AFID = a.AFID
    WHERE s.AFSetID = COALESCE(@afSetID, s.AFSetID)
    GROUP BY s.AFSetID, s.ModifiedOn, s.Tag, s.Name

Since you want to count the AFID and AFGroupID, you do not want to group on them.由于您要计算 AFID 和 AFGroupID,因此您不想对它们进行分组。

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

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