2017-02-21 98 views
0

任何人都可以请建议在下面的查询中出现什么错误?其实我想从选择查询输出中删除所有重复项,并只提取唯一的行。提前致谢。从选择查询输出中删除重复项

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 
+2

把它放在Excel中,然后单击”删除重复“按钮?在您将大量未格式化的文字墙倾倒在我们身上后,没有人会帮助您。 – DavidG

+0

SELECT DISTINCT? – Juan

+0

我尝试了不同的。但它不起作用。 – Che

回答

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 
+0

感谢您的回复。仍然在声明“)as b where b.Record = 1)”的行处得到相同的错误“。该错误表示“语法不正确”),期望AS,ID或QUOTED_ID。“同时我找到了另一种解决这个问题的方法。我已经使用CTE来存储内部选择查询中的所有行,然后我已将该分区应用于CTE的内容。 – Che

+0

@Che更新为尝试修复您的其他错误 – SqlZim

+0

非常感谢!它的工作..... :) – Che