在sql server中需要帮助查询

时间:2015-12-29 06:50:03

标签: sql-server sql-server-2008 sql-server-2012

我有两张桌子。 emp:

CREATE TABLE dbo.emp (
    empid INT NULL,
    deptid INT NULL,
    doj DATE NULL,
    loc VARCHAR(50) NULL,
    [status] INT NULL,
    [guid] INT NULL,
    locid INT
)
INSERT INTO emp (empid, deptid, doj, loc, [status], [guid], locid)
VALUES
    (1, 10, '2013-09-25', 'hyd', 5, 10, 4),
    (1, 10, '2014-03-25', 'che', 5, 11, 1),
    (1, 10, '2014-04-09', 'pune', 5, 12, 2),
    (1, 10, '2015-01-22', 'pune', 5, 13, 2),
    (2, 20, '2015-12-13', 'beng', 5, 14, 3),
    (2, 20, '2014-12-12', 'chen', 5, 15, 2),
    (2, 20, '2010-10-15', 'beng', 5, 16, 3),
    (2, 20, '2010-10-15', 'beng', 4, 17, 3),
    (3, 30, '2011-11-15', 'beng', 5, 18, 3),
    (4, 40, '2010-11-15', 'chen', 5, 19, 1),
    (4, 40, '2010-11-15', 'beng', 5, 20, 3),
    (4, 40, '2009-11-15', 'beng', 5, 21, 3),
    (5, 50, '2010-12-14', 'beng', 5, 22, 3)

empref:

CREATE TABLE dbo.empref (
    empid INT NULL,
    deptid INT NULL,
    startdate DATE NULL,
    [status] INT NULL,
    [guid] INT NULL,
    checkid INT NULL
)
INSERT INTO empref (empid, deptid, startdate, [status], [guid], checkid)
VALUES
    (1, 10, '2013-10-02', 2, 1, 1),
    (1, 10, '2014-04-09', 2, 2, 2),
    (1, 10, '2015-12-09', 1, 3, 3),
    (1, 10, '2015-01-30', 2, 4, 4),
    (2, 20, '2015-12-14', 2, 2, 5),
    (2, 20, '2015-12-15', 2, 3, 6),
    (3, 30, '2011-11-15', 2, 3, 7),
    (3, 30, '2011-11-16', 2, 5, 8),
    (4, 40, '2010-11-17', 2, 6, 9),
    (5, 50, '2010-11-15', 2, 7, 9),
    (5, 50, '2010-11-15', 2, 8, 10)

两个表都有公共列Empid + deptid我们需要考虑emp表status = 5相关记录与empref表status = 2相关记录和emp表doj< = startdate --empref表和天数之差小于或等于30天。在处理时间内忽略状态值4或1。

如果我们在emp表中找到多个记录,那么我们考虑min(doj)和min(locid)优先级以及empref表和记录中的相应guid在过滤器中视为更新。 如果我们在empref表中找到多个记录,那么我们考虑min(startdate)和min(checkid_)优先级以及empref表中的相应guid,并将记录视为过滤器中的更新。

如果上述条件不满足emp表中的少量记录,那么记录需要考虑在过滤器中插入。

基于上面的表我想要输出如下

Empid| Deptid | loc | Status | Filter | Doj |guid
1 | 10 | hyd | 5 | Update | 2013-09-25|1
1 | 10 | che | 5 | insert | 2014-03-25|2 ------min(startdate) corresponding record
1 | 10 | pune| 5 | update | 2014-04-09|11 --------mul
1 | 10 | Pune| 5 | update | 2015-01-22|4 
2 | 20 | beng| 5 | insert | 2015-12-13|14 --------------min(doj) record
2 | 20 | chen| 5 | update | 2014-12-12|2 
2 | 20 | beng| 5 | insert | 2010-10-15|16 -----this record not fall the above conditions
3 | 30 | beng| 5 | update | 2011-11-15 |3
4 | 40 |chen |5 |update |2010-11-15 |6
4 | 40 |beng |5 |insert |2010-11-15 |20
4 | 40 |beng |5 |insert |2010-11-15 |21
5 | 50 |beng |5 |update |2010-12-14 |9

我试过以下

SELECT e.Empid,
       ER.deptid,
       e.loc,
       e.[status],
       ER.[guid],
       e.[guid],
       CASE
           WHEN DATEDIFF(DAY, e.doj, ER.startdate) <= 0
               THEN 'INSERT'
           ELSE 'UPDATE'
       END filter,
       e.doj
FROM emp e
INNER JOIN empref ER ON e.Empid = ER.empid AND e.deptid = ER.deptid
WHERE e.[status] = 5
    AND ER.[status] = 2
    AND e.doj <= ER.startdate
    AND DATEDIFF(DAY, e.doj, ER.startdate) <= 30

我尝试了另一种方式

SELECT *
FROM (
    SELECT e.empid,
           e.deptid,
           e.loc,
           e.status,
           CASE
               WHEN DATEDIFF(DAY, e.doj, b.startdate) < 30
                   THEN 'Update'
               ELSE 'Insert'
           END AS filter,
           e.doj,
           CASE
               WHEN DATEDIFF(DAY, e.doj, b.startdate) < 30
                   THEN b.guid
               ELSE e.guid
           END AS guid,
           CASE
               WHEN ROW_NUMBER() OVER (PARTITION BY e.guid ORDER BY e.doj DESC) = 1
                   THEN 'current'
               ELSE 'initial'
           END AS Fil,
           CASE
               WHEN ROW_NUMBER() OVER (PARTITION BY b.empid, b.deptid ORDER BY b.startdate DESC) = 1
                   THEN 'current'
               ELSE 'initial'
           END AS Fil1,
           b.startdate
    FROM emp e
    LEFT JOIN empref b ON b.empid = e.empid
        AND b.deptid = e.deptid
        AND b.startdate >= e.doj
    WHERE e.status = 5
        AND b.status = 2
        AND e.empid = 1
) a
WHERE a.fil = 'current'

在上面的查询中未给出预期结果。请告诉我如何编写查询以在sql server中实现此任务

0 个答案:

没有答案