【问题标题】:How can I group table after inner join?内部连接后如何对表进行分组?
【发布时间】:2016-09-11 12:22:35
【问题描述】:

我使用的是 SQL Server 2012。

我有这张表叫InspectionReviews:

|Id  | SiteId| IsNormal|     DateReview            | ObjectId |FrequencyId|InspectItemId |
|3379|      5|  True   |    2016-09-08 00:00:00.000|    1019  |     1     |     16       |
|3380|      5|  True   |    2016-09-08 00:00:00.000|    1019  |     1     |     20       |
|3381|      5|  False  |    2016-09-08 00:00:00.000|    1020  |     1     |     16       |
|3382|      5|  True   |    2016-09-08 00:00:00.000|    1020  |     1     |     54       |

IsNormalbit 列。

而这张表叫DamageEvents

| Id | ExternalId | Subject | CMT           |    
| 1  |   3379     | damage5 | some comment7 |
| 2  |   3380     | damage3 | some comment3 | 
| 3  |   3382     | damage4 | some comment5 |
| 4  |   3381     | damage1 | some comment4 |

DamageEvents 表中的ExternalId 列是外键。

我在两个表之间写了一个内连接:

SELECT  
    InspectionReviews.Id, InspectionReviews.SiteId,
    InspectionReviews.IsNormal, InspectionReviews.DateReview,
    InspectionReviews.ObjectId, InspectionReviews.FrequencyId, 
    InspectionReviews.InspectItemId, 
    DamageEvents.ExternalId, DamageEvents.Subject, DamageEvents.CMT,
FROM    
    InspectionReviews 
INNER JOIN
    DamageEvents ON InspectionReviews.Id = DamageEvents.ExternalId

我得到的结果:

|Id  | SiteId| IsNormal|     DateReview            | ObjectId |FrequencyId|InspectItemId | ExternalId | Subject |     CMT       |
|3379|  5    |  True   |    2016-09-08 00:00:00.000|    1019  |     1     |     16       |    3379    | damage5 | some comment7 |
|3380|  5    |  True   |    2016-09-08 00:00:00.000|    1019  |     1     |     20       |    3380    | damage3 | some comment3 |
|3381|  5    |  False  |    2016-09-08 00:00:00.000|    1020  |     1     |     16       |    3381    | damage4 | some comment5 |
|3382|  5    |  True   |    2016-09-08 00:00:00.000|    1020  |     1     |     54       |    3382    | damage1 | some comment4 |

实现内部连接后,我需要通过ObjectId 进行分组。这是期望的结果:

|    Id   | SiteId| IsNormal|     DateReview           | ObjectId |FrequencyId|InspectItemId | Subject |     CMT                     |
|3379,3380|  5    |  True   |   2016-09-08 00:00:00.000|    1019  |     1     |     16,20    | damage5 | some comment7,some comment3 |
|3381,3382|  5    |  False  |   2016-09-08 00:00:00.000|    1020  |     1     |     16,54    | damage4 | some comment5,some comment4 |

我需要在ObjectId(内部连接)之后对上面的表进行分组,如果分组表中至少一行有IsNormal false,则它必须为 False。

我该如何实现它?

【问题讨论】:

    标签: sql-server tsql sql-server-2012 group-by


    【解决方案1】:

    你可以像这样使用查询:

    ; with cte as (
    SELECT  InspectionReviews.Id,
            InspectionReviews.SiteId,
            InspectionReviews.IsNormal,
            InspectionReviews.DateReview,
            InspectionReviews.ObjectId,                 
            InspectionReviews.FrequencyId, 
            InspectionReviews.InpectItemId, 
            DamageEvents.ExternalId, 
            DamageEvents.Subject,
            DamageEvents.CMT
    
    FROM    #inspectionreviews InspectionReviews INNER JOIN
            #damageevents DamageEvents ON InspectionReviews.Id = DamageEvents.ExternalId
    )
    select objectid, min(convert(int,IsNormal)) as IsNormal , stuff ((
    select ',' + convert(varchar(5),Id) from cte where objectid = t.objectid 
    for xml path('')
    ),1,1,'') as Id
    , stuff ((
    select ',' + convert(varchar(5),inpectitemid) from cte where objectid = t.objectid 
    for xml path('')
    ),1,1,'') as InspectItemId
    , stuff ((
    select ',' + subject from cte where objectid = t.objectid 
    for xml path('')
    ),1,1,'') as Subject
    , stuff ((
    select ',' + CMT from cte where objectid = t.objectid 
    for xml path('')
    ),1,1,'') as CMT,
    max(frequencyid) as FrequencyId,
    max(SiteId) as SiteId
    from cte t group by objectid
    

    【讨论】:

    • 谢谢 什么是-for xml path('')?
    • select ','+ colname from #Table1 for xml path('') 这个查询从 xml 中给出逗号分隔的值,并且东西用于剪切第一个逗号并选择其余所有
    【解决方案2】:

    愿此帮助-

    select
        ObjectId,
        Id = Stuff( (select ',' + convert(varchar(100), Id) 
                     from InspectionReviews     
                      where ObjectId = ir.ObjectId for xml path('')), 1, 1, ''),
        IsNormal = Min(convert(int, IsNormal))
    from
        InspectionReviews ir
    group by
        ObjectId
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-13
      • 2021-05-20
      • 2017-11-14
      • 1970-01-01
      • 2016-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多