工人每天的第一次和最后一次检查

时间:2016-12-02 14:54:45

标签: tsql timetable

我需要每个工人的第一次办理登机手续的清单以及每天的最后退房...

根据以下查询得到的结果是,每天只有一个数据......

SELECT 
    p.prsnCode [Sicil No]
    ,p.[prsnName1]
    ,p.[prsnName2]
    ,t.[ioTransactionDate]

      ,[Giris/Cikis] = (
  case 
  when [ioStatus] = 0 then 'Giris' 
  when [ioStatus] = 1 then 'Cikis' 
  else 'Uzaya Gitti' end )


  FROM [IOTransaction] t

  left join dbo.Person p on t.ioPrsnRefId = p.prsnRefId 

  where t.[ioTransactionDate] = (select MIN(m.[ioTransactionDate]) from IOTransaction m 
                                    where m.ioPrsnRefId = t.ioPrsnRefId and CAST(m.[ioTransactionDate] AS DATE) = CAST(t.[ioTransactionDate] AS DATE)
                                            group by m.ioPrsnRefId)
         OR t.[ioTransactionDate] = (select MAX(m.[ioTransactionDate]) from IOTransaction m 
                                    where m.ioPrsnRefId = t.ioPrsnRefId and CAST(m.[ioTransactionDate] AS DATE) = CAST(t.[ioTransactionDate] AS DATE)
                                            group by m.ioPrsnRefId)

group by t.ioPrsnRefId, prsnCode, prsnName1 , prsnName2, t.[ioTransactionDate], ioStatus 


    ORDER BY P.prsnCode 

我还尝试将此group by m.ioPrsnRefId, CAST(m.[ioTransactionDate] AS DATE)添加到where子句但它给出错误...(子查询返回的值超过1。)

0 个答案:

没有答案