Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8420637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:55:29+00:00 2026-06-10T02:55:29+00:00

Iv’e got a SQL query that I hope you can help me figure out.

  • 0

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

  1. Return one category per product. (lowest sort)
  2. Product Must be in active category.
  3. Use AlternateCategory if available but not required.
  4. 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' 
                                               ) 
              ) 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:55:29+00:00Added an answer on June 10, 2026 at 2:55 am

    Here is a SQLFiddle demo. This query selects Category with min(TblCategory.Sort). If you need to select category with min(TblProductXCategories.SortOrder) just replace order by column in OVER statement toTblProductXCategories.SortOrder. It works fine with duplicate sortorders

    with t as 
    (select TblProductXCategories.*,TblCategories.Name CatName,
     row_number() over (partition by ProductID order by TblCategories.Sort) rownum 
      from TblProductXCategories
       join TblCategories on  TblProductXCategories.CategoryId = TblCategories.id 
             and TblCategories.Active=1 
     )
    select TblProducts.SKU,TblProducts.Price,t.CatName,
    
    (select top 1 AltName from TblAlternateCategoryName 
       where TblAlternateCategoryName.CategoryId=t.CategoryId order by Id )
    from t 
    left join TblProducts on t.productid=TblProducts.id
    where rownum=1
    

    or an equivalent without WITH. SQLFiddle:

    select TblProducts.SKU,TblProducts.Price,t.CatName,
    (select top 1 AltName 
            from TblAlternateCategoryName 
            where TblAlternateCategoryName.CategoryId=t.CategoryId 
            order by Id ) AltCat
    
    from
    
    (select TblProductXCategories.*,
            TblCategories.Name CatName,
            row_number() 
             over (partition by ProductID order by TblCategories.Sort) rownum 
     from TblProductXCategories
     join TblCategories  on  TblProductXCategories.CategoryId = TblCategories.id 
           and TblCategories.Active=1 
    ) t
    
    left join TblProducts on t.productid=TblProducts.id
    where rownum=1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.