我想显示目标已达到或未达到类别明智,我没有在提交的链接列中获得正确的计数,提交的链接计数出错了。结果是没有分配的类别。实际提交的链接15.但它显示15 * 4,即60
我的样本数据
select CatId,DayName,DayTarget,ProjectID from Tbl_DaySubmission where ProjectID='3'and DayName='FRIDAY'
CatId DayName DayTarget ProjectID
35 Friday 5 3
3 Friday 4 3
6 Friday 5 3
2 Friday 15 3
select LinkId,LnkSubmsnDate,LnkSubmtdBy,ProjectId,CategoryId from tbl_Link where LnkSubmsnDate='2015-10-09'
LinkId LnkSubmsnDate LnkSubmtdBy ProjectId CategoryId
147950 2015-10-09 swapna 3 2
147951 2015-10-09 swapna 3 2
147952 2015-10-09 swapna 3 2
147953 2015-10-09 swapna 3 2
147954 2015-10-09 swapna 3 2
147955 2015-10-09 swapna 3 2
147956 2015-10-09 swapna 3 2
147957 2015-10-09 swapna 3 2
147958 2015-10-09 swapna 3 2
147959 2015-10-09 swapna 3 2
147960 2015-10-09 swapna 3 2
147961 2015-10-09 swapna 3 2
147962 2015-10-09 swapna 3 2
147963 2015-10-09 swapna 3 2
147964 2015-10-09 swapna 3 2
147965 2015-10-09 swapna 3 35
我的查询
SELECT
c.Categoryname
, ds.DayTarget
, CASE WHEN (COUNT(LinkId) > 0) THEN l.LnkSubmsnDate
ELSE NULL END AS [LnkSubmsnDate]
, CASE WHEN (COUNT(LinkId) > 0) THEN l.LnkSubmtdBy
ELSE NULL END AS [LnkSubmtdBy]
, COUNT(LinkId) AS submittedLinks
, (CASE
WHEN DayTarget=COUNT(LinkId) THEN 'Reached'
WHEN DayTarget-COUNT(LinkId) < 1 THEN 'Reached'
WHEN DayTarget-COUNT(LinkId) >= 1 THEN 'Not Reached'
END ) AS performance
FROM tbl_Category AS c
INNER JOIN Tbl_DaySubmission AS ds ON ds.CatId = c.CategoryId
LEFT JOIN (SELECT * FROM tbl_Link WHERE LnkSubmsnDate='2015-10-12' and ProjectId='109' and LnkSubmtdBy='swapna') AS l ON l.ProjectId = ds.ProjectID AND l.CategoryId = ds.CatId
WHERE ds.ProjectID = 109
GROUP BY c.Categoryname, ds.DayTarget, l.LnkSubmsnDate, l.LnkSubmtdBy
ORDER BY l.LnkSubmtdBy
我需要这样的输出
Categoryname DayTarget LnkSubmsnDate LnkSubmtdBy submittedLinks performance
Classifieds 15 2015-10-09 swapna 15 Reached
BlogCommenting 5 2015-10-09 swapna 1 Not Reached
ForumSites 4 Not Reached
Directorysites 5 Not Reached
答案 0 :(得分:0)
当你加入tbl_Category和Tbl_DaySubmission以及tbl_Link时,你很可能会多次计算一些ID。
我想在您想要计算关联链接的情况下,您可以简单地替换
COUNT(LinkId)
与
COUNT(DISTINCT LinkId)
获取正确的号码。