我有一个表格,其中包含“代码”值变化的历史记录。每个月这个表都会获得一个新记录,其中包含指定月份的“代码”的新值。
+----------+------------+------------+------+
| Employee | FromDate | ToDate | Code |
+----------+------------+------------+------+
| Employee | 01/07/2016 | 31/07/2016 | 4 |
| Employee | 01/06/2016 | 30/06/2016 | 2 |
| Employee | 01/05/2016 | 31/05/2016 | 2 |
| Employee | 01/04/2016 | 30/04/2016 | 3 |
| Employee | 01/03/2016 | 31/03/2016 | 3 |
| Employee | 01/02/2016 | 29/02/2016 | 4 |
| Employee | 01/01/2016 | 31/01/2016 | 4 |
+----------+------------+------------+------+
我需要按此数据进行分组,以便每次“代码”更改时获取新记录,并获取“起始日期”的最小值和“到日期”的最大值。数据必须按“FromDate”降序排序。根据我的查询,我得到了这个结果:
+----------+------------+------------+------+
| Employee | FromDate | ToDate | Code |
+----------+------------+------------+------+
| Employee | 01/05/2016 | 30/06/2016 | 2 |
| Employee | 01/03/2016 | 30/04/2016 | 3 |
| Employee | 01/01/2016 | 31/07/2016 | 4 |
+----------+------------+------------+------+
它工作正常,但如果相同的“代码”有更多的日期范围(参见第一个表中的4个代码),每个代码我得到一行。我想用2个记录中的4个代码得到这个结果,因为它的周期不是连续的,但它被其他代码(3和2)打破了:
+----------+------------+------------+------+
| Employee | FromDate | ToDate | Code |
+----------+------------+------------+------+
| Employee | 01/07/2016 | 31/07/2016 | 4 |
| Employee | 01/05/2016 | 30/06/2016 | 2 |
| Employee | 01/03/2016 | 30/04/2016 | 3 |
| Employee | 01/01/2016 | 29/02/2016 | 4 |
+----------+------------+------------+------+
我使用此查询:
SELECT
d."Employee",
MIN (d."FromDate") AS "FromDate",
MAX (d."ToDate") AS "ToDate",
d."Code"
FROM
(
SELECT
"Employees"."FromDate",
"Employees"."ToDate",
"Employees"."Code",
"Employees"."Employee"
FROM
schema_estelspa."Employees"
ORDER BY
"Employees"."FromDate" DESC
) d
GROUP BY
d."Code",
d."Employee"
ORDER BY
(MIN(d."FromDate")) DESC
有什么技巧可以得到我想要的结果吗?
日期格式为:dd / MM / yyyy
答案 0 :(得分:1)
在这里,你需要制作日期范围,并将from_date作为逐列的一部分。你还需要自己加入来实现这个结果。我在teradata中准备了以下SQL。请对您的数据库进行必要的更改(coalesc用作null表达式,您也可以使用nvl或case语句)
<强>查询:强>
SELECT E.EMPLOYEE, E.CODE,COALESCE(ET1.FROMdATE,E.FROMDATE)FROM_DATE ,MAX(E.TODATE)TO_D
FROM EMP_TEST E
LEFT OUTER JOIN EMP_TEST ET1
ON E.EMPLOYEE=ET1.EMPLOYEE
AND E.CODE=ET1.CODE
AND E.FromDate=ET1.ToDate+1
GROUP BY 1,2,3
ORDER BY FROM_DATE
<强>输出:强>
Employee Code FROM_DATE TO_D
1 Employee 4 1/1/2016 2/29/2016
2 Employee 2 5/1/2016 6/30/2016
3 Employee 4 7/1/2016 7/31/2016
4 Employee 3 3/1/2016 4/30/2016
答案 1 :(得分:1)
连接点
的标准递归解决方案lower_limit <= X < upper_limit
)更易于使用注意:下面的代码不处理重叠间隔。
-- Table
CREATE TABLE ecode
( employee varchar NOT NULL
, code INTEGER NOT NULL
, fromdate DATE NOT NULL
, uptodate DATE NOT NULL
);
SET datestyle = 'DMY' ;
-- Data
INSERT INTO ecode(employee, fromdate, uptodate, code) VALUES
('Employee','01/07/2016','31/07/2016', 4)
, ('Employee','01/06/2016','30/06/2016', 2)
, ('Employee','01/05/2016','31/05/2016', 2)
, ('Employee','01/04/2016','30/04/2016', 3)
, ('Employee','01/03/2016','31/03/2016', 3)
, ('Employee','01/02/2016','29/02/2016', 4)
, ('Employee','01/01/2016','31/01/2016', 4)
;
-- Convert to half-open interval
UPDATE ecode SET uptodate = uptodate + '1 day'::interval;
-- SELECT * FROM ecode;
WITH RECURSIVE zzz AS (
SELECT employee, code, fromdate, uptodate
FROM ecode e0
WHERE NOT EXISTS ( -- first one in series
SELECT * FROM ecode nx
WHERE nx.employee = e0.employee
AND nx.code = e0.code
AND nx.uptodate = e0.fromdate
)
UNION ALL -- append consecutive intervals
SELECT e1.employee, e1.code, zzz.fromdate, e1.uptodate
FROM ecode e1
JOIN zzz ON zzz.employee = e1.employee
AND zzz.code = e1.code
AND zzz.uptodate = e1.fromdate
)
SELECT * FROM zzz
-- suppress the partial results
WHERE NOT EXISTS (SELECT * FROM ecode nx
WHERE nx.employee = zzz.employee
AND nx.code = zzz.code
AND nx.fromdate = zzz.uptodate
)
ORDER BY employee, code, fromdate
;
DROP SCHEMA
CREATE SCHEMA
SET
CREATE TABLE
SET
INSERT 0 7
UPDATE 7
employee | code | fromdate | uptodate
----------+------+------------+------------
Employee | 2 | 2016-05-01 | 2016-07-01
Employee | 3 | 2016-03-01 | 2016-05-01
Employee | 4 | 2016-01-01 | 2016-03-01
Employee | 4 | 2016-07-01 | 2016-08-01
(4 rows)