SQL表只与1行重复

时间:2016-02-10 19:35:00

标签: sql-server

我有3个表,2个表(TableB,TableC)具有相同的记录数据。 TableA TableB和C是在从另一个表到HH:mm:ss的SUMing / cast秒之后创建的。但是,TableA具有重复的名称,因为它计算登录时间,每个人每天可以记录多次,而另外2个是转换后的秒数。有没有一种方法可以将表A与B和C进行比较,只取一个来自A的重复项的名称?基本上,如果TableA在早上6点和下午12点显示蒂姆史密斯的事件,那么请使用那些单独的行,只用2显示他的名字一次?

这是TableA的样子:

enter image description here

我使用的WHOLE SQL命令是:

SELECT TableD.[Name], RIGHT('0' + CAST(TableD.[Staffed Time] / 3600 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST((TableD.[Staffed Time] / 60) % 60 AS VARCHAR),2)  + ':' +
RIGHT('0' + CAST(TableD.[Staffed Time] % 60 AS VARCHAR),2)
AS StaffedTime, RIGHT('0' + CAST(TableD.[Time in Break] / 3600 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST((TableD.[Time in Break] / 60) % 60 AS VARCHAR),2)  + ':' +
RIGHT('0' + CAST(TableD.[Time in Break] % 60 AS VARCHAR),2) 
AS BreakTime, RIGHT('0' + CAST(TableD.[Time in Lunch] / 3600 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST((TableD.[Time in Lunch] / 60) % 60 AS VARCHAR),2)  + ':' +
RIGHT('0' + CAST(TableD.[Time in Lunch] % 60 AS VARCHAR),2) 
AS LunchTime

INTO TableE
FROM TableD

SELECT [Name], LunchTime,
        case
          when LunchTime < '01:04:00' then 0

          else 1
        end as LunchInc
into TableB
FROM TableE

SELECT [Name], BreakTime,
        case
          when BreakTime BETWEEN '00:29:00' AND '00:40:00' then 0

          else 1
        end as BreakInc
into TableC
FROM TableE

SELECT TableG.[Name], TableG.[Login Time]
,      case 
            when ABS(DATEDIFF(MINUTE, TableG.[Login Time], TableF.StartTime)) BETWEEN 0 and 4 then 0 else 1
       end as LoginInc 
into TableA 
FROM TableG
JOIN TableF 
  on TableG.[Agent Name] = TableF.AgentName

如果我在执行转换时将登录时间添加到顶部的select命令,它将返回数千行数据,这些数据完全不准确,或者至少是我需要它的工作方式。

这是我尝试过的。

 SELECT TableD.[Name], TableG.[Name], RIGHT('0' + CAST(TableD.[Staffed Time] / 3600 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST((TableD.[Staffed Time] / 60) % 60 AS VARCHAR),2)  + ':' +
RIGHT('0' + CAST(TableD.[Staffed Time] % 60 AS VARCHAR),2)
AS StaffedTime, RIGHT('0' + CAST(TableD.[Time in Break] / 3600 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST((TableD.[Time in Break] / 60) % 60 AS VARCHAR),2)  + ':' +
RIGHT('0' + CAST(TableD.[Time in Break] % 60 AS VARCHAR),2) 
AS BreakTime, RIGHT('0' + CAST(tacmain.[Time in Lunch] / 3600 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST((TableD.[Time in Lunch] / 60) % 60 AS VARCHAR),2)  + ':' +
RIGHT('0' + CAST(TableD.[Time in Lunch] % 60 AS VARCHAR),2) 
AS LunchTime

INTO TableE
FROM TableD, TableG
INNER JOIN TableG.[Name] = TableD.[Name]

希望它能够将名称匹配在一起并使用TableE输入登录时间,这就是它添加数千行数据的地方。

0 个答案:

没有答案