简体   繁体   中英

How to get unique records in the SQL Server 2000

I have 4 tables

Table1 : Employeetab
Employeeid     EmployeeName
     1             xyz
     2             abc
     3             mno
     4             pqr

Table2 :   PurchaseRequesttab
PRID    Employeeid     PRNR
 1          1           pr1

Table3 :    Statustab
StatusID         Statusmsg
   1             approve by manager
   2             approve by CC manager
   3             approve by designer
   4             approve by BU head

Table4 :      ApprovalStatustab
PRID    StatusID   Employeeid     ApprovalDate
 1           1           3           jan 1 2015
 1           2           3           jan 3 2015
 1           4           4           Jan 5 2015

Result should be

        PRNR           EmployeeName           Statusmsg 
        pr1                 mno             approve by CC manager 

Below is my query, which gives duplicate

Select 
   distinct P.PRNR, EmployeeName, Statusmsg 
from  Employeetab as E 
     inner join PurchaseRequesttab AS P  ON  E. Employeeid   = p.Employeeid  
     inner join  ApprovalStatustab  as A ON P. PRID    = A. PRID    
     inner join  Statustab as S  on A. StatusID    = S. StatusID   

How to get unique records in the result set, edited the result :)

Some thing your data is mismatch with your entry and your table structure.

 1. ApprovalStatustab A and PurchaseRequesttab as P -  P.PRID = A.PRID mismatch entry as above.

Here is your sample query below, I comment and add some entry for better understand what you doing wrong.

declare @Employeetab table(Employeeid   int,  EmployeeName varchar(50))
declare @PurchaseRequesttab table(PRID int,Employeeid int, PRNR varchar(50) )
declare @Statustab table(StatusID int, Statusmsg varchar(50) )
declare @ApprovalStatustab table(PRID int, StatusID int, Employeeid int, ApprovalDate varchar(50) )


insert into @Employeetab values (1,'xyz'),(2,'abc'),(3,'mno'),(4,'pqr')
insert into @PurchaseRequesttab values (1,1,'pr1') 
, (2,3,'pr3') --this I added to get the result
insert into @Statustab values (1,'approve by manager'),(2,'approve by CC manager'),(3,'approve by designer'),(4,'approve by BU head')
insert into @ApprovalStatustab values (1,1,3,'jan 1 2015'),(1,2,3,'jan 3 2015'),(1,4,4,'jan 5 2015')
,(2,2,3,'jan 5 2015') --this I added to get the result

Select 
   distinct 
   P.PRNR, 
   EmployeeName
   ,Statusmsg 
from  @Employeetab as E 
     inner join @PurchaseRequesttab AS P  ON  E.Employeeid   = p.Employeeid  
     inner join  @ApprovalStatustab  as A ON P.PRID    = A.PRID  
     --and a.Employeeid = e.Employeeid  --this is added for distinct result too
     inner join  @Statustab as S  on A.StatusID    = S.StatusID 

您的状态消息必须有所不同,因为您的状态ID不同并且它们具有不同的消息...如果它们具有相同的消息,则您将有1行。

确保PurchaseRequesttab中的PRNR列是唯一的(如PRID列)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM