-1

我想将 table1 转换为 table2。因为我需要从表中找出除 mis_date 之外的所有不同记录,最重要的条件是两个相似的不同记录之间是否发生任何变化,而不是在这种情况下,我希望它们都作为两个不同的记录。

例子:

i/p

empId Empname Pancard MisDate
123 亚历克斯广告234 2012 年 11 月 31 日
123 亚历克斯广告234 2012 年 12 月 31 日
123 亚历克斯广告234 2013 年 1 月 31 日
123 亚历克斯 dds124 29/02/2013
123 亚历克斯广告234 2013 年 3 月 31 日
123 亚历克斯广告234 2013 年 4 月 31 日
123 亚历克斯 dds124 2013 年 5 月 30 日

预期 o/p

empId Empname Pancard MisDate
123 亚历克斯广告234 2012 年 11 月 31 日
123 亚历克斯 dds124 29/02/2013
123 亚历克斯广告234 2013 年 3 月 31 日
123 亚历克斯 dds124 2013 年 5 月 30 日
4

1 回答 1

0

Assuming there's only one row for each MisDate (otherwise you'll have to find another way to specify ordering):

SELECT t1.empId, t1.Empname, t1.Pancard
FROM Table1 t1
LEFT OUTER JOIN Table1 t2 
    ON t2.MisDate = (SELECT MAX(MisDate) FROM Table1 t3 WHERE t3.MisDate < t1.MisDate)
WHERE t2.empId IS NULL 
OR t2.empId <> t1.empId OR t2.Empname <> t1.Empname OR t2.Pancard <> t1.Pancard

SQL Fiddle example

This performs a self-join on the previous record, as ordered by MisDate, outputting if it is different or if there is no previous record (it is the first row).

Note: You've got some funky dates. I assume these are just transcription errors and have corrected them in the fiddle.

于 2013-10-03T17:47:30.707 回答