在sql中显示10条记录中的唯一字段

时间:2012-12-18 11:55:28

标签: sql sql-server sql-server-2008 tsql

我的SQL查询是

select I.[Old Product Code], 
    I.[Trade Name], 
    I.[Short Name], 
    SIL.[BOM Item No_] ,
    CASE when SIL.[Dimension Group Code] = 'IOL' 
        then I.[Group Description] 
        else I.[Short Name] END as GD,
    CASE when SIL.[BOM Item No_] <> '' 
        then 'Kit' end
from [Sales Invoice Header] SIH, [Sales Invoice Line]  SIL, [Item] I 
where I.No_ =  SIL.No_ 
    and SIL.[Document No_] = 'PEXP1213-153' 
    and SIH.No_ = SIL.[Document No_] 
group by I.[Old Product Code], I.[Trade Name], I.[Short Name],  
    SIL.[Dimension Group Code], I.[Group Description], SIL.[BOM Item No_]

我的结果是

current output

在这21行中,我有17行作为工具包。我需要对此工具包进行分组,并在旧产品代码中显示为一行而不是17行。

1 个答案:

答案 0 :(得分:0)

看看你想要做什么,你可以使用这样的东西来返回数据:

;with data as
(
  select I.[Old Product Code], 
      I.[Trade Name], 
      I.[Short Name], 
      SIL.[BOM Item No_] ,
      CASE when SIL.[Dimension Group Code] = 'IOL' 
          then I.[Group Description] 
          else I.[Short Name] END as GD,
      CASE when SIL.[BOM Item No_] <> '' 
          then 'Kit' end CombinedKit
  from SalesInvoiceHeader SIH
  inner join SalesInvoiceLine  SIL
    on SIH.No_ = SIL.[Document No_] 
  inner join Item I 
    on I.No_ =  SIL.No_ 
  where SIL.[Document No_] = 'PEXP1213-153' 
),
d2 as
(
  select [Old Product Code],
    [Trade Name],
    [Short Name],
    [BOM Item No_],
    GD,
    CombinedKit,
    row_number() 
      over(partition by CombinedKit order by [Old Product Code]) rn
  from data
) 
select 
    case when combinedkit = 'kit' 
        then 'Kit' else [Old Product Code] end  [Old Product Code],
    [Trade Name],
    [Short Name],
    [BOM Item No_],
    GD
    --, CombinedKit
from d2
where (CombinedKit = 'Kit' and rn = 1)
  or (CombinedKit is null) 

请参阅SQL Fiddle with Demo

编辑,如果您想以特定方式对此进行排序,则可以将Order ByCASE声明一起使用:

;with data as
(
  select I.[Old Product Code], 
      I.[Trade Name], 
      I.[Short Name], 
      SIL.[BOM Item No_] ,
      CASE when SIL.[Dimension Group Code] = 'IOL' 
          then I.[Group Description] 
          else I.[Short Name] END as GD,
      CASE when SIL.[BOM Item No_] <> '' 
          then 'Kit' end CombinedKit
  from SalesInvoiceHeader SIH
  inner join SalesInvoiceLine  SIL
    on SIH.No_ = SIL.[Document No_] 
  inner join Item I 
    on I.No_ =  SIL.No_ 
  where SIL.[Document No_] = 'PEXP1213-153' 
),
d2 as
(
  select [Old Product Code],
    [Trade Name],
    [Short Name],
    [BOM Item No_],
    GD,
    CombinedKit,
    row_number() 
      over(partition by CombinedKit order by [Old Product Code]) rn
  from data
) 
select 
    case when combinedkit = 'kit' 
        then 'Kit' else [Old Product Code] end  [Old Product Code],
    [Trade Name],
    [Short Name],
    [BOM Item No_],
    GD
    --, CombinedKit
from d2
where (CombinedKit = 'Kit' and rn = 1)
  or (CombinedKit is null) 
order by 
  case when combinedkit = 'kit' then 0 else 1 end, [Old Product Code]

请参阅SQL Fiddle with Demo