Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
i have 2 query one to display and 1 to count , im getting wrong count in 2nd query
-- query of grid 1
SQL
SELECT distinct(k.keyword), p.projectname,pa.username,a.currentposition AS oldposition, b.currentposition AS newposition,pa.KeywordStatus as TimeLineStatus,k.targetdate,k.positionExp 
FROM   seo.tbl_keywordposition a
 JOIN   seo.tbl_keywordposition b ON a.keywordid = b.keywordid AND  a.psnupdatedate = '1/22/2015' AND b.psnupdatedate = '1/23/2015'  left join tbl_keywords k on k.keywordid= a.keywordid and a.keywordid = b.keywordid 
 left join tbl_project p  on p.ProjectId=k.ProjId left join tbl_projAssignment pa on pa.ProjId=p.ProjectId and pa.KeywordID=k.keywordid where  p.Projectname='Homeocare' order by pa.username


--query to count in next grid
SQL
SELECT 
    USERNAME 
    , SUM(IMPROVED) IMPROVED
    , SUM(DECREASED) DECREASED
     , SUM(NoChange) NoChange
    , SUM(TimeLineCOMPLETED) TimeLineCOMPLETED
      , SUM(TLNotCompleted) TLNotCompleted
FROM 
(SELECT (k.keyword)
    ,p.projectname
    ,pa.username
    ,CASE 
    
        WHEN a.currentposition > b.currentposition
            THEN 1
        ELSE 0
        END IMPROVED
    ,CASE 
        WHEN b.currentposition > a.currentposition
            THEN 1
        ELSE 0
        END DECREASED
           ,CASE 
        WHEN a.currentposition = b.currentposition
            THEN 1
        ELSE 0
        END NoChange
    ,CASE 
        WHEN pa.KeywordStatus = 'Stopped'
            THEN 1
        ELSE 0
        END TimeLineCOMPLETED
         ,CASE 
        WHEN pa.KeywordStatus = 'InProgress'
            THEN 1
        ELSE 0
        END TLNotCompleted
    ,a.currentposition AS oldposition
    ,b.currentposition AS newposition
    ,pa.KeywordStatus AS TimeLineStatus
    ,k.targetdate
    ,k.positionExp
FROM seo.tbl_keywordposition a
JOIN seo.tbl_keywordposition b ON a.keywordid = b.keywordid
    AND a.psnupdatedate = '1/22/2015'
    AND b.psnupdatedate = '1/23/2015'
LEFT JOIN tbl_keywords k ON k.keywordid = a.keywordid
    AND a.keywordid = b.keywordid
LEFT JOIN tbl_project p ON p.ProjectId = k.ProjId
LEFT JOIN tbl_projAssignment pa ON pa.ProjId = p.ProjectId
        AND pa.KeywordID = k.keywordid
    WHERE p.Projectname = 'Homeocare')INNERQUERY 
GROUP by USERNAME


screen shot of my error
http://postimg.org/image/ljq77xqvb/[^]
Posted
Updated 28-Jan-15 21:08pm
v2
Comments
Umer Akram 29-Jan-15 5:06am    
Once suggestion, you should not change the expression in a case which can cause confusion:

IF A > B THEN 'yes'

instead of using B > A

you should be using something like this:

IF A < B THEN 'No'.

for the said issue, share sample data for table tbl_keywordposition and share your desired output. as
W Balboos, GHB 29-Jan-15 14:38pm    
Since you're using LEFT JOINS, one presumes that in your data set their are items without matches: what I don't see is your handling of the failed matches (such as with ISNULL( ). Not handling NULL's can often give one some unexpected results

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900