COUNT在JOIN问题中添加了两次

时间:2014-05-03 23:17:21

标签: mysql join count

我正在开展项目,我的报告有三个状态,其中"已检查"," inprocess"并且"已完成"

有两张桌子!

tbl_tracking - 用于跟踪报告     

    report_id   usr_id   date        status
    ----------------------------------------------
    0000        abc      2014/04/05  checked
    0001        abc      2014/04/05  checked
    0000        abc      2014/04/05  inprocess
    0001        abc      2014/04/05  completed
    0002        abc      2014/04/06  completed
    0004        xyz      2014/04/05  checked
    0005        xyz      2014/04/06  checked
    

tbl_timestatus-用于跟踪员工处理报告的时间     

    usr_id   date        time_worked (hrs)
    ----------------------------------------------
    abc      2014/04/05  6
    abc      2014/04/06  5
    

现在我想创建一个视图,以便始终显示所有内容的最新状态,以便我对此进行编码     

    CREATE VIEW VW_STATUS AS
    SELECT tb1.usr_id, tb1.date,
    COUNT(CASE tb1.status WHEN 'checked' THEN 1 ELSE NULL END) AS checkedcount,
    COUNT(CASE tb1.status WHEN 'inprocess' THEN 1 ELSE NULL END) AS inprocesscount,
    COUNT(CASE tb1.status WHEN 'completed' THEN 1 ELSE NULL END) AS compltedcount,
    CASE WHEN (tb1.usr_id=tb2.usr_id AND tb1.date=tb2.date) THEN tb2.time_worked ELSE NULL END AS timeworked
    FROM tbl_tracking tb1, tbl_timestatus tb2
    GROUP BY tb1.usr_id, tb1.date;
    

预期产出:     

    usr_id   date        checkedcount   inprocesscount   completedcount   timeworked
    ---------------------------------------------------------------------------------------
    abc      2014/04/05  2              1                1                6
    abc      2014/04/06  0              0                1                5
    xyz      2014/04/05  1              0                0                NULL
    xyz      2014/04/06  1              0                0                NULL
    

实际输出:     

    usr_id   date        checkedcount   inprocesscount   completedcount   timeworked
    ---------------------------------------------------------------------------------------
    abc      2014/04/05  4              2                2                6
    abc      2014/04/06  0              0                2                5
    xyz      2014/04/05  2              0                0                NULL
    xyz      2014/04/06  2              0                0                NULL
    

虽然" timeworked"值保持正确,计数(*)自己加两次!!加入有一些问题!需要帮助...

2 个答案:

答案 0 :(得分:0)

在tb1.usr_id = tb2.usr_id AND tb1.date = tb2.date上加入tbl1和tb2。请注意,工作时间是正确的。

答案 1 :(得分:0)

你应该尝试如下。请在此处查看示例小提琴http://sqlfiddle.com/#!3/0b24c/6

CREATE VIEW VW_STATUS AS
SELECT tb1.usr_id, tb1.date,
COUNT(CASE tb1.status WHEN 'checked' THEN 1 ELSE NULL END) AS checkedcount,
COUNT(CASE tb1.status WHEN 'inprocess' THEN 1 ELSE NULL END) AS inprocesscount,
COUNT(CASE tb1.status WHEN 'completed' THEN 1 ELSE NULL END) AS compltedcount,
tb2.time_worked
FROM tbl_tracking tb1
LEFT JOIN tbl_timestatus tb2
   ON tb1.usr_id=tb2.usr_id 
    AND tb1.date=tb2.date   
    GROUP BY tb1.usr_id, tb1.date , tb2.time_worked
    ORDER BY tb1.usr_id;

结果是

enter image description here