2015-10-12 77 views
0

我想顯示的目標達到或沒有類別明智,我沒有得到正確的計數提交鏈接列,提交的鏈接計數出錯了。結果是乘以沒有分配的類別。實際提交鏈接15.但是其顯示15 * 4即60不正確的結果和計數進來sql查詢

我的樣本數據

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 

my output

輸出

回答

0

當您加入tbl_Category和Tbl_DaySubmission和tbl_Link時,您很可能會將某些ID多次計數。

我想你的情況要數相關的鏈接,你可以簡單地用

COUNT(DISTINCT LinkId) 

更換

COUNT(LinkId) 

,以獲得正確的號碼。