日期:2014-05-17 浏览次数:20513 次
--> 测试数据:[tb] IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb] GO CREATE TABLE [tb]([Card] VARCHAR(6),[ADate] DATETIME,[ATime] time) INSERT [tb] SELECT '002001','2012-09-02','13:03:00' UNION ALL SELECT '002001','2012-09-02','14:23:00' UNION ALL SELECT '769041','2012-09-02','13:16:00' UNION ALL SELECT '769101','2012-09-02','14:23:00' GO --> 开始查询: select [Card], [ADate], [ATime], case when rn=1 then'进' else '出' end as Mark from (SELECT *,rn=row_number() over(partition by [Card] order by [ADate],[ATime]) FROM [tb])a ------------------------------------------- -->结果集 /* Card ADate ATime Mark ----------------------------------------------------- 002001 2012-09-02 00:00:00.000 13:03:00 进 002001 2012-09-02 00:00:00.000 14:23:00 出 769041 2012-09-02 00:00:00.000 13:16:00 进 769101 2012-09-02 00:00:00.000 14:23:00 进 */
------解决方案--------------------
LS就是MSSQL的
------解决方案--------------------