如何在连接多个表时避免重复

时间:2016-08-12 09:49:32

标签: sql-server

该表包含三条记录,请参见临时表:

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 1,'Photo' union all
    Select 2,'Survey' union all
    Select 3,'Merchand'
)
Select id=Row_Number() over (Order By Time_In,SalesMan_id,Seq)
      ,SalesMan_id
      ,b.Module_Cdoe
      ,Time_In
      ,Time_Out
 from @TimeStamp A
 Join cteBase B on (A.Module_Cdoe='Analysis')
 Order By 1

从上面的查询结果是:

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

那么实现我期望的查询是什么?

2 个答案:

答案 0 :(得分: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

返回 - 不清楚"分析"进或出。无论哪种方式,您都可以在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 :(得分: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