0

我被这个问题困住了。我的查询中有重复项,因为某些记录同时在内部和外部。我更喜欢在内部而不是外部,但是当内部没有入口时,一些外部更可取。

从产品中选择 ID、日期、位置

id date        location
----------
2  01/01/2012  in-house
2  05/01/2012  outside <- in this situation i want to keep just in-house

id date        location
----------
4  01/01/2012  in-house
5  03/01/2012  outside <- in this situation i want to keep both since there is no db entry for id=5 therefor i have just info from outside

有人可以帮忙吗?

4

1 回答 1

1

一种方法是执行从表到自身的完全外部联接,然后使用合并。

 Select 
    COALESCE(Inside.Id, outside.id) Id,
    COALESCE(Inside.date, outside.date) Date,
    COALESCE(Inside.location, outside.location) Location
From 
    prod Inside
    FULL OUTER JOIN prod Outside
    ON Inside.id = Outside.iD
    and Inside.location <> Outside.Location
Where
    (Inside.Location = 'in-house'
    or 
    Inside.Location is null)
    and 
    (outside.Location = 'outside'
     or
     outside.Location is null)

演示

笔记:

如果您的字段可以为空,您可能希望使用 Case 语句而不是 coalesce 并使用 ID 字段来确定要使用的表。以日期为例

CASE WHEN Inside.Id is not null THEN Inside.date ELSE outside.date END date

正如 Dems 所指出的,这也假设这{id, location}是独一无二的。

更新

由于您使用的是 SQL Server 并且 {ID, Location} 不是唯一的,并且您想要最大日期值并且始终选择内部而不是外部,因此您可以ROW_NUMBER/WHERE RowNumber = 1在这里有效地使用,首先在位置订购,然后按日期订购。

WITH cte 
     AS (SELECT Row_number() OVER ( partition BY ID 
               ORDER BY CASE LOCATION WHEN  'in-house' THEN 0  
                                      WHEN  'outside' THEN 1 END, 
                        DATE DESC) rn, 
                ID, 
                Date, 
                Location 
         FROM   prod) 
SELECT ID, 
       Date, 
       Location 
FROM   cte 
WHERE  rn = 1 

演示

注意我们不必使用 case 语句,但我希望映射是明确的。

于 2012-05-22T16:30:26.883 回答