あなたがやろうとしていることを見ると、次のようなものを使用してデータを返すことができるかもしれません:
;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 を参照してください
ステートメントOrder By
で使用できる特定の方法でこれを注文する場合は、編集します。CASE
;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 を参照してください