【问题标题】:Query to return unique values when one of column value is repeating查询以在列值之一重复时返回唯一值
【发布时间】:2013-12-20 19:18:46
【问题描述】:

我有一个如下的 sql 查询

SELECT Name.Nameguid,Name.Name,relation.ancestorguid,relation.productguid,relation.pathLength
     FROM Name Name 
     JOIN ProductRelationship relation 
       ON Name.productGuid = relation.ancestorGuid 
      AND relation.productGuid = '6075D04A-E74A-464B-94E7-25374F0B9833' 
     ORDER BY relation.pathLength DESC

返回以下内容

NameGuid                                Name                AncestorGuid                            ProductGuid                             PathLength
6D39CB04-88D9-4125-A052-8DF608AAD29C    NameParentOnly  8E07F824-763C-434F-926C-80FCC8690243    6075D04A-E74A-464B-94E7-25374F0B9833    2
186E1DF3-4D1A-4020-B845-1280CF1092EA    NameParentChild 8E07F824-763C-434F-926C-80FCC8690243    6075D04A-E74A-464B-94E7-25374F0B9833    2
CA60E542-4047-4B4D-AA22-0744A1E5F2E0    childlevelName  D9833FCA-93A7-42F5-AFC3-5544F7A4425D    6075D04A-E74A-464B-94E7-25374F0B9833    1
A09D01FC-D69D-4AFA-B4D0-C804F030A281    NameParentChild D9833FCA-93A7-42F5-AFC3-5544F7A4425D    6075D04A-E74A-464B-94E7-25374F0B9833    1
63148C89-59FD-4C96-883F-60E8446A6BC4    NameParentChild 6075D04A-E74A-464B-94E7-25374F0B9833    6075D04A-E74A-464B-94E7-25374F0B9833    0

我的情况是,每当 name gets repeated 时,我希望查询只返回 Path Length 值最小的行。

在上面的例子中,只有NameParentChild 重复了三遍,我希望它返回路径长度最短的那个(在这种情况下为零)

这是我期待的结果

NameGuid                                Name                AncestorGuid                            ProductGuid                             PathLength
6D39CB04-88D9-4125-A052-8DF608AAD29C    NameParentOnly  8E07F824-763C-434F-926C-80FCC8690243    6075D04A-E74A-464B-94E7-25374F0B9833    2
CA60E542-4047-4B4D-AA22-0744A1E5F2E0    childlevelName  D9833FCA-93A7-42F5-AFC3-5544F7A4425D    6075D04A-E74A-464B-94E7-25374F0B9833    1
63148C89-59FD-4C96-883F-60E8446A6BC4    NameParentChild 6075D04A-E74A-464B-94E7-25374F0B9833    6075D04A-E74A-464B-94E7-25374F0B9833    0

如何调整查询来做到这一点?我希望查询也可以在 Oracle、DB2 和 SQL 服务器上运行。

谢谢

【问题讨论】:

  • 这些列是否是唯一的(通常是主键)?
  • 是的 ..NameGuid 是唯一的

标签: sql sql-server oracle oracle11g db2


【解决方案1】:

您应该可以使用analytic (or window) functions 执行此操作。您的 RDBMS 中是否提供分析函数,以及您必须使用哪种语法是另一回事。在 Oracle 中,假设 NameGuid 对于您的原始查询是唯一的,您可以这样做:

select NameGuid, Name, AncestorGuid, ProductGuid, PathLength
from (
    select 
    NameGuid, 
    Name, 
    AncestorGuid, 
    ProductGuid, 
    PathLength, 
    -- take every row from original query with the same Name as this,
    -- order those rows by PathLength (and NameGuid to disambiguate)
    -- and return the NameGuid of the first row in that "partition"
    first_value(NameGuid) over (partition by Name order by PathLength asc, NameGuid asc) MinNameGuid
    from ( 
        ... your original query ...
    )
)
where 
-- return rows whose NameGuid is the same as the NameGuid calculated by first_value(...)
NameGuid = MinNameGuid

【讨论】:

    猜你喜欢
    • 2015-03-25
    • 1970-01-01
    • 2017-04-05
    • 2021-12-23
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    相关资源
    最近更新 更多