0

我的查询:

SELECT tp.EMPCODE,
       tp.NAME,
       tp.DEPARTMENT, 
       Group_Concat(d.dte) AbsentDate, 
       COUNT(tp.EMPCODE) Totalnoofabsentdates
FROM test_prefixmaster tp
JOIN daterange25 d
LEFT JOIN test_prefixtransactions tpt 
       ON (tp.EMPCODE = tpt.empcode) AND DATE(S_DateTime) = d.dte
WHERE tpt.empcode IS NULL
GROUP BY tp.EMPCODE;   

我有一串这样的记录

Empcode         AbsentDate

111             2012-11-12,2012-11-23,2012-12-22

我的要求是显示如下记录:

Empcode          AbsentDate       
111              2012-11-12
                 2012-11-23
                 2012-12-22  

如何在 MySql 中显示记录?

注意:在我的查询中,如果我删除 Group_Concat 函数,则会显示 2012-11-12 记录(跳过 2012-11-23、2012-12-22)

4

1 回答 1

1

我会试一试,但您缺少一些信息 - 请阅读我的内联评论:

SELECT
    tp.EMPCODE,
    tp.NAME,
    tp.DEPARTMENT,
    d.dte AbsentDate
FROM
    test_prefixmaster tp
JOIN
    daterange25 d
    ON (tp.[foreignKey] = d.[someKey]) --You need to specify this...
LEFT JOIN
    test_prefixtransactions tpt
    ON (tp.EMPCODE = tpt.empcode)
    AND DATE(S_DateTime) = d.dte --Where is S_DateTime from?
WHERE
    tpt.empcode IS NULL;

如果您不想EMPCODE在每一行中重复该值,您可以这样做,但我不确定您为什么要在 SQL 中这样做:

CREATE TABLE tempTable AS (
SELECT
    tp.EMPCODE,
    d.dte AbsentDate
INTO
    #tempTable
FROM
    test_prefixmaster tp
JOIN
    daterange25 d
    ON (tp.[foreignKey] = d.[someKey]) --You need to specify this...
LEFT JOIN
    test_prefixtransactions tpt
    ON (tp.EMPCODE = tpt.empcode)
    AND DATE(S_DateTime) = d.dte --Where is S_DateTime from?
WHERE
    tpt.empcode IS NULL;
);

SELECT
    CASE WHEN COUNT(b.AbsentDate) = 1 THEN a.EMPCODE ELSE '' END AS EMPCode,
    a.AbsentDate
FROM
    tempTable a
LEFT JOIN
    tempTable b
    ON  (a.EMPCODE = b.EMPCODE)
    AND (a.AbesentDate >= b.AbesentDate)
GROUP BY
    a.EMPCODE,
    a.AbsentDate
ORDER BY
    1,2;
于 2013-01-07T06:51:55.590 回答