0

I have a table structure as shown in following image. Basically, it has users ids lke u1, u2 who will be giving exams like E1, E2 etc on given date at a given exam center/venue in given shift like shift 1, shift 2 etc. There will be maximum 4 shifts in a day with shifts nos. from 1 to 4.

Given this table, I need to find out rows where same user is giving exam on same date and center but in continuous shifts. e.g Red rows highlighed in image where U1 is giving two exams in same center but in continuous shifts i.e. 1 and 2. It can happen that user is giving at max 4 exams in one day in 4 consecutive shifts.

Another query i am trying to write is where, if the same user is giving two or more exams on same date but in different centers. Rows highlighted in blue.

enter image description here

4
  • Do you need to check for scheduling conflicts, i.e. two exams scheduled for the same time? Commented Apr 8, 2017 at 4:11
  • yes, basically idea is we need to check if a user is giving multiple exams in a single day we want to give him the same center so that he does not have to run between centers. Then again, if the user is giving multiple exams in a same day and at same center also, if possible we need to give him break between shifts i.e. if he is giving exam in shift 1 and shift 2 , we want to reallocate to shift 3 or shift 4. SO we need this report Commented Apr 8, 2017 at 4:13
  • Am I correct in assuming that Sr.No (which I recommend changing to srNum) is the primary key? Commented Apr 8, 2017 at 4:18
  • yeah its just the primary key. Commented Apr 8, 2017 at 4:19

1 Answer 1

1

Please try...

SELECT tblExams.srNum AS srNum,
       tblExams.user_id AS user_id,
       tblExams.exam_id AS exam_id,
       tblExams.exam_date AS exam_date,
       tblExams.exam_center_id AS exam_center_id,
       tblExams.exam_shift as exam_shift
FROM tblExams
JOIN
{
    SELECT user_id AS user_id,
           exam_date AS exam_date,
           exam_center_id AS exam_center_id,
           COUNT( exam_center_id ) AS exam_center_id_count
    FROM tblExams
    GROUP BY user_id,
             exam_date,
             exam_center_id
} exam_center_counter ON tblExams.user_id = exam_center_counter.user_id
                     AND tblExams.exam_date = exam_center_counter.exam_date
                     AND tblExams.exam_center_id = exam_center_counter.exam_center_id
WHERE exam_center_counter.exam_center_id_count >= 2;

The way I have interpreted your question you are asking for records where the user has 2 or more exams in the same center on the same date. This suggested to me that the User was the most dominant factor, and within that the Exam Date, and within that the Centers and the count of Exams. Thus the GROUP BY and COUNT() lines from my inner SELECT statement.

The other three selected fields are there partly because the GROUP BY uses them and thus requires them to be part of the SELECT and partly because they are needed to form the INNER JOIN with tblExams (the name that I have assumed for your table of data). (Note : Where the word JOIN is not preceded by a join type then an INNER JOIN is performed).

The INNER JOIN has the effect here of tacking on the count of exams taking place in that Exam Center on that Date for that User onto the corresponding row(s).

Then all we need to do is select all the fields from each row from tblExams where that count is at least 2.

A variation on this logic was used when I constructed the following code for your second query...

SELECT tblExams.srNum AS srNum,
       tblExams.user_id AS user_id,
       tblExams.exam_id AS exam_id,
       tblExams.exam_date AS exam_date,
       tblExams.exam_center_id AS exam_center_id,
       tblExams.exam_shift as exam_shift
FROM tblExams
JOIN
{
    SELECT user_id AS user_id,
           exam_date AS exam_date,
           COUNT( exam_center_id ) AS exam_center_count
    FROM
    {
        SELECT user_id AS user_id,
               exam_date AS exam_date,
               exam_center_id AS exam_center_id
        FROM tblExams
        GROUP BY user_id,
                 exam_date,
                 exam_center_id
    } exam_center_id_grouper
    GROUP BY user_id,
             exam_date
} exam_center_counter ON tblExams.user_id = exam_center_counter.user_id
                     AND tblExams.exam_date = exam_center_counter.exam_date
WHERE exam_center_counter.exam_center_count >= 2;

In this query I use the inner-most SELECT statement to get a list of Exam Centers that are attended by each User on various Dates.

This data is then used by the middle-most SELECT statement to form a list of how many Centers that each User attends for an Exam on each DATE they do so.

This count is used by the outer-most SELECT to return only those rows from tblExams that meet the criteria specified.

If you have any questions or comments, then please feel free to post a Comment accordingly.

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

10 Comments

I have posted an Answer that contains the first query. The second query shall follow shortly...
I have added the second query and am now working on the explanation.
I have added an explanation of the reasoning that I used.
the 2nd query gives me only one row in result. Its not returning second row if there are two exams on same date but in different centers.
Both of your queries work at my local system. When I run the same on the staging server, it gives me error "View's SELECT contains a subquery in the FROM clause". Any ideas??
|

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.