我正在使用SQL Server 2012。
我有一个名为|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 |
的表:
IsNormal
bit
是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 |
:
ExternalId
DamageEvents
表中的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
之后对(上连接)上面的表进行分组,如果在分组表中至少有一行s = "bobobobobobsdfsdfbob"
count = s.count("bob")
print(count)
为false,则它必须为False。
我该如何实施?
答案 0 :(得分: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
答案 1 :(得分:0)
愿这个帮助 -
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