2016-08-12 1 views
0

에 가입 할 때 중복을 방지하는 방법, 임시 테이블 아래를 참조표는 세 ​​개의 레코드를 포함 여러 테이블

id SalesMan_id Module_Cdoe Time_In Time_Out 
1 13524  Photo  1:30pm 1:40pm 
2 13524  Survey  1:30pm 1:40pm 
3 13524  Merchand 1:30pm 1:40pm 
4 12543  Photo  2:10pm 2:30pm 
5 12543  Survey  2:10pm 2:30pm 
6 12543  Merchand 2:10pm 2:30pm 
7 12543  Photo  3:10pm 3:30pm 
8 12543  Survey  3:10pm 3:30pm 
9 12543  Merchand 3:10pm 3:30pm 

하지만 내가 볼처럼 필요 아래 :

id SalesMan_id Module_Cdoe Time_In Time_Out 
1 13524  Photo  1:30pm 1:40pm 
2 13524  Survey  1:30pm 1:40pm 
3 13524  Merchand 1:30pm 1:40pm 
4 12543  Photo  2:10pm 2:30pm 
5 12543  Survey  2:10pm 2:30pm 
6 12543  Merchand 2:10pm 2:30pm 

(OR)

id SalesMan_id Module_Cdoe  Time_In Time_Out 
1 13524  Photo   1:30pm 1:40pm 
2 13524  Survey   1:30pm 1:40pm 
3 13524  Merchand  1:30pm 1:40pm 
4 12543  Photo   3:10pm 3:30pm 
5 12543  Survey   3:10pm 3:30pm 
6 12543  Merchand  3:10pm 3:30pm 

내 기대치를 달성하기위한 쿼리는 무엇입니까?

+0

예상을 할 이전을 보여주고 싶다. 시간 또는 그 이후의 시간. 둘 중 하나를 지정하는 것이 더 좋습니다. 당신이 그들에게 관심이 없다면, 당신은 그들을 필요로합니까? 그렇지 않다면 그룹화를 사용할 수 있습니다. – Tanner

+0

나중에 나중에 –

+0

이 질문을 업데이트하고 예상 출력의 나중 행과 일치하도록 ID를 변경하십시오. – Tanner

답변

1

요청에 따라 이는 원본의 작은 비틀기 일뿐입니다.

Declare @TimeStamp table (Id int, SalesMan_ID int, Module_Cdoe varchar(50),Time_In varchar(10),Time_Out varchar(10)) 
Insert into @TimeStamp values 
(1,13524,'Analysis','1:30pm','1:40pm'), 
(2,12543,'Analysis','2:10pm','2:30pm'), 
(3,12543,'Analysis','3:10pm','3:30pm') 


;with cteBase (Seq,Module_Cdoe) as(
    Select 0,'Analysis' union all 
    Select 1,'Photo' union all 
    Select 2,'Survey' union all 
    Select 3,'Merchand' 
) 
, cteDistPop as (
    Select Distinct 
      SalesMan_Id 
      ,Time_In = Max(Time_In) 
      ,Time_Out = Max(Time_Out) 
    From @TimeStamp 
    Group By SalesMan_Id 
) 
Select id=Row_Number() over (Order By Time_In,SalesMan_id,Seq) 
     ,SalesMan_id 
     ,b.Module_Cdoe 
     ,Time_In 
     ,Time_Out 
from cteDistPop A 
Cross Join cteBase B 
Order By 1 

반품 - "Analysis"In 또는 Out에서 명확하지 않습니다. 어느 쪽이든 당신은에서 cteBase

id SalesMan_id Module_Cdoe Time_In Time_Out 
1 13524   Analysis  1:30pm 1:40pm 
2 13524   Photo   1:30pm 1:40pm 
3 13524   Survey  1:30pm 1:40pm 
4 13524   Merchand  1:30pm 1:40pm 
5 12543   Analysis  3:10pm 3:30pm <<-- Notice Later Date 
6 12543   Photo   3:10pm 3:30pm 
7 12543   Survey  3:10pm 3:30pm 
8 12543   Merchand  3:10pm 3:30pm 
1

는이 같은 시도 것을 제어

Declare @TimeStamp table (Id int, SalesMan_ID int, Module_Code 
varchar(50),Time_In varchar(10),Time_Out varchar(10)) 

Insert into @TimeStamp values 
(1,13524,'Analysis','1:30pm','1:40pm'), 
(2,12543,'Analysis','2:10pm','2:30pm'), 
(3,12543,'Analysis','3:10pm','3:30pm') 

먼저 테이블 출력

select *From (
Select Row_Number() over (Order By Time_In,SalesMan_id,Seq) id 
     ,SalesMan_id 
     ,b.Module_Code 
     ,Time_In 
     ,Time_Out 
     ,row_number() over (partition by SalesMan_Id,B.Module_code order by id) as rn 
from @TimeStamp A 
Join (select *from 
(values (1,'Photo'),(2,'Survey'),(3,'Merchand'))cteBase(Seq,Module_Code)) B 
on (A.Module_Code='Analysis'))t 
where rn=1 
Order By 1 

둘째 예상 테이블 출력

select *From (
Select Row_Number() over (Order By Time_In,SalesMan_id,Seq) id 
     ,SalesMan_id 
     ,b.Module_Code 
     ,Time_In 
     ,Time_Out 
     ,row_number() over (partition by SalesMan_Id,B.Module_code order by id desc) as rn 
from @TimeStamp A 
Join (select *from 
(values (1,'Photo'),(2,'Survey'),(3,'Merchand'))cteBase(Seq,Module_Code)) B 
on (A.Module_Code='Analysis'))t 
where rn=1 
Order By 1 
관련 문제