I have 3 tables in database Task, User & assignedTo.
Task contains information of Task created by Admin, User is list of User & assignedTo contains information about which task assign to which user.
i need to query on 'assignedTo' table to show a list of Task assigned to users on their Dashboard. Problem is a single Task is assigned to multiple user and it may possible task may be completed by one of them. So i need to show task which are in Open Status and not completed by anyone in assigned list.
Below is structure of 'assignedTo' Table:
--------------------------------
| id | taskId | userId | status|
--------------------------------
| 1 | 1 | 1 | open |
| 2 | 1 | 2 | open |
| 3 | 1 | 3 | open |
| 4 | 1 | 4 | open |
| 5 | 2 | 1 | open |
| 6 | 2 | 2 | close |
| 7 | 3 | 1 | open |
| 8 | 3 | 2 | open |
--------------------------------
So as per above data, Task 1 is shown on user 1,2,3 & 4 Dashboard as it is not closed by any of them.
Task 2 is shown on no one's Dash as it is closed by User 2.
Similarly, Task 3 is shown on user 1 & 2 Dash as it is open for both of them.
i tried to join same table to it's own but failed to execute.
SELECT * FROM assignedTo a1
JOIN assignedTo a2 ON a2.taskId = a1.taskId AND a2.userId != 1 AND a2.status = 'open'
WHERE a1.userId = 1 AND a1.status = 'open'
Please help with Query if possible.
Best Answer
Fiddler: http://rextester.com/CAHOPS39734
Help from this post
The above query will return one extra column as
CurrentStatus
. If the taskId has anyclose
status, for those tasks theCurrentStatus
will beTask Completed
and for remaining tasks it will show asopen
only.So the output will be: