0

I have data in two tables that i need to join and return the count of occurences of a record in one of the tables,

The data in Employee table looks like,

empId  workpatternId
    1        20

The data in the workPattern tables looks like,

workpatternId  monday tuesday wednesday thursday friday saturday sunday
   20           ALL     ALL     ALL       ALL     NULL   NULL     ALL 

The following query should return 5, which is the count of ALL, but returns 7 instead,

SELECT empId,b.workingPatternId, COUNT(monday='ALL') +
 COUNT(tuesday='ALL') + COUNT(wednesday='ALL')+ COUNT(thursday='ALL') + 
    COUNT(friday='ALL')+ COUNT(saturday='ALL')+ COUNT(sunday='ALL') AS COUNT
      FROM workPattern b 
 join Employee e on (e.workpatternId = b.workpatternId) and e.empId = 1
         GROUP BY empId ;

what is wrong with the query?

EDIT

dbfiddle

5
  • Seriously consider revising your schema. A database table is NOT a spreadsheet Commented Oct 20, 2020 at 8:46
  • 1
    You can't use COUNT for this. Use SUM with ISNULL function instead. Commented Oct 20, 2020 at 8:48
  • @Tajni NULL in this case is text Commented Oct 20, 2020 at 8:59
  • Check this answer: stackoverflow.com/questions/8732036/… Commented Oct 20, 2020 at 9:13
  • @Tajni replacing the count with sum seems to have resolved it Commented Oct 20, 2020 at 9:14

2 Answers 2

1

I don't see the point for aggregation, since it seems you have just one row in workPattern per empId. You could just write this as:

SELECT e.empId, wp.workingPatternId, 
      (wp.monday    = 'ALL') 
    + (wp.tuesday   = 'ALL') 
    + (wp.wednesday = 'ALL') 
    + (wp.thursday  = 'ALL') 
    + (wp.friday    = 'ALL') 
    + (wp.saturday  = 'ALL') 
    + (wp.sunday    = 'ALL') cnt
FROM workPattern wp
INNER Employee e on e.workpatternId = wp.workpatternId 
WHERE e.empId = 1

If you need aggregation for some reason, then you want sum() rather than count(): the latter counts all non-null values, while a false condition is evaluated as 0 (which is not null, so it is taken into account in your query):

SELECT 
    SUM(
          (wp.monday    = 'ALL') 
        + (wp.tuesday   = 'ALL') 
        + (wp.wednesday = 'ALL') 
        + (wp.thursday  = 'ALL') 
        + (wp.friday    = 'ALL') 
        + (wp.saturday  = 'ALL') 
        + (wp.sunday    = 'ALL')
    ) cnt
FROM workPattern wp
INNER JOIN Employee e on e.workpatternId = wp.workpatternId 
WHERE e.empId = 1
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you. The data i provided is a snippet, can't possibly post all due to the volume, so yes aggregation is needed.
I think you need to edit the aggregation sql. cnt is showing 1, should be 5
@Bisoux: I don't see how the second query can return 1. It returns a value that is equal to, or greater than, the value returned by the first query.
I tested it and it returns 1. The first and second query are not directly related. I have posted what worked as an answer.
@Bisoux: ah yes, you were right... The conditions need to be surrounded with parentheses. I fixed the answer. I should have spotted that by myself in the first place, but I learnt something here!
|
0

This worked:

 SELECT empId,b.workingPatternId, sum(Monday='ALL') + sum(Tuesday='ALL') + sum(Wednesday='ALL')+ sum(Thursday='ALL') + sum(Friday='ALL') + sum(Saturday='ALL')+ sum(Sunday='ALL') AS COUNT
 FROM WorkPatterns b 
 JOIN Employee e 
        ON (e.workingPatternId = b.workingPatternId) 
        AND e.empId = 1   
 GROUP BY empId ;

Comments

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.