2

I have a question regarding a ways of forming query in MySQL 5.7.

First of all here is the table structure and sample data:


create table m_user
(
    id bigint(10) auto_increment primary key,
    deleted tinyint(1) default 0 not null,
    suspended tinyint(1) default 0 not null,
    username varchar(100) default '' not null,
    password varchar(255) default '' not null,
    idnumber varchar(255) default '' not null,
    firstname varchar(100) default '' not null,
    lastname varchar(100) default '' not null,
    email varchar(100) default '' not null,
    timecreated bigint(10) default 0 not null,
    timemodified bigint(10) default 0 not null
);

INSERT INTO m_user (username, firstname, lastname, email, timecreated) VALUES('user1', 'John', 'Doe', '[email protected]', UNIX_TIMESTAMP());
INSERT INTO m_user (username, firstname, lastname, email, timecreated) VALUES('user2', 'Jane', 'Doe', '[email protected]', UNIX_TIMESTAMP());

create table m_workflow
(
    id bigint(10) auto_increment primary key,
    name varchar(255) default '' not null,
    timecreated bigint(10) default 0 not null,
    timemodified bigint(10) default 0 not null
);

INSERT INTO m_workflow (name, timecreated) VALUES('nr1', UNIX_TIMESTAMP());
INSERT INTO m_workflow (name, timecreated) VALUES('nr2', UNIX_TIMESTAMP());

create table m_workflow_questions
(
    id bigint(10) auto_increment primary key,
    workflowid bigint(10) not null,
    title varchar(255) default '' not null,
    timecreated bigint(10) default 0 not null,
    timemodified bigint(10) default 0 not null
);

INSERT INTO m_workflow_questions (workflowid, title, timecreated) VALUES(1, 'Q1', UNIX_TIMESTAMP());
INSERT INTO m_workflow_questions (workflowid, title, timecreated) VALUES(1, 'Q2', UNIX_TIMESTAMP());
INSERT INTO m_workflow_questions (workflowid, title, timecreated) VALUES(1, 'Q3', UNIX_TIMESTAMP());

create table m_workflow_answers
(
    id bigint(10) auto_increment primary key,
    workflowid bigint(10) default 0 not null,
    questionid bigint(10) default 0 not null,
    answer longtext not null,
    value bigint(10) default 0 not null,
    timecreated bigint(10) default 0 not null,
    timemodified bigint(10) default 0 not null
);

INSERT INTO m_workflow_answers (workflowid, questionid, answer, timecreated) VALUES(1, 1, 'a1', UNIX_TIMESTAMP());
INSERT INTO m_workflow_answers (workflowid, questionid, answer, timecreated) VALUES(1, 1, 'b1', UNIX_TIMESTAMP());
INSERT INTO m_workflow_answers (workflowid, questionid, answer, timecreated) VALUES(1, 1, 'c1', UNIX_TIMESTAMP());

INSERT INTO m_workflow_answers (workflowid, questionid, answer, timecreated) VALUES(1, 2, 'd1', UNIX_TIMESTAMP());
INSERT INTO m_workflow_answers (workflowid, questionid, answer, timecreated) VALUES(1, 2, 'f1', UNIX_TIMESTAMP());

INSERT INTO m_workflow_answers (workflowid, questionid, answer, timecreated) VALUES(1, 3, 'k1', UNIX_TIMESTAMP());
INSERT INTO m_workflow_answers (workflowid, questionid, answer, timecreated) VALUES(1, 3, 'j1', UNIX_TIMESTAMP());

create table m_workflow_responses
(
    id bigint(10) auto_increment primary key,
    questionid bigint(10) default 0 not null,
    userid bigint(10) default 0 not null,
    answerid bigint(10) default 0 not null,
    timecreated bigint(10) default 0 not null,
    timemodified bigint(10) default 0 not null
);

INSERT INTO m_workflow_responses (questionid, userid, answerid, timecreated) VALUES(1, 1, 1, UNIX_TIMESTAMP());
INSERT INTO m_workflow_responses (questionid, userid, answerid, timecreated) VALUES(2, 1, 2, UNIX_TIMESTAMP());

INSERT INTO m_workflow_responses (questionid, userid, answerid, timecreated) VALUES(1, 2, 2, UNIX_TIMESTAMP());
INSERT INTO m_workflow_responses (questionid, userid, answerid, timecreated) VALUES(2, 2, 3, UNIX_TIMESTAMP());

We create here several tables - m_user, m_workflow, m_workflow_questions, m_workflow_answers, m_workflow_responses

There is a workflow that can have one or more questions. Each question can have at least 2 or more defined answers. Users can respond to questions by choosing one ore more offered answers for each questions. This is kind of multiple choice type of test. Each of these user choices is stored in m_workflow_responses table.

