使用Sql Query选择Record with Repeated Status

时间:2014-06-04 09:12:51

标签: asp.net sql

我有一个附表如 enter image description here

我想看起来像选择查询数据是:

1和11是重复培训师和重复日,时间

10和12是复制培训师,复制vanueid和重复日期,时间

所以最后两列显示不是可用的重复,并且是重复而不是显示不可用 enter image description here

1 个答案:

答案 0 :(得分:1)

enter image description here这是我想到的解决方案。可能有一些语法问题,但我给出的逻辑可以帮到你。

        DECLARE @duplicate TABLE ( 
        trainerId INT,
        dt varchar(50)
        )

          INSERT INTO @duplicate SELECT TrainerId , [Date] from tbl GROUP BY TrainerId , Date HAVING (COUNT(*) > 1)
         SELECT * FROM @duplicate
         DECLARE @tempTable TABLE (
             trainerId INT,
             dt varchar(50),
             status int
          )
          INSERT INTO @tempTable   
          SELECT trainerId , [Date]  , STATUS = (SELECT COUNT(*) FROM @duplicate  where           trainerId = tbl.TrainerId and dt = tbl.Date) FROM tbl
          ![enter image description here][2]SELECT * ,  CASE [status] WHEN 0 THEN 'Available' ELSE 'Not Available' END FROM @tempTable