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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:00:57+00:00 2026-06-17T15:00:57+00:00

I have to get all count for all orders, which was passed to query,

  • 0

I have to get all count for all orders, which was passed to query, even for null. I have developed such kind of query in SQL Server 2008R2:

SELECT COUNT(UserId), b.OrderId
FROM @OrderList b
LEFT JOIN 
(
    SELECT DISTINCT pat_base.UserId, prov_list.OrderId
    FROM Users pat_base
    LEFT JOIN Assetments hlth_asmt ON hlth_asmt.UserId = pat_base.UserId
        AND hlth_asmt.Date BETWEEN @StartDate AND @EndDate
    LEFT JOIN Logs proc_log ON proc_log.UserId = pat_base.UserId 
        AND proc_log.PLDATE BETWEEN @StartDate AND @EndDate
    LEFT JOIN Encounter med_enc_log ON med_enc_log.UserId = pat_base.UserId
        AND med_enc_log.EncounterDate BETWEEN @StartDate AND @EndDate
    LEFT JOIN @OrderList prov_list ON
        hlth_asmt.OrderId = prov_list.OrderId
        OR proc_log.OrderId = prov_list.OrderId
        OR med_enc_log.OrderId = prov_list.OrderId
    WHERE hlth_asmt.UserId IS NOT NULL
        OR proc_log.UserId IS NOT NULL
    OR med_enc_log.UserId IS NOT NULL
) a ON a.OrderId=b.OrderId
GROUP BY b.OrderId

If I remove joined subquery, order is being cut with many-to-many relation. Is it possible to replace subquery with join?

P.S. Here is output now:

            OrderId
----------- -----------
8           10001
0           10003

Output without subquery:

            OrderId
----------- -----------
8           10001
  • 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-17T15:00:59+00:00Added an answer on June 17, 2026 at 3:00 pm

    you have to change

    LEFT JOIN @OrderList prov_list ON
    

    to

    RIGHT JOIN @OrderList prov_list ON
    

    if you use left join you filter out orders wich are in @OrderList but dont have expected user

    so the query would be like

    SELECT COUNT(UserId), prov_list.OrderId
        FROM Users pat_base
        LEFT JOIN Assetments hlth_asmt ON hlth_asmt.UserId = pat_base.UserId
            AND hlth_asmt.Date BETWEEN @StartDate AND @EndDate
        LEFT JOIN Logs proc_log ON proc_log.UserId = pat_base.UserId 
            AND proc_log.PLDATE BETWEEN @StartDate AND @EndDate
        LEFT JOIN Encounter med_enc_log ON med_enc_log.UserId = pat_base.UserId
            AND med_enc_log.EncounterDate BETWEEN @StartDate AND @EndDate
        RIGHT JOIN @OrderList prov_list ON
            hlth_asmt.OrderId = prov_list.OrderId
            OR proc_log.OrderId = prov_list.OrderId
            OR med_enc_log.OrderId = prov_list.OrderId
        WHERE hlth_asmt.UserId IS NOT NULL
            OR proc_log.UserId IS NOT NULL
        OR med_enc_log.UserId IS NOT NULL
    group by prov_list.OrderId
    

    edit

    yeah, that means that those rows were filtered out in where clause

    change it to

    WHERE hlth_asmt.UserId IS NOT NULL
    OR proc_log.UserId IS NOT NULL
    OR med_enc_log.UserId IS NOT NULL
    OR pat_base IS NULL
    

    but i think what it means, is that you have inconsistent data within db

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

Sidebar

Related Questions

I have to create an SQL Query to get all rows starting with a
I need to query the orders table to get a count of all orders
I have a query to get all the compatible phones in a specific country,
I need get all items these have no categories int? categoryId = null; var
I have a sql table images which has columns id , imagePath , caption``count
I have the following query, which is fine, but will get slower as the
I have the following code which retrieves the count of the following query: <%
I have this issue trying to get all the text nodes in an HTML
I have a written a method to get all the records and return in
I have a requirement that i want to get all the system services running

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.