我一直致力于查询: 业务逻辑 - 显示在指定日期范围内参加健身房的所有成员(从下拉列表中选择)。
遇到的问题 - 我意识到结果显示重复,但请你帮忙。
以下是我使用的查询。
Declare @StartDate datetime = '29 May 2014'
Declare @EndDate datetime = '29 May 2014'
DEclare @SiteID INT =14
Declare @StartTime datetime = '05:00 AM'
Declare @EndTime datetime = '10:00 PM'
Declare @Start datetime = cast (@StartDate + ' '+ @StartTime as datetime)
Declare @End datetime = cast(@EndDate + ' ' + @EndTime as datetime )
SELECT isnull(atc.totalaccepted,0) TotalAcceptedVisits,ISNULL(att.TotalOverrideVisits, 0) AS TotalOverrides, isnull(att1.TotalOverrideDenieds,0) as TotalDenies, ast.Name as VisitiedSite, c.FirstName + ' ' + c.LastName as Name, md.MemRefNo
FROM Contacts c
inner JOIN Attendance a on a.ContactGUID = c.GUID
inner JOIN MemberDetail md on md.ContactGUID = c.GUID
inner JOIN Sites ast on a.SiteID = ast.ID
OUTER APPLY
(
SELECT a1.contactguid,COUNT(*) totalaccepted
FROM Attendance a1
WHERE a1.contactguid = c.guid
AND (a1.IsSwipeSuccessful = 1)
AND a1.accessoverridereasonid IS NULL
AND a1.AttendDate BETWEEN @Start AND @End
group by a1.contactguid
)atc
outer APPLY
(
SELECT a2.contactguid, COUNT(*) TotalOverrideVisits
FROM Attendance a2
WHERE a2.contactguid = c.guid
And (a.IsSwipeSuccessful = 1)
AND a2.accessoverridereasonid IS NOT NULL
AND a2.AttendDate BETWEEN @Start AND @End
and (a2.SiteID = @SiteID OR @SiteID = 0)
group by a2.contactguid
) att
OUTER APPLY
(
SELECT a3.contactguid,COUNT(*) TotalOverrideDenieds
FROM Attendance a3
WHERE a3.contactguid = c.guid
AND a3.IsSwipeSuccessful = 0
AND a3.accessoverridereasonid IS NULL
AND a3.AttendDate BETWEEN @Start AND @end
group by a3.contactguid
) att1
where (a.SiteID = @SiteID OR @SiteID = 0)
and a.AttendDate BETWEEN @Start AND @End
order by md.MemRefNo
结果:正如您在下面看到的那样,有成员会重复自己,我只需要看到每个成员中有一行的总访问次数(如果有的话),如果有的话会覆盖,如果有则拒绝访问。
TotalAcceptedVisits TotalOverrides TotalDenies VisitiedSite Name memRefNo
1 0 0 Groblersdal Jean T G0030
1 1 1 Groblersdal Koky Bakkes G0032
1 0 1 Groblersdal Koky Bakkes G0032
1 1 1 Groblersdal Koky Bakkes G0032
1 0 0 Groblersdal Naomi Fisher G0035
1 0 0 Groblersdal Arthur Bart G0089
1 0 0 Groblersdal Tulinda Swi G0034
1 1 0 Groblersdal Devon Mooi G0008
1 1 0 Groblersdal Devon Mooi G0008
答案 0 :(得分:0)
我认为你需要这样的东西:
Declare @StartDate datetime = '29 May 2014'
Declare @EndDate datetime = '29 May 2014'
DEclare @SiteID INT =14
Declare @StartTime datetime = '05:00 AM'
Declare @EndTime datetime = '10:00 PM'
Declare @Start datetime = cast (@StartDate + ' '+ @StartTime as datetime)
Declare @End datetime = cast(@EndDate + ' ' + @EndTime as datetime )
SELECT CASE WHEN TotalAcceptedVisits > 0 THEN 1 ELSE 0 END AS TotalAcceptedVisits,
CASE WHEN TotalOverrides > 0 THEN 1 ELSE 0 END AS TotalOverrides,
CASE WHEN TotalDenies > 0 THEN 1 ELSE 0 END AS TotalDenies,
VisitiedSite, Name, MemRefNo
FROM
(SELECT SUM(isnull(atc.totalaccepted,0)) TotalAcceptedVisits, SUM(ISNULL(att.TotalOverrideVisits, 0)) AS TotalOverrides, SUM(isnull(att1.TotalOverrideDenieds,0)) as TotalDenies, ast.Name as VisitiedSite, c.FirstName + ' ' + c.LastName as Name, md.MemRefNo
FROM Contacts c
inner JOIN Attendance a on a.ContactGUID = c.GUID
inner JOIN MemberDetail md on md.ContactGUID = c.GUID
inner JOIN Sites ast on a.SiteID = ast.ID
OUTER APPLY
(
SELECT a1.contactguid,COUNT(*) totalaccepted
FROM Attendance a1
WHERE a1.contactguid = c.guid
AND (a1.IsSwipeSuccessful = 1)
AND a1.accessoverridereasonid IS NULL
AND a1.AttendDate BETWEEN @Start AND @End
group by a1.contactguid
)atc
outer APPLY
(
SELECT a2.contactguid, COUNT(*) TotalOverrideVisits
FROM Attendance a2
WHERE a2.contactguid = c.guid
And (a.IsSwipeSuccessful = 1)
AND a2.accessoverridereasonid IS NOT NULL
AND a2.AttendDate BETWEEN @Start AND @End
and (a2.SiteID = @SiteID OR @SiteID = 0)
group by a2.contactguid
) att
OUTER APPLY
(
SELECT a3.contactguid,COUNT(*) TotalOverrideDenieds
FROM Attendance a3
WHERE a3.contactguid = c.guid
AND a3.IsSwipeSuccessful = 0
AND a3.accessoverridereasonid IS NULL
AND a3.AttendDate BETWEEN @Start AND @end
group by a3.contactguid
) att1
where (a.SiteID = @SiteID OR @SiteID = 0)
and a.AttendDate BETWEEN @Start AND @End
GROUP BY atc.totalaccepted, att.TotalOverrideVisits, att1.TotalOverrideDenieds, ast.Name, c.FirstName + ' ' + c.LastName, md.MemRefNo) TB
ORDER BY MemRefNo
因为,您不想要值的SUM,那么您可以执行上述操作,基本上将SELECT作为子查询嵌入,然后使用CASE语句查看Total Values是否大于0,如果是这样,则返回1,否则为0。