Iv'e got a SQL query that I hope you can help me figure out.
This should be easy, My head is just not producing today.
Here is a sample of the tables
TblProducts
ID | SKU | Price
-----------------
1 | ABC | 10.00
2 | DEF | 5.00
3 | OSKD | 6.00
4 | 123 | 6.00
5 | LPD | 12.00
6 | TRE | 3.00
TblCategories
ID | Name | Active |Sort
-------------------------
1 | Home | 1 |4
2 | Garden| 1 |55
3 | Misc | 1 |2
4 | Test | 0 |1
TblAlternateCategoryName
ID | CategoryID | AltName
-------------------------
1 | 1 | House
2 | 1 | Crib
3 | 3 | Anything
TblProductXCategories
ID | ProductID | CategoryID | SortOrder
---------------------------------------
1 | 1 | 1 | 1
2 | 1 | 2 | 1
4 | 2 | 2 | 4
5 | 2 | 3 | 6
6 | 3 | 3 | 6
7 | 4 | 4 | 0
8 | 5 | 2 | 1
need this result
SKU | Price | Category | AlternateCategory
-------------------------------------------
ABC | 10.00 | Home | House
DEF | 5.00 | Misc | Anything
OSKD | 6.00 | Misc | Anything
LPD | 12.00 | Garden | Null
Rules
- Return one category per product. (lowest sort)
- Product Must be in active category.
- Use AlternateCategory if available but not required.
- sort could sometimes be duplicate.
Thanks in advance
Here is the original SQL Statment
DECLARE @feedID int =4
SELECT Pro_Chl.id,
Pro_Chl.sku,
Pro_Chl.productname,
(SELECT top 1 tbl_componentsettinglist.componentsubtype
FROM tbl_offers
INNER JOIN tbl_componentsettinglist
ON tbl_offers.id = tbl_componentsettinglist.componentid
WHERE ( tbl_componentsettinglist.componentsubtype = N'Free Shipping' )
AND ( tbl_componentsettinglist.componenttype = N'Offer' )
AND ( tbl_offers.startdate <= { fn NOW() } )
AND ( tbl_offers.enddate > { fn NOW() } )
AND ( tbl_offers.enabled = 1 )
AND ( Pro_Chl.id = tbl_componentsettinglist.setting1 )
ORDER BY tbl_offers.[order]) AS FreeShipping,
TblCategories.name AS CategoryName,
TblAlternateCategoryName.value AS FeedCat
FROM TblProducts AS Pro_Chl
INNER JOIN (
SELECT productid,
categoryid,
sortorder
FROM TblProductXCategories main
WHERE sortorder = (
SELECT top 1 Min(srt.sortorder)
FROM TblProductXCategories srt
INNER JOIN TblCategories
ON srt.categoryid =
TblCategories.id
WHERE srt.productid = main.productid
AND srt.categoryid = main.categoryid
AND TblCategories.hidden = 0
)
)
AS PxC
ON ( Pro_Chl.id = PxC.productid
OR Pro_Chl.parentid = PxC.productid )
INNER JOIN TblCategories
ON PxC.categoryid = TblCategories.id
LEFT OUTER JOIN TblAlternateCategoryName
ON PxC.categoryid = TblAlternateCategoryName.categoryid AND TblAlternateCategoryName.feedid = @feedID
WHERE (
( Pro_Chl.parentid = '' )
AND ( Pro_Chl.id NOT IN (SELECT parentid
FROM TblProducts AS pc
WHERE ( customproperties LIKE '%upc%' )) )
AND ( Pro_Chl.status = 1 )
AND ( Pro_Chl.manufacturerid IS NOT NULL )
AND ( Pro_Chl.manufacturerid <> '' )
AND ( Pro_Chl.manufacturerid <> '- No Manufacturer -' )
AND ( Pro_Chl.id NOT IN (SELECT productid
FROM TblProductschoicecombinations
WHERE available = 0) )
AND Pro_Chl.manufacturerid NOT IN (
'f46c9a25-8172-49a8-991a-a8219663453b' )
)
OR
(
( Pro_Chl.parentid <> '' )
AND ( Pro_Chl.customproperties LIKE '%upc%' )
AND ( Pro_Chl.parentid IN (SELECT id
FROM TblProducts AS cp
WHERE ( status = 1 )
AND ( parentid = '' )) )
AND ( Pro_Chl.status = 1 )
AND ( Pro_Chl.manufacturerid IS NOT NULL )
AND ( Pro_Chl.manufacturerid <> '' )
AND ( Pro_Chl.manufacturerid <> '- No Manufacturer -' )
AND ( Pro_Chl.id NOT IN (SELECT productid
FROM TblProductschoicecombinations
WHERE available = 0) )
AND Pro_Chl.manufacturerid NOT IN (
'f46c9a25-8172-49a8-991a-a8219663453b'
)
)