0

次のクエリがあります。

`

select e.empid, convert(char(5), tr.In_Time, 108) as In_time, 
        convert(char(5), tr.Out_Time, 108) as Out_time,
        convert(varchar(5), sum(datediff(minute, trr.In_Time, isnull(trr.Out_Time,       null))) / 60)
        + ':' +
        convert(varchar(5),sum(datediff(minute, trr.In_Time, isnull(trr.Out_Time,null))) % 60)
   as TotalHours,  

  from EMPLOYEES e
 Left Join EMPLOYEE_TIME tr
    on (e.empid=tr.empid)
 Left Join EMPLOYEE_TIME trr
    on (e.empid=trr.empid)
 where (
            trr.In_Time BETWEEN '2013-09-11' AND DATEADD(DAY, 1, '2013-09-11')
            and  tr.In_Time BETWEEN '2013-09-11' AND DATEADD(DAY, 1,  '2013-09-11')
        )  group by e.empid, tr.In_Time, tr.Out_Time e.JoiningDate    order  by e.JoiningDate ASC

`

上記のクエリを実行した後、次の結果が得られます: `

EmpID        in_time            out_time            totalhours  
1           9:30                18:00                   8:30  
2           10:00               13:00                   8:00  
2           14:00               19:00  
3           10:30               13:30                   3:00  
3           14:30               NULL                    3:00  

`

しかし、以下のように、 複数の時間エントリOut_time が Nullの場合、 totalhoursを 2 回出力したくありません。

`

EmpID        in_time            out_time            totalhours  
1           9:30                18:00                   8:30  
2           10:00               13:00                   8:00  
2           14:00               19:00  
3           10:30               13:30                   3:00  
3           14:30               NULL                      

`

誰でも私を助けてもらえますか?前もって感謝します

ワーキングSQL

SELECT  e.empid ,
        CONVERT(CHAR(5), tr.In_Time, 108) AS In_time ,
        CONVERT(CHAR(5), tr.Out_Time, 108) AS Out_time ,
        CONVERT(VARCHAR(5), SUM(DATEDIFF(minute, trr.In_Time,
                                         ISNULL(trr.Out_Time, NULL))) / 60)
        + ':' + CONVERT(VARCHAR(5), SUM(DATEDIFF(minute, trr.In_Time,
                                                 ISNULL(trr.Out_Time, NULL)))
        % 60) AS TotalHours
FROM    EMPLOYEES e
        LEFT JOIN EMPLOYEE_TIME tr ON ( e.empid = tr.empid )
        LEFT JOIN EMPLOYEE_TIME trr ON ( e.empid = trr.empid )
WHERE   ( trr.In_Time BETWEEN '2013-09-11'
                      AND     DATEADD(DAY, 1, '2013-09-11')
          AND tr.In_Time BETWEEN '2013-09-11'
                         AND     DATEADD(DAY, 1, '2013-09-11')
        )
GROUP BY e.empid ,
        tr.In_Time ,
        tr.Out_Time ,
        e.JoiningDate
ORDER BY e.JoiningDate ASC
4

2 に答える 2

0

あなたの例に基づいて、それぞれの最初の行の時間のみが必要なようですEmpID。(そうでない場合は、これに対する例外である追加データで質問を更新してください。)

これを前提として、を使用する方法は次のrow_number()とおりです。

with yourquery as (<your query here>)
select EmpID, in_time, out_time,
       (case when seqnum = 1 then totalhours end) as totalhours
from (select yq.*,
             row_number() over (partition by EmpId order by in_time) as seqnum
      from yourquery yq
     ) yq;

編集:

これは次のようになります。

with yourquery as (
    SELECT  e.empid ,
            CONVERT(CHAR(5), tr.In_Time, 108) AS In_time ,
            CONVERT(CHAR(5), tr.Out_Time, 108) AS Out_time ,
            CONVERT(VARCHAR(5), SUM(DATEDIFF(minute, trr.In_Time,
                                             ISNULL(trr.Out_Time, NULL))) / 60)
            + ':' + CONVERT(VARCHAR(5), SUM(DATEDIFF(minute, trr.In_Time,
                                                     ISNULL(trr.Out_Time, NULL)))
            % 60) AS TotalHours
    FROM    EMPLOYEES e
            LEFT JOIN EMPLOYEE_TIME tr ON ( e.empid = tr.empid )
            LEFT JOIN EMPLOYEE_TIME trr ON ( e.empid = trr.empid )
    WHERE   ( trr.In_Time BETWEEN '2013-09-11'
                          AND     DATEADD(DAY, 1, '2013-09-11')
              AND tr.In_Time BETWEEN '2013-09-11'
                             AND     DATEADD(DAY, 1, '2013-09-11')
            )
    GROUP BY e.empid ,
            tr.In_Time ,
            tr.Out_Time ,
            e.JoiningDate
    )
select yq.EmpID, yq.in_time, yq.out_time,
       (case when seqnum = 1 then yq.totalhours end) as totalhours
from (select yq.*,
             row_number() over (partition by yq.EmpId order by in_time) as seqnum
      from yourquery yq
     ) yq
ORDER BY yq.JoiningDate ASC
于 2013-09-12T12:49:36.140 に答える