2017-12-06 6 views
-3

従業員のID、たわごとと日付を含む従業員シフトテーブルが1つあります。 実際、私は毎日すべての従業員のシフトを入れています。表は1私は従業員の履歴を取得するために複雑なSQLクエリが必要です

Employee id  Shift  Date 
---------------------------------- 
1     G  2017-11-01 
1     G  2017-11-02 
1     NG  2017-11-03 
1     NG  2017-11-04 
1     G  2017-11-05 
1     G  2017-11-06 
1     G  2017-11-07 
1     G  2017-11-08 

以下のようにlooingされ、私は事前に

Employee Id  Shift From Date  To Date 
1     G  2017-11-01 2017-11-02 
1    NG  2017-11-03 2017-11-04 
1     G  2017-11-05 2017-11-08 

おかげで、以下のような出力が必要です。ループを使わずに簡単にログインできるようにしてください。

+2

1)太陽の下のすべてのdbmsタグにカーペット爆弾を付けないでください。 2)これはSOの仕組みではありません。あなたはいくつかの努力を示す必要があります。あなたは何を試しましたか? – OldProgrammer

+2

ようこそスタックオーバーフロー。これは宿題/コード/ SQL /正規表現の作成サービスではなく、あなたの要件と言語のリストを投稿し、コード猿があなたのためにコードを生成します。私たちは喜んでお手伝いしますが、まず問題を自分で解決する努力をすることが期待されます。これを済ませたら、あなたが抱えている問題について説明したり、関連する**部分を含めたり、特定の質問をしたりすることができます。がんばろう。 –

+0

DB2、Sybase、そして私の永遠に愛用しているdBase IIIを忘れました。 – tadman

答えて

0
create table #merchant_comm(tid nvarchar(20), preval nvarchar(20), curval nvarchar(20), nextval nvarchar(20), datetimerequest datetime, nextdate datetime) 

truncate table #merchant_comm 

insert into #merchant_comm select * from (select tid,LAG(communication_mode) over (order by datetimerequest) preval, communication_mode curval, LEAD(communication_mode) over (order by datetimerequest) nextval,datetimerequest,LEAD(datetimerequest) over (order by datetimerequest) nextdate 
from MRLPOSNET_MSCRM.dbo.Pos_RL_communication_mode_Merchant_Detail 
where tid = '3734200M') as a 
where preval <> nextval or nextval is null or preval is null order by datetimerequest 


    with CTE as (
    select tid, 
case when preval is null then 1 when preval = curval then 0 when preval <> curval then 1 end as flag, 
curval as comm_mode, datetimerequest as from_date, lead(datetimerequest) over(order by datetimerequest) as to_date 
from #merchant_comm) 


select * from CTE where flag = 1 
+0

回答の仕組みを説明する必要があります –

関連する問題