SQL查询在使用UNION和Distinct后返回重复值

时间:2013-04-01 06:52:16

标签: sql-server stored-procedures

我有一个SQL查询,我在尝试在两个查询联合后获取不同的值。我已经在两个查询中使用了Distinct。

我在RegID表中有Registration作为主键,对于注册表中的每个条目,RoomType中都有相应的条目,即RoomType表有{{1}作为外键。

现在的问题是如果RegID表格中有多个条目用于特定RoomType,我会收到重复的Regid。

我的查询打印如下:

RegID

和我的程序是这样的:

SELECT DISTINCT 
    dbo.tbRegistration.RegID, 
    dbo.tbRegistration.PropertyName, dbo.tbRegistration.IsActive, 
    dbo.tbRegistration.Stars, dbo.tbRegistration.City, 
    dbo.tbRegistration.Address, 
    dbo.tbHotelImages.MainImage, dbo.tbHotelImages.Img1, 
    dbo.tbHotelImages.Img2, dbo.tbHotelImages.Img3, 
    dbo.tbHotelImages.Img4, dbo.tbHotelImages.Img5, 
    dbo.tbHotelImages.Img6, dbo.tbHotelImages.Img7, 
    dbo.tbHotelImages.Img8, dbo.tbHotelImages.Img9, 
    dbo.tbRegistration.PropertyID, 
    dbo.tbHotelFeatures.*, 
    dbo.tbBillingPlan.BaseRate, dbo.tbRoomType.Name as RoomTypeName
FROM         
    dbo.tbRoomType 
LEFT OUTER JOIN
    dbo.tbRegistration ON dbo.tbRoomType.RegID = dbo.tbRegistration.RegID  
LEFT OUTER JOIN
    dbo.tbBillingPlan ON dbo.tbRoomType.RTID = dbo.tbBillingPlan.RTID    
LEFT OUTER JOIN
    dbo.tbHotelFeatures ON dbo.tbRegistration.RegID = dbo.tbHotelFeatures.RegID  
LEFT OUTER JOIN
    dbo.tbHotelImages ON dbo.tbRegistration.RegID = dbo.tbHotelImages.RegID                                          
WHERE 
    tbRegistration.PMS = 0 
    AND tbRegistration.Stars IN (0) OR 1=1 
    AND (tbRegistration.City LIKE '%' + @SearchText + '%')  
    AND (tbRoomType.Adults >= @Adults OR tbRoomType.Children >= @Children)
    AND (dbo.GetFreeRoomType(dbo.tbRoomType.RTID, @CheckIn, @CheckOut) > 0) 
    AND dbo.tbRegistration.IsActive = 1     

UNION

SELECT DISTINCT 
    dbo.tbRegistration.RegID  , dbo.tbRegistration.PropertyName, dbo.tbRegistration.IsActive, dbo.tbRegistration.Stars, dbo.tbRegistration.City, dbo.tbRegistration.Address, 
                      dbo.tbHotelImages.MainImage, dbo.tbHotelImages.Img1, dbo.tbHotelImages.Img2, dbo.tbHotelImages.Img3, dbo.tbHotelImages.Img4, dbo.tbHotelImages.Img5, 
                      dbo.tbHotelImages.Img6, dbo.tbHotelImages.Img7, dbo.tbHotelImages.Img8, dbo.tbHotelImages.Img9, dbo.tbRegistration.PropertyID    , 
                      dbo.tbHotelFeatures.*, dbo.tbBillingPlan.BaseRate,dbo.tbRoomType.Name as RoomTypeName
FROM         dbo.tbRoomType left OUTER JOIN
                      dbo.tbRegistration ON dbo.tbRoomType.RegID = dbo.tbRegistration.RegID  left OUTER JOIN
                      dbo.tbBillingPlan ON dbo.tbRoomType.RTID = dbo.tbBillingPlan.RTID    LEFT OUTER JOIN
                      dbo.tbHotelFeatures ON dbo.tbRegistration.RegID = dbo.tbHotelFeatures.RegID  LEFT OUTER JOIN
                      dbo.tbHotelImages ON dbo.tbRegistration.RegID = dbo.tbHotelImages.RegID                                           
       Where tbRegistration.PMS=1  and   tbRegistration.Stars IN (   0 ) or 1=1 and    (tbRegistration.City like  '%' + @SearchText + '%' )    

and (tbRoomType.Adults>=@Adults  or tbRoomType.Children>=@Children )
 and(dbTestSparrowPMS . dbo.GetFreeRoomType(dbo.tbRoomType.ID, @CheckIn, @CheckOut) >0) and dbo.tbRegistration.IsActive=1



   order by  tbRegistration.RegID

1 个答案:

答案 0 :(得分:0)

UNION删除重复记录(UNION = SELECT DISTINCT),而UNION ALL则返回重复记录。请检查this链接。

现在,在您的查询中,您正在对查询执行SELECT DISTINCT并使用UNION。但是如果你的两个查询为其中一个列返回不同的值,那么你将得到它们两个而UNION不会删除重复。

例如

ID文字

1 A

1 B

因此,您必须删除具有不同值的列,并因此尽可能返回重复记录。或者添加过滤掉在第一次查询中已经选择的记录的条件。