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 7033391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:03:37+00:00 2026-05-28T01:03:37+00:00

I want to create conditional join to a table in my T-SQL query. Tables

  • 0

I want to create conditional join to a table in my T-SQL query. Tables used in this example are from Northwind database (with only one additional table ProductCategories)

Table Products and table Categories have many-to-many relationship, hence table ProductCategories comes into picture.

I need the sum of Quantity column on table OrderDetails for each of the products falling under certain category. So I have a query like one below

Select p.ProductName, Sum(od.Quantity) As Qty
From Products p
    Join OrderDetails od On od.ProductID = p.ProductID
    Join ProductCategories pc On pc.ProductID = p.ProductID
        And pc.CategoryID = @CategoryID
Group By p.ProductName

@CategoryID is an optional parameter. So in case it’s not supplied, the join to table ProductCategories will not be required
and query should look like one below

Select p.ProductName, Sum(od.Quantity) As Qty
From Products p
    Join OrderDetails od On od.ProductID = p.ProductID
Group By p.ProductName

I want to achieve this without repeating the whole query with If conditions (as below)

If @CategoryID Is Null
    Select p.ProductName, Sum(od.Quantity) As Qty
    From Products p
        Join OrderDetails od On od.ProductID = p.ProductID
    Group By p.ProductName
Else
    Select p.ProductName, Sum(od.Quantity) As Qty
    From Products p
        Join OrderDetails od On od.ProductID = p.ProductID
        Join ProductCategories pc On pc.ProductID = p.ProductID
            And pc.CategoryID = @CategoryID
    Group By p.ProductName

This is simplified version of the query which has many other tables and conditions like ProductCategories. And will require multiple multiple If conditions and repetition of the query. I have also tried dynamically generating the query. It works but query is not readable at all.

Any solutions?
Thank you.

  • 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-05-28T01:03:37+00:00Added an answer on May 28, 2026 at 1:03 am

    Try this, if you’ll use properly parametrized query – there will be no performance impact but may be gain:

    Select p.ProductName, Sum(od.Quantity) As Qty
    From Products p
        Join OrderDetails od On od.ProductID = p.ProductID
    WHERE @CategoryID IS NULL OR EXISTS (SELECT * FROM ProductCategories WHERE CategoryID = @CategoryID AND ProductID = p.ProductID)
    Group By p.ProductName
    

    Actually

    in your query if there can be multiple rows in ProductCategories for one row in OrderDetails – then you get duplicates of od.Quantity in your SUM – is it an intended behavior?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create conditional comments in XSLT. But when I use this: <!--
I want to avoid string concatenation to create dynamic SQL query in SQL 2008.
Following on from my last question Sql Server query performance , and discovering that
I want to return four conditional aggregates from a function. I tried using CASE
Is there a way to create a conditional binding in XAML? Example: <Window x:Name=Me
I am trying to use ICriteria to create a query that has a join
I want create a drop shadow around the canvas component in flex. Technically speaking
I want create a excel with Apache POI in java and I must insert
i want create image animation , i have 50 images with png format now
I want to create a Java application bundle for Mac without using Mac. According

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.