I keep getting a "missing operator" error on Access and SQL. Query worked before I tried joining table to itself - sql

SELECT ap.ID, ap.[Adjustment Name], ap.[Adjustment Name Description], ap.[2nd Item Number], [ap].Description, ap.[Unit of Measure], ap.[Effective Date], ap.[Expired Date], MAX( ap.[Factor Value Numeric] ) , ap.[Prc Cls], ap.[Prc Cls Description], ap.[Address Number], ap.[Sales Detail Value 01], ap.[Currency Code], c.[Customer Pricing Rule], c.[Alpha Name]
FROM [Adv Price Query Export] ap
INNER JOIN ( SELECT [Adjustment Name], [2nd Item Number], MAX([Effective Date]), [Factor Value Numeric], [Sales Detail Value 01]
FROM [Adv Price Query Export] ) s ON ((s.[Adjustment Name] = ap.[Adjustment Name]) AND (s.[Effective Date] = ap.[Effective Date]) AND (s.[Sales Detail Value 01] = ap.[Sales Detail Value 01]))
INNER JOIN Customer c ON (ap.[Adjustment Name] = c.[Adjustment Schedule])
WHERE ( ap.[2nd Item Number] = "18500" OR ap.[2nd Item Number] = "185047" OR ap.[2nd Item Number] = "18550" OR ap.[2nd Item Number] = "26004" OR ap.[2nd Item Number] = "55010" )
GROUP BY ap.[Sales Detail Value 01]

Not sure where the error is but you can re-write your query like below
SELECT ap.ID,
ap.[Adjustment Name],
ap.[Adjustment Name Description],
ap.[2nd Item Number],
[ap].Description,
ap.[Unit of Measure],
ap.[Effective Date],
ap.[Expired Date],
MAX( ap.[Factor Value Numeric] ) ,
ap.[Prc Cls],
ap.[Prc Cls Description],
ap.[Address Number],
ap.[Sales Detail Value 01],
ap.[Currency Code],
c.[Customer Pricing Rule],
c.[Alpha Name]
FROM [Adv Price Query Export] ap
INNER JOIN ( SELECT [Adjustment Name],
[2nd Item Number],
MAX([Effective Date]),
[Factor Value Numeric],
[Sales Detail Value 01]
FROM [Adv Price Query Export] ) s
ON s.[Adjustment Name] = ap.[Adjustment Name]
AND s.[Effective Date] = ap.[Effective Date]
AND s.[Sales Detail Value 01] = ap.[Sales Detail Value 01]
INNER JOIN Customer c ON ap.[Adjustment Name] = c.[Adjustment Schedule]
WHERE ap.[2nd Item Number] IN ("18500", "185047", "18550", "26004", "55010" )
GROUP BY ap.[Sales Detail Value 01]

Related

Incorrect Syntax Left Join subquery with aggregation

I'm trying to run the following query through SQL Server. I keep getting a incorrect syntax error near',' but I can't figure out which comma is incorrect. I'm pretty new to SQL but especially still trying to figure out more complex queries.
SELECT
lastdate.[Date of Record],
billing.[Club Code],
billing.[Club Name],
lastdate.[Member Code with Name],
billing.[Activity Code],
billing.[Category Code],
billing.[Dues Net Amount],
billing.[Dues Gross Amount],
billing.[Member Type Code],
billing.[Member Join Date],
billing.[Member Status Rule Code]
FROM
[dbo].[view_Club_Transactions_0100_(15) Dues_Summary] billing
LEFT JOIN
(MAX(lastdate.[Date of Record]) dor,
lastdate.[Member Code with Name]
FROM
[dbo].[view_Club_Transactions_0100_(15) Dues_Summary] lastdate
GROUP BY
lastdate.[Member Code with Name])
ON
billing.[Member Code with Name]=lastdate.[Member Code with Name]
WHERE
([Member Status Rule Code] = N'ZRESIGN')
AND
([Activity Code] = N'DUES')
Your subquery is missing the SELECT keyword... And an alias too. You also need to align the subquery column alias for the date column with the outer query:
SELECT
lastdate.[Max Date of Record], ---------------> column alias
billing.[Club Code],
...
FROM [dbo].[view_Club_Transactions_0100_(15) Dues_Summary] billing
LEFT JOIN (
SELECT -------------------------------> "SELECT" keyword
MAX([Date of Record]) [Max Date of Record], ---> column alias
[Member Code with Name]
FROM [dbo].[view_Club_Transactions_0100_(15) Dues_Summary]
GROUP BY [Member Code with Name]
) lastdate -------------------------------> subquery alias
ON billing.[Member Code with Name]=lastdate.[Member Code with Name]
WHERE ...
I actually suspect that you can skip the self join and use window functions instead. That could be:
SELECT *
FROM (
SELECT
MAX([Date of Record]) OVER(PARTITION BY [Member Code with Name]) [Max Date of Record],
[Club Code],
[Club Name],
[Member Code with Name],
[Activity Code],
[Category Code],
[Dues Net Amount],
[Dues Gross Amount],
[Member Type Code],
[Member Join Date],
[Member Status Rule Code]
FROM [dbo].[view_Club_Transactions_0100_(15) Dues_Summary]
) t
WHERE [Member Status Rule Code] = N'ZRESIGN' AND [Activity Code] = N'DUES'

