【问题标题】:Removing duplicates from select query output从选择查询输出中删除重复项
【发布时间】:2017-07-11 00:47:27
【问题描述】:

谁能建议以下查询中的错误是什么?实际上,我想从选择查询输出中删除所有重复项并仅获取唯一行。提前致谢。

select *
from (
    select ROW_NUMBER() over (
            partition by Dep
            , tariffkode
            , LinkTariffType
            , poliata
            , poliatavia
            , podiata
            , podiatavia
            , PreCarriage
            , PortTerminalId
            , Product
            , RoutingOrder
            , PrepaidCollect
            , isnull(description, '')
            , ScaleCalcCode
            , isnull(scalefrom, 0)
            , isnull(scaleto, 0)
            , CurrencyCode
            , Base order by LinkTariffType desc
            ) Record
        , *
    from (
        select tn.LinkTariffType
            , tn.Dep
            , tn.POLIata
            , tn.POLIatavia
            , tn.PODIata
            , tn.PODIatavia
            , tn.CurrencyCode
            , tn.LegalEntityID
            , tn.Rate
            , tn.Base
            , tn.Minimum
            , tn.NrDescription
            , tn.Description
            , tn.DateFrom
            , tn.DateUntil
            , tn.DateCreate
            , tn.DateMod
            , tn.ModName
            , tn.Tariffkode
            , tn.ExpiryDate
            , tn.PClass
            , tn.Maximum
            , tn.RoutingOrder
            , tn.TariffCompType
            , tn.PrePaidCollect
            , tn.Product
            , tn.IsDeleted
            , (
                select distinct Location_IATA
                from Company
                where Called = 'KARL KING'
                    and LegalEntityID = 1
                    and IsDeleted = 0
                ) as PreCarriage
            , tn.PortTerminalID
            , tn.ScaleFrom
            , tn.ScaleTo
            , tn.ScaleCalcCode
            , tn.Mandatory
            , (
                select CompanyID
                from PlaceOfReceipt
                where warehouse = 'KARL KING'
                    and LegalEntityID = 1
                    and OfficeID = 13
                    and IsDeleted = 0
                ) as WarehouseID
            , tn.TariffRelID
            , tn.FreeDescription
            , 0
            , tn.ShipCode
            , tn.AgentID
            , tn.ContainerTypeID
            , tn.CommodityID
            , 0 as TempTable
        from TariffNew tn
        inner join hhInvoiceLines inv
            on tn.Tariffkode = inv.NrInvoiceLine
        where (
                tn.PreCarriage is not null
                and tn.PreCarriage != ''
                )
            and (
                tn.POLIata is not null
                and tn.POLIata != ''
                )
            and inv.OfficeID = 13
            and inv.IsDeleted = 0
            and inv.LegalEntityID = 1
            and tn.LegalEntityID = 1
            and tn.Dep = 'E'
            and tn.IsDeleted = 0
            and tn.DateUntil = '2078-12-31 00:00:00'
            and tn.Description = 'kgl'
        )
    ) as b
where b.Record = 1

【问题讨论】:

  • 我尝试过不同的。但它不起作用。
  • 请努力格式化查询。

标签: sql sql-server tsql select duplicates


【解决方案1】:

首先,您必须定义要称为“唯一行”的内容。

一旦你有一组列来确定一行是唯一的,那就是你在row_number()partition by 部分中使用的一组列

在下面的代码中,取消注释定义“唯一行”的列集:

  select *

  from (
   select ROW_NUMBER() over (
     partition by 
       Dep
     --, tariffkode
     --, LinkTariffType
     --, poliata
     --, poliatavia
     --, podiata
     --, podiatavia
     --, PreCarriage
     --, PortTerminalId
     --, Product
     --, RoutingOrder
     --, PrepaidCollect
     --, isnull(description, '')
     --, ScaleCalcCode
     --, isnull(scalefrom, 0)
     --, isnull(scaleto, 0)
     --, CurrencyCode
     --, Base 
     order by LinkTariffType desc
     ) Record
    , *

   from (
    select tn.LinkTariffType
     , tn.Dep
     , tn.POLIata
     , tn.POLIatavia
     , tn.PODIata
     , tn.PODIatavia
     , tn.CurrencyCode
     , tn.LegalEntityID
     , tn.Rate
     , tn.Base
     , tn.Minimum
     , tn.NrDescription
     , tn.Description
     , tn.DateFrom
     , tn.DateUntil
     , tn.DateCreate
     , tn.DateMod
     , tn.ModName
     , tn.Tariffkode
     , tn.ExpiryDate
     , tn.PClass
     , tn.Maximum
     , tn.RoutingOrder
     , tn.TariffCompType
     , tn.PrePaidCollect
     , tn.Product
     , tn.IsDeleted
     , (
      select distinct Location_IATA
      from Company
      where Called = 'KARL KING'
       and LegalEntityID = 1
       and IsDeleted = 0
      ) as PreCarriage
     , tn.PortTerminalID
     , tn.ScaleFrom
     , tn.ScaleTo
     , tn.ScaleCalcCode
     , tn.Mandatory
     , (
      select CompanyID
      from PlaceOfReceipt
      where warehouse = 'KARL KING'
       and LegalEntityID = 1
       and OfficeID = 13
       and IsDeleted = 0
      ) as WarehouseID
     , tn.TariffRelID
     , tn.FreeDescription
     , 0 as UnnamedColumn
     , tn.ShipCode
     , tn.AgentID
     , tn.ContainerTypeID
     , tn.CommodityID
     , 0 as TempTable
    from TariffNew tn
        inner join hhInvoiceLines inv on tn.Tariffkode = inv.NrInvoiceLine
    where tn.PreCarriage is not null
      and tn.PreCarriage != ''
      and tn.POLIata is not null
      and tn.POLIata != ''
      and inv.OfficeID = 13
      and inv.IsDeleted = 0
      and inv.LegalEntityID = 1
      and tn.LegalEntityID = 1
      and tn.Dep = 'E'
      and tn.IsDeleted = 0
      and tn.DateUntil = '2078-12-31 00:00:00'
      and tn.Description = 'kgl'
    ) as s
   ) as b
  where b.Record = 1

【讨论】:

  • 感谢您的回复。在语句“ ) as b where b.Record = 1) ”被写入的行仍然得到相同的错误。错误提示“) 附近的语法不正确。需要 AS、ID 或 QUOTED_ID。”同时,我找到了另一种解决此问题的方法。我已经使用 CTE 来存储来自内部选择查询的所有行,然后我将分区应用于 CTE 的内容。
  • @Che 乐于助人!
猜你喜欢
  • 2017-09-16
  • 1970-01-01
  • 2022-10-15
  • 2014-09-23
  • 2019-08-19
  • 2022-12-22
  • 2014-07-03
  • 1970-01-01
  • 2016-09-03
相关资源
最近更新 更多