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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:23:10+00:00 2026-05-30T23:23:10+00:00

I have the following SQL query to return all Customers who have no OrderLines

  • 0

I have the following SQL query to return all Customers who have no OrderLines with no Parts assigned – i.e. I only want the customers within which every order line of every order has no parts assigned – (in the actual problem I am dealing with a different domain but have translated to customers/orders to illustrate the problem)

SELECT c.Customer_PK
FROM Customers c
  INNER JOIN Orders o
  ON c.Customer_PK = o.Customer_FK
  LEFT OUTER JOIN OrderLines l
  ON o.Order_PK = l.Order_FK
  LEFT OUTER JOIN Parts p
  ON l.OrderLine_PK = p.OrderLine_FK
GROUP BY c.Customer_PK
HAVING COUNT(p.Part_PK) = 0

The best I have come up with in LINQ is as follows:

Dim qry =     
(From c In context.Customers
 Select New With { c.Customer_PK,
                  .CountParts = 
                      (From o In c.Orders
           From l In o.OrderLines
                       Select l.Parts.Count).DefaultIfEmpty.Sum})

qry = (From grp In qry
       Where grp.CountParts  = 0
       Select grp.Customer_PK)

This works but generates less than optimal SQL – it is doing a subquery for Count on each row of the customers query rather than using Group By and Having. I tried making the LINQ Group By syntax work but it kept putting the filter as a WHERE not a HAVING clause.

Any ideas?

Edit in response to Answers below:

I am accepting JamieSee’s answer as it addresses the stated problem, even though it does not produce the GROUP BY HAVING query I originally had.

Thanks Peter and Nick for your input on this. I am a VB developer so I have had a crack translating your code to VB, this is the closest I got to but it does not quite produce the desired output:

Dim qry = From c In context.Customers
          Group Join o In context.Orders On c.Customer_PK Equals o.Customer_FK
          Into joinedOrders = Group
          From jo In joinedOrders.DefaultIfEmpty
          Group Join l In context.OrderLines On jo.Order_PK Equals l.Order_FK
          Into joinedLines = Group
          From jl In joinedLines.DefaultIfEmpty
          Group c By Key = New With {c.Customer_PK, jl} Into grp = Group
          Where Key.jl Is Nothing OrElse Not Key.jl.Parts.Any
          Select c.Customer_PK 

The problem I had is that I have to push “jl” into the Group By “Key” so I can reference it from the Where clause, otherwise the compiler cannot see that variable or any of the other variables appearing before the Group By clause.

With the filter as specified I get all customers where at least one order has lines with no parts rather than only customers with no parts in any order.

  • 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-30T23:23:11+00:00Added an answer on May 30, 2026 at 11:23 pm

    Given that you don’t care about the counts, only the resulting customers, consider the folllowing restatement of the problem:

    Identify all Customers who do not have any Orders that have Lines with Parts.

    This yields:

    var customersWithoutParts = from c in Context.Customers
                                where !(from o in Context.Orders
                                        from l in o.Lines
                                        from p in l.Parts
                                        select o.Customer_FK).Contains(c.Customer_PK)
                                select c.Customer_PK;
    

    This should yield emitted SQL that is roughly equivalent to the following:

    SELECT     c.Customer_PK
    FROM         Customers AS c
    WHERE     (NOT EXISTS
                              (SELECT     o.Cusomer_FK
                                FROM          Orders AS o INNER JOIN
                                                       OrderLines AS l ON o.Order_PK = l.Order_FK INNER JOIN
                                                       Parts AS p ON l.OrderLine_PK = p.OrderLine_FK
                                WHERE      (o.Customer_FK = c.Customer_PK))) 
    

    To get the SQL you were trying to reproduce, I’d start by trying the following:

    var customersWithoutParts = from c in Context.Customers
                                from o in c.Orders.DefaultIfEmpty()
                                from l in o.Lines.DefaultIfEmpty()
                                join part in Context.Parts on part.OrderLine_FK equals l.OrderLine_PK into joinedParts
                                where joinedParts.Count() == 0
                                select c.Customer_PK;
    

    Note that in VB the join here would be replaced by Group Join.

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

Sidebar

Related Questions

I have the following sql query and I want to filter the results where
I have following somewhat complex sql query which has horrible performance, 'certainly' due to
I have the following which works in SQL Query Analyzer . select oh.* from
Here is the simple sql query that will return all tables that have the
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
I have the following SQL query I was given, but I am not sure
Let's say I have the following SQL query SELECT id, name, title, description, time
I have a SqlDependency set up using the following query: string sql = "SELECT
In sql server 2008, I have the following query: select c.title as categorytitle, s.title
I have the following sql which returns Projects and related Contractors as well as

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.