Getting Null Value joining 3 tables

Below is the Query,I had joined 3 tables supplier is main table. The actual scenario is I want all the data from table account payable table though it is not their in purchase order table, so I have joined using FULL Outer with supplier and Purchase Order, but the supplier details were not coming against the data of account payable though supplier key is available.
SELECT ISNULL(dbo.Supplier.supplier_key,dbo.Fact_AccountPayables.supplier_key) AS supplier_key,
dbo.Supplier.Supplier,
dbo.Supplier.Name,
dbo.Supplier.Status,
dbo.Supplier.AddressCode,
dbo.Supplier.Address,
dbo.Supplier.HouseNo,
dbo.Supplier.Street,
dbo.Supplier.City,
dbo.Supplier.Country,
dbo.Supplier.ZipCode,
dbo.Supplier.StartDate,
dbo.Supplier.CreditLimit,
dbo.Supplier.FinancialGroup,
dbo.Supplier.LastTransactionDate,
dbo.Fact_PurchaseOrder.Company,
ISNULL(dbo.Fact_PurchaseOrder.[Purchase Order],dbo.Fact_AccountPayables.[PO Number]) AS PurchaseOrder,
ISNULL( dbo.Fact_PurchaseOrder.Sequence,dbo.Fact_AccountPayables.Line) AS POSequence,
dbo.Fact_PurchaseOrder.[Order Quantity],
dbo.Fact_PurchaseOrder.[Per Purchase Unit],
dbo.Fact_PurchaseOrder.[Per Quantity Price],
dbo.Fact_PurchaseOrder.[Purchase price unit],
dbo.Fact_PurchaseOrder.[Total Order Amount],
dbo.Fact_PurchaseOrder.Currency,
dbo.Fact_PurchaseOrder.[Rate Date],
dbo.Fact_PurchaseOrder.[Actual Receipt Date],
dbo.Fact_PurchaseOrder.[Receipt No],
dbo.Fact_PurchaseOrder.[Receipt Sequence],
dbo.Fact_PurchaseOrder.[Received Quantity],
dbo.Fact_PurchaseOrder.[Approved Quantity],
dbo.Fact_PurchaseOrder.[Purchase Office],
dbo.Fact_PurchaseOrder.[Invoice Number],
dbo.Fact_PurchaseOrder.[Invoice Date],
dbo.Fact_PurchaseOrder.[Invoice Quantity],
dbo.Fact_PurchaseOrder.[Invoice Amount],
dbo.Fact_AccountPayables.InvoiceNumber,
dbo.Fact_AccountPayables.Type AS InvoiceType,
dbo.Fact_AccountPayables.[Order Type] AS OrderInvoiceType,
dbo.Fact_AccountPayables.AP_Balance_EUR,
dbo.Fact_AccountPayables.[Invoice Amount_EUR],
dbo.Fact_AccountPayables.supplier_key AS EXPR2,
dbo.Fact_AccountPayables.[IntercompanyTrade Order No] AS EXPR23,
dbo.Fact_AccountPayables.[IntercompanyTrade Line Number] AS EXPR24,
dbo.Fact_AccountPayables.[Intercompany Trade Financial Company] AS EXPR25,
dbo.Fact_AccountPayables.[Intercompany Trade Purchase Company] AS EXPR26,
dbo.Fact_AccountPayables.InvoiceNumber,
dbo.Fact_AccountPayables.DueDate,
dbo.Fact_AccountPayables.DocDate,
dbo.Fact_PurchaseOrder.[Order Date],
dbo.Fact_AccountPayables.[Invoice Amount_EUR],
(CASE WHEN dbo.Fact_PurchaseOrder.[Receipt No] = ' ' THEN dbo.Fact_PurchaseOrder.[Total Order Amount]
WHEN dbo.Fact_PurchaseOrder.[Receipt No] != ' ' and dbo.Fact_AccountPayables.InvoiceNumber IS NULL then dbo.Fact_PurchaseOrder.[Total Order Amount] END) AS ORDERBALANCE,
(dbo.Supplier.CreditLimit -(ORDERBALANCE + dbo.Fact_AccountPayables.[Invoice Amount_EUR])) AS Availablecredit
FROM dbo.Supplier
LEFT OUTER JOIN dbo.Fact_PurchaseOrder ON dbo.Supplier.supplier_key = dbo.Fact_PurchaseOrder.buyfrom_supplier_key
full OUTER JOIN dbo.Fact_AccountPayables ON dbo.Fact_AccountPayables.supplier_key = dbo.Supplier.supplier_key AND
dbo.Fact_AccountPayables.[PO Number] = dbo.Fact_PurchaseOrder.[Purchase Order] AND
dbo.Fact_AccountPayables.[PO Line] = dbo.Fact_PurchaseOrder.Sequence
The output is like this:
Try your Payable table after that FROM Clause and join the other tables by using LEFT JOIN
FROM dbo.Fact_AccountPayables
LEFT JOIN dbo.Fact_PurchaseOrder ON dbo.Fact_AccountPayables.[PO Number] = dbo.Fact_PurchaseOrder.[Purchase Order] AND
dbo.Fact_AccountPayables.[PO Line] = dbo.Fact_PurchaseOrder.Sequence
LRFT JOIN dbo.Supplier ON dbo.Supplier.supplier_key = dbo.Fact_PurchaseOrder.buyfrom_supplier_key AND dbo.Fact_AccountPayables.supplier_key = dbo.Supplier.supplier_key
You have used not only supplier key, but also PO number and PO line as join criterion with full outer join. If there is no matching entry for these (matching entries for ALL join criteria in the same time) in supplier table you will also see the null values you mentioned.
Just for testing remove temporarily the other join criteria (PO number and line) and leave only the supplier key. Check if you still see the null values in this case.
You're using a FULL join for the Fact_AccountPayables table. This way, all records from Fact_AccountPayables are included even if there are no matches with the other two tables. If you don't want that, use LEFT join instead of FULL join.
SELECT
ISNULL(dbo.Supplier.supplier_key, dbo.Fact_AccountPayables.supplier_key) AS supplier_key,
dbo.Supplier.Supplier,
dbo.Supplier.Name,
dbo.Supplier.Status,
dbo.Supplier.AddressCode,
dbo.Supplier.Address,
dbo.Supplier.HouseNo,
dbo.Supplier.Street,
dbo.Supplier.City,
dbo.Supplier.Country,
dbo.Supplier.ZipCode,
dbo.Supplier.StartDate,
dbo.Supplier.CreditLimit,
dbo.Supplier.FinancialGroup,
dbo.Supplier.LastTransactionDate,
dbo.Fact_PurchaseOrder.Company,
ISNULL(dbo.Fact_PurchaseOrder.[Purchase Order], dbo.Fact_AccountPayables.[PO Number]) AS PurchaseOrder,
ISNULL(dbo.Fact_PurchaseOrder.Sequence, dbo.Fact_AccountPayables.Line) AS POSequence,
dbo.Fact_PurchaseOrder.[Order Quantity],
dbo.Fact_PurchaseOrder.[Per Purchase Unit],
dbo.Fact_PurchaseOrder.[Per Quantity Price],
dbo.Fact_PurchaseOrder.[Purchase price unit],
dbo.Fact_PurchaseOrder.[Total Order Amount],
dbo.Fact_PurchaseOrder.Currency,
dbo.Fact_PurchaseOrder.[Rate Date],
dbo.Fact_PurchaseOrder.[Actual Receipt Date],
dbo.Fact_PurchaseOrder.[Receipt No],
dbo.Fact_PurchaseOrder.[Receipt Sequence],
dbo.Fact_PurchaseOrder.[Received Quantity],
dbo.Fact_PurchaseOrder.[Approved Quantity],
dbo.Fact_PurchaseOrder.[Purchase Office],
dbo.Fact_PurchaseOrder.[Invoice Number],
dbo.Fact_PurchaseOrder.[Invoice Date],
dbo.Fact_PurchaseOrder.[Invoice Quantity],
dbo.Fact_PurchaseOrder.[Invoice Amount],
dbo.Fact_AccountPayables.InvoiceNumber,
dbo.Fact_AccountPayables.Type AS InvoiceType,
dbo.Fact_AccountPayables.[Order Type] AS OrderInvoiceType,
dbo.Fact_AccountPayables.AP_Balance_EUR,
dbo.Fact_AccountPayables.[Invoice Amount_EUR],
dbo.Fact_AccountPayables.supplier_key AS EXPR2,
dbo.Fact_AccountPayables.[IntercompanyTrade Order No] AS EXPR23,
dbo.Fact_AccountPayables.[IntercompanyTrade Line Number] AS EXPR24,
dbo.Fact_AccountPayables.[Intercompany Trade Financial Company] AS EXPR25,
dbo.Fact_AccountPayables.[Intercompany Trade Purchase Company] AS EXPR26,
dbo.Fact_AccountPayables.InvoiceNumber,
dbo.Fact_AccountPayables.DueDate,
dbo.Fact_AccountPayables.DocDate,
dbo.Fact_PurchaseOrder.[Order Date],
dbo.Fact_AccountPayables.[Invoice Amount_EUR],
CASE
WHEN dbo.Fact_PurchaseOrder.[Receipt No] = ' ' THEN dbo.Fact_PurchaseOrder.[Total Order Amount]
WHEN dbo.Fact_AccountPayables.InvoiceNumber IS NULL THEN dbo.Fact_PurchaseOrder.[Total Order Amount]
END AS ORDERBALANCE,
dbo.Supplier.CreditLimit - (ORDERBALANCE + dbo.Fact_AccountPayables.[Invoice Amount_EUR]) AS Availablecredit
FROM
dbo.Supplier
LEFT JOIN dbo.Fact_PurchaseOrder ON dbo.Supplier.supplier_key = dbo.Fact_PurchaseOrder.buyfrom_supplier_key
LEFT JOIN dbo.Fact_AccountPayables ON
dbo.Fact_AccountPayables.supplier_key = dbo.Supplier.supplier_key AND
dbo.Fact_AccountPayables.[PO Number] = dbo.Fact_PurchaseOrder.[Purchase Order] AND
dbo.Fact_AccountPayables.[PO Line] = dbo.Fact_PurchaseOrder.Sequence
Some of the rows are getting filtered in the Fact_PurchaseOrder, if they dont have supplier. You need to go for FULL OUTER JOIN for all tables, to get data for all rows of AccountPayables, irrespective of whether they have got suppliers or not.
FROM dbo.Supplier
FULL OUTER JOIN dbo.Fact_PurchaseOrder ON dbo.Supplier.supplier_key = dbo.Fact_PurchaseOrder.buyfrom_supplier_key
FULL OUTER JOIN dbo.Fact_AccountPayables ON dbo.Fact_AccountPayables.supplier_key = dbo.Supplier.supplier_key AND
dbo.Fact_AccountPayables.[PO Number] = dbo.Fact_PurchaseOrder.[Purchase Order] AND
dbo.Fact_AccountPayables.[PO Line] = dbo.Fact_PurchaseOrder.Sequence

Union Query using AS creates issues with ORDER BY in SQL

I am outputting a report for another department and they require specific headers (Excel cell column headers). I have a union query to output the information.
All of it works fine except the ORDER BY section.
If I use the full tblInventory.[Employee Number] AS [Employee No], I get a "Missing Operator" error and it highlights the AS.
If you just put ORDER BY [Employee No] it has problems with the DISTINCT claus which I need.
Any ideas on what operator it needs or how I can get this to sort?
SELECT DISTINCT tblinventory.[Phone Number] AS [Wireless No],
tblemployeelist.[Employee Number] AS [Employee No],
tblemployeelist.[Payroll First Name] AS [First Name],
tblemployeelist.[Payroll Last Name] AS [Last Name],
tblvendors.[Vendor Name] AS [Wireless Carrier],
"Company" AS [Acct Liability]
FROM tblvendors
INNER JOIN (tblemployeelist
INNER JOIN tblinventory ON tblemployeelist.[Employee Number] = tblinventory.[Employee Number])
AND (tblemployeelist.[Employee Number] = tblinventory.[Employee Number])) ON tblvendors.id = tblinventory.carrier
WHERE (((tblinventory.[Phone Number]) IS NOT NULL)
AND ((tblvendors.[Vendor Name]) <>"Roadpost"
AND (tblvendors.[Vendor Name]) <>"LIVETV Airfone Inc.")
AND ((tblinventory.[Asset Description]) LIKE "*" & "phone" & "*")
AND ((tblinventory.disposition) =2)
AND ((tblinventory.spare) =FALSE)
AND ((tblemployeelist.[End Date]) NOT LIKE "*"))
ORDER BY ([tblEmployeeList].[Employee Number] AS [Employee No])
UNION
SELECT tblmcpcollated.[Phone Number] AS [Wireless No],
tblemployeelist.[Employee Number] AS [Employee No],
tblemployeelist.[Payroll First Name] AS [First Name],
tblemployeelist.[Payroll Last Name] AS [Last Name],
tblvendors.[Vendor Name] AS [Wireless Carrier],
"Employee" AS [Acct Liability]
FROM tblvendors
INNER JOIN (tblemployeelist
INNER JOIN tblmcpcollated ON tblemployeelist.[Employee Number] = tblmcpcollated.[Employee Number]) ON tblvendors.id = tblmcpcollated.vendor
WHERE (((tblmcpcollated.[Phone Number]) IS NOT NULL)
AND ((tblmcpcollated.status)="Active")
AND ((tblmcpcollated.[MCP Program])<>1)
AND ((tblmcpcollated.[Compensation Amt])>0)
AND ((tblemployeelist.[End Date]) NOT LIKE "*"))
OR (((tblmcpcollated.[Phone Number]) IS NOT NULL)
AND ((tblmcpcollated.status)="Pending")
AND ((tblmcpcollated.[MCP Program])<>1)
AND ((tblmcpcollated.[Compensation Amt])>0)
AND ((tblemployeelist.[End Date]) NOT LIKE "*"))
ORDER BY ([tblEmployeeList].[Employee Number] AS [Employee No]);
If I remove the ORDER BY, everything works. I just would like the sort function in there.
Thanks in advance for your awesome knowledge.
You need to remove the column name from the ORDER BY. As you stated it's throwing an error around the keyword AS.
You need:
ORDER BY ([tblEmployeeList].[Employee Number])
not:
ORDER BY ([tblEmployeeList].[Employee Number] AS [Employee No])
As I'm not able de read your SQL, this should do it and will be transparent for the query builder :
SELECT *
FROM
(
SELECT DISTINCT tblInventory.[Phone Number] AS [Wireless No],
tblEmployeeList.[Employee Number] AS [Employee No], tblEmployeeList.[Payroll
First Name] AS [First Name], tblEmployeeList.[Payroll Last Name] AS [Last
Name], tblVendors.[Vendor Name] AS [Wireless Carrier], "Company" AS [Acct
Liability]
FROM tblVendors INNER JOIN (tblEmployeeList INNER JOIN tblInventory ON
tblEmployeeList.[Employee Number] = tblInventory.[Employee Number]) AND
(tblEmployeeList.[Employee Number] = tblInventory.[Employee Number])) ON
tblVendors.ID = tblInventory.Carrier
WHERE (((tblInventory.[Phone Number]) Is Not Null) AND ((tblVendors.[Vendor
Name])<>"Roadpost" And (tblVendors.[Vendor Name])<>"LIVETV Airfone Inc.")
AND ((tblInventory.[Asset Description]) Like "*" & "phone" & "*") AND
((tblInventory.Disposition)=2) AND ((tblInventory.Spare)=False) AND
((tblEmployeeList.[End Date]) Not Like "*"))
ORDER BY ([tblEmployeeList].[Employee Number] AS [Employee No])
UNION SELECT tblMCPCollated.[Phone Number] as [Wireless No],
tblEmployeeList.[Employee Number] as [Employee No], tblEmployeeList.[Payroll
First Name] as [First Name], tblEmployeeList.[Payroll Last Name] as [Last
Name], tblVendors.[Vendor Name] as [Wireless Carrier], "Employee" as [Acct
Liability]
FROM tblVendors INNER JOIN (tblEmployeeList INNER JOIN tblMCPCollated ON
tblEmployeeList.[Employee Number] = tblMCPCollated.[Employee Number]) ON
tblVendors.ID = tblMCPCollated.Vendor
WHERE (((tblMCPCollated.[Phone Number]) Is Not Null) AND
((tblMCPCollated.Status)="Active") AND ((tblMCPCollated.[MCP Program])<>1)
AND ((tblMCPCollated.[Compensation Amt])>0) AND ((tblEmployeeList.[End
Date]) Not Like "*")) OR (((tblMCPCollated.[Phone Number]) Is Not Null) AND
((tblMCPCollated.Status)="Pending") AND ((tblMCPCollated.[MCP Program])<>1)
AND ((tblMCPCollated.[Compensation Amt])>0) AND ((tblEmployeeList.[End
Date]) Not Like "*"))
) t1
ORDER BY t1.[Employee No];

Ambiguous Column Name with Group By

I am having Ambiguous Column Name "Item" error for the query below. However, I already type in the desired form as parameters are at the beginning of columns.
SELECT
[Country Code],
Item,
[FE SSO],
[Newest Job Number],
[Newest Transaction Date],
Z.ConsignDate AS [ConsignDate],
FROM DailyOnhand
LEFT JOIN
(SELECT
[Job Number],
[Item],
Min([Transaction Day]) AS ConsignDate
FROM vwAllTxns
GROUP BY [Job Number], [Item]) Z
ON vwDailyOnhand_v2.[Newest Job Number] = Z.[Job Number]
AND vwDailyOnhand_v2.[Item] = Z.[Item]
Any help is appreciated.
Thank you!
You need to prefix item in your select with the name/alias of the table it is sourced from.
SELECT
d.[Country Code],
d.Item,
d.[FE SSO],
d.[Newest Job Number],
d.[Newest Transaction Date],
Z.ConsignDate AS [ConsignDate],
FROM DailyOnHand d
LEFT JOIN
(SELECT
v.[Job Number],
v.[Item],
Min(v.[Transaction Day]) AS ConsignDate
FROM vwAllTxns v
GROUP BY v.[Job Number], v.[Item]
) Z
ON d.[Newest Job Number] = Z.[Job Number]
AND d.[Item] = Z.[Item]
Your from specifies DailyOnHand but your on specifies vwDailyOnhand_v2, I removed the later and used an alias instead.
SELECT
[Country Code],
Z.[Item], -- Need to specify which item is source
[FE SSO],
[Newest Job Number],
[Newest Transaction Date],
Z.ConsignDate AS [ConsignDate],
FROM DailyOnhand
LEFT JOIN
(SELECT
[Job Number],
[Item],
Min([Transaction Day]) AS ConsignDate
FROM vwAllTxns
GROUP BY [Job Number], [Item]) Z
ON vwDailyOnhand_v2.[Newest Job Number] = Z.[Job Number]
AND vwDailyOnhand_v2.[Item] = Z.[Item]

display unique field out of 10 records in sql

My sql query is
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_]
And my result is
In this out of 21 rows i am having 17 rows as kit. I need to group this kit and to display in Old Product Code as Kit in one row instead of 17 rows.
Looking at what you are trying to do you might be able to use something like this to return the data:
;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)
See SQL Fiddle with Demo
Edit, if you want to order this in a specific way you can use Order By with a CASE statement:
;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]
See SQL Fiddle with Demo