0

When ever I am using the below query :

SELECT  
    AC.iJobID as JobID,
    ROW_NUMBER()OVER (ORDER BY AC.iJobID) AS RowNumber,     
    (SELECT COUNT (*) FROM tbUS_ReferJob WHERE iJobId_FK = 202424 AND sReferredFrom = 'F' AND iUserId_FK = 9550) AS FaceBook_Applications
FROM tbUS_AffiliateJobCount AC,tbUS_ReferJob RJ
WHERE AC.iJobID = 202424 AND RJ.iJobId_FK = 202424
GROUP BY AC.iJobID

It is giving the correct result.

JobID   RowNumber   FaceBook_Applications
202424  1           2

But when I use the query below

SELECT
    AC.iJobID as JobID,
    ROW_NUMBER()OVER (ORDER BY AC.iJobID) AS RowNumber,     
    SUM(CASE WHEN RJ.sReferredFrom = 'F' THEN 1 ELSE 0 END) AS FaceBook_Applications
FROM tbUS_AffiliateJobCount AC,tbUS_ReferJob RJ
WHERE AC.iJobID = 202424 AND RJ.iJobId_FK = 202424
GROUP BY AC.iJobID

It is giving the incorrect result

JobID   RowNumber   FaceBook_Applications
202424  1           12

Now my questions are

  1. What is the reason behind it?
  2. How can I make it using SUM() function to lower the cost?

Any suggestion will be helpful.

Thanks for your time.

UPDATE

SELECT  AC.iJobID as JobID,
        ROW_NUMBER()OVER (ORDER BY AC.iJobID) AS RowNumber,     
        SUM(CASE WHEN (RJ.sReferredFrom = 'F' AND RJ.iJobId_FK = 202424 AND RJ.iUserId_FK = 9550) THEN 1 ELSE 0 END) AS FaceBook_Applications
FROM    tbUS_AffiliateJobCount AC,tbUS_ReferJob RJ
WHERE   AC.iJobID = 202424 AND RJ.iJobId_FK = 202424
GROUP BY AC.iJobID

It is giving :

JobID   RowNumber   FaceBook_Applications
202424  1            8

UPDATE 2

SELECT iJobID_FK, sReferredFrom FROM tbUS_ReferJob WHERE iUserID_FK=9550 AND iJobID_FK=202424 AND sReferredFrom='F'

Result:

iJobID_FK   sReferredFrom
202424          F
202424          F

3 Answers 3

1

Take your second query (I'll repeat it here):

SELECT
    AC.iJobID as JobID,
    ROW_NUMBER()OVER (ORDER BY AC.iJobID) AS RowNumber,     
    SUM(CASE WHEN RJ.sReferredFrom = 'F' THEN 1 ELSE 0 END) AS FaceBook_Applications
FROM tbUS_AffiliateJobCount AC,tbUS_ReferJob RJ
WHERE AC.iJobID = 202424 AND RJ.iJobId_FK = 202424
GROUP BY AC.iJobID

and remove (or comment out) all the grouping & aggregating (ranking too, as it will serve no purpose in what I'm suggesting), like this, for example:

SELECT
    AC.iJobID as JobID, RJ.iUserId_FK,
    ROW_NUMBER()OVER (ORDER BY AC.iJobID) AS RowNumber,
    SUM(CASE WHEN RJ.sReferredFrom = 'F' THEN 1 ELSE 0 END) AS FaceBook_Applications
FROM tbUS_AffiliateJobCount AC,tbUS_ReferJob RJ
WHERE AC.iJobID = 202424 AND RJ.iJobId_FK = 202424
GROUP BY AC.iJobID

Now run the resulting query and have a look at how many Fs it produces. My point is, the result of the join most likely contains many duplicates, because both tables' subsets return more than one row and that results in the good old Cartesian product. A solution might consist either in cleaning up the duplicates in the original tables, if appropriate, or in making the WHERE condition more specific.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Also strongly recommend not using the old-fashion implicit join; while this same scenario could occur with an explicit INNER JOIN, the old-stype table,table,table scenario exposes one more a chance for failure: it lets you forget to put any join criteria at all.
1

In your first query, you restrict the subquery with:

WHERE iUserId_FK = 9550

This restriction is missing in your second query. It should look like this:

SELECT  AC.iJobID as JobID,
        ROW_NUMBER()OVER (ORDER BY AC.iJobID) AS RowNumber,     
        SUM(CASE WHEN RJ.sReferredFrom = 'F' THEN 1 ELSE 0 END) AS FaceBook_Applications
FROM    tbUS_AffiliateJobCount AC,tbUS_ReferJob RJ
WHERE   AC.iJobID = RJ.iJobId_FK 
AND     AC.iJobID = 202424 
AND     RJ.iUserId_FK = 9550

3 Comments

It is giving the result 8. I mentioned in my updated answer. Can you please have a look at it.
@ARINDAM could you show us the result for this query: SELECT iJobID_FK, sReferredFrom FROM tbUS_ReferJob WHERE iUserID_FK=9550 AND iJobID_FK=202424 AND sReferredFrom=f
I updated my question (UPDATE2) and it is giving the correct result.
1

If i looked at it correctly, i think that you are missing a filter in the second query

AND iUserId_FK = 9550

The second query should be like the following:

SELECT
    AC.iJobID as JobID,
    ROW_NUMBER()OVER (ORDER BY AC.iJobID) AS RowNumber,     
    SUM(CASE WHEN RJ.sReferredFrom = 'F'  AND iUserId_FK = 9550 THEN 1 ELSE 0 END) AS FaceBook_Applications
FROM tbUS_AffiliateJobCount AC,tbUS_ReferJob RJ
WHERE AC.iJobID = 202424 AND RJ.iJobId_FK = 202424
GROUP BY AC.iJobID

EDIT

i think there could be some problem with the join condition... try to make a clearer join before sum

SELECT
    AC.iJobID as JobID,
    ROW_NUMBER()OVER (ORDER BY AC.iJobID) AS RowNumber,     
    SUM(CASE WHEN RJ.sReferredFrom = 'F'   THEN 1 ELSE 0 END) AS FaceBook_Applications
FROM tbUS_AffiliateJobCount AC
JOIN tbUS_ReferJob RJ
ON AC.iJobID =  RJ.iJobId_FK AND Rj.iUserId_FK = 9550
WHERE AC.iJobID = 202424
GROUP BY AC.iJobID

EDIT 2

SELECT
    AC.iJobID as JobID,
    ROW_NUMBER()OVER (ORDER BY AC.iJobID) AS RowNumber,     
    COUNT(*) AS FaceBook_Applications
FROM tbUS_AffiliateJobCount AC
JOIN tbUS_ReferJob RJ
ON AC.iJobID =  RJ.iJobId_FK AND Rj.iUserId_FK = 9550 AND RJ.sReferredFrom = 'F'
WHERE AC.iJobID = 202424
GROUP BY AC.iJobID

5 Comments

how many rows are there in tbUS_AffiliateJobCount with iJobID = 202424?
edit2 added also the 'F' filter to the join. This should fix it, i think
Same problem it returns the count as 8.
can you give the output of select * from tbUS_AffiliateJobCount where iJobId = 202424 ?
Ok! Then the problem is the JOIN (implicit in your query, explicit in mine). The program join every rows in the first table (4) with the rows in the second table (2) making a 4x2 table which return a count of 8. Performing the query on the second table should solve in the faster way, another way is to do a COUNT(DISTINCT [XXX]) instead of COUNT(*) where [XXX] is the key in your second table (the one with the 2 records to count)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.