What I need to do now is to present a simple list of all users and optionally their status in specific workflow.

Example:

User      Workflow Status Started   Finished
John Doe  Started         12/12/18  --
Jane Doe  Not started     --        --
Jimmy Doe Completed       11/12/18  12/12/18 

The problem I'm having is related to the way's of determining if specific question is to be considered as completed.

My idea here is to count number of questions and number of distinct responses of a user to a question and compare that.

This is what I have but it is not correct. It does not provide correct count of available questions in workflow

   SELECT   mq.workflowid
          , COUNT(mq.id) AS mqcount
          , mr.userid
          , SUM(CASE WHEN mr.userid IS NOT NULL THEN 1 END) AS mrcount
     FROM m_workflow_questions mq
LEFT JOIN (
      SELECT mra.questionid, mra.userid
        FROM m_workflow_responses mra
    GROUP BY mra.questionid, mra.userid
         ) mr ON mr.questionid = mq.id
 GROUP BY mq.workflowid, mr.userid

Here is the SQL fiddle with the same table structure and test data:

http://sqlfiddle.com/#!9/1d6db3/2

Notice: SQL fiddle uses MySQL 5.6 but I provided the exact SQL_MODE setting to simulate the behavior.

Further clarification:

My main issue was in way of determining the completion of workflow. Completion of workflow for a user happens when he/she responds to all questions with at least one response. So what I need is to count the number of questions responded not number of given responses in total since one question can have multiple responses from one user. This is the way I would like to calculate number of responded questions for a user in workflow:

SELECT COUNT(DISTINCT(mq.id)) AS mrcount
  FROM m_workflow_questions mq
  JOIN m_workflow_responses mr ON mr.questionid = mq.id
 WHERE mr.userid = 3 AND mq.workflowid = 1
5
  • Thank you so much for writing a question with a SQLFiddle and sample data! Is the "example" result you show what you hope to get from the sample data your provide? Apr 25, 2019 at 22:07
  • Yes, that is the essence of my question. Apr 25, 2019 at 22:13
  • It seems your m_workflow_responses table should have a workflowid column, otherwise how will you know which response belongs to which workflow?
    – Nick
    Apr 25, 2019 at 23:19
  • That can be resolved through question table Apr 25, 2019 at 23:21
  • 1
    I've been playing with this - not quite ready to submit as an answer. It would really help if your SQLFiddle had data matching your expected results. I've added "Jimmy", but the other two records appear to have the same answers. Apr 26, 2019 at 12:16

2 Answers 2

2

This is work in progress - it may be enough to show you how to complete the question, but it's not quite there yet.

select  u.id,
        u.firstname,
        u.lastname,
        w.name,
        min(wr_start.timecreated),
        max(wr_end.timecreated),
        (select count(*) 
         from m_workflow_questions
         where workflowid = w.id) as total_questions,
         (select count(*)
          from m_workflow_responses
             where userid = u.id) as answered_questions

from    m_user u
left outer join m_workflow_responses wr_start
  on u.id = wr_start.userid
left outer join m_workflow_answers wa
  on wr_start.questionid = wa.questionid
left outer join m_workflow w on w.id = wa.workflowid
left outer join m_workflow_responses wr_end
  on u.id = wr_end.userid
group by u.id,
        u.firstname,
        u.lastname,
        w.name,
        w.id
1
  • This is not bad though not entirely what I need. The issue with this query is that it counts all available questions and all available answers for them. As I said before a question can have more than one response. My problem with all this is that I was never able to flatten responses correctly i.e. any number of responses to a question by user should count as "question is answered" and count as 1. Apr 26, 2019 at 13:47
0

Thanks to the very helpful ideas from Neville Kuyt I did following things:

  • Added workflowid to the m_workflow_responses table
  • Reworked the base query

Now it looks like this:

SELECT t.id, t.userid, (t.mqcount = t.mrcount) AS complete , t.started, t.completed
FROM (
         SELECT m.id
              , mr.userid
              , (SELECT COUNT(*) FROM m_workflow_questions mq1 WHERE mq1.workflowid = m.id) AS mqcount
              , COUNT(DISTINCT (mr.questionid))                                             AS mrcount
              , (SELECT MIN(mr1.timecreated) FROM m_workflow_responses mr1 WHERE mr1.userid = mr.userid AND mr1.workflowid = m.id) AS started
              , (SELECT MAX(mr2.timecreated) FROM m_workflow_responses mr2 WHERE mr2.userid = mr.userid AND mr2.workflowid = m.id) AS completed
         FROM m_workflow m
         JOIN m_workflow_questions mq ON mq.workflowid = m.id
    LEFT JOIN m_workflow_responses mr ON mr.questionid = mq.id
        WHERE mr.userid IS NOT NULL
     GROUP BY m.id, mr.userid
     ) t
;

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.