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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:51:38+00:00 2026-05-23T18:51:38+00:00

I have the following SQL statement: SELECT r.Result AS Resuly, COUNT(f.fieldID) AS [Count] FROM

  • 0

I have the following SQL statement:

    SELECT r.Result AS Resuly, 
           COUNT(f.fieldID) AS [Count] 
      FROM dbo.Fields f
RIGHT JOIN dbo.Results r ON f.Results = r.ID
     WHERE f.RegionID = @RegionID
  GROUP BY r.Result

What I Would like to statement to do is return all the different results (weither they have a count in the Field DB or not). Currently the Query only returns the values that have a count.

ie in the reuslts DB I have

ID 1, 2 and 3
Result x, y, z

only x and z have field that require this result in the field DB so I only get back

Result x, z
count 1, 2

what I want is

Result x,y,z
Count 1,(null or 0), 2
  • 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-23T18:51:39+00:00Added an answer on May 23, 2026 at 6:51 pm

    That’s because your where clause is filtering out results with no fields.
    The where clause is applied after the join is made; criteria in the ON clause is applied before the JOIN is made.

    The [conceptual] process for executing a SQL select query is:

    • Compute the cartesian product of all tables in the from clause.
    • Apply the join criteria
    • Apply the where criteria, if such exists.
    • Partition the results set into groups, based on the criteria in the group by clause, if it exists.
    • Compute the values of any summary (aggregate) functions specified, collapsing each group to a single row, whose columns consist solely of aggregate functions, constant values or grouping columns.
    • Apply the criteria specified by the having clause, if such exists.
    • Order the results according to the order by clause, if such exists.
    • Compute the values of the aggregate functions specified in the compute/compute by clauses, if such exists.

    So…you need to do this to get what you want:

    select Result      = result.Result ,
           ResultCount = count(field.fieldID)
    from      dbo.Results result
    left join dbo.Fields field   on field.Results  = result.ID
                                and field.RegionID = @RegionID
    group by result.Results
    

    The above will give you one row for every result, with a count of matching fields in the specified region (what I believe is what you’re asking for). The count will be zero for any result with no matching fields in the specified region.

    • 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 statement: select val, count(*) as tally from sometable group
I have the following SQL statement SELECT [Motor] ,[Time] FROM [logger].[dbo].[motor] WHERE day([Time]) =
I have the following SQL-statement: SELECT DISTINCT name FROM log WHERE NOT name =
I have the following SQL query: select expr1, operator, expr2, count(*) as c from
I have a following SQL QUERY: SELECT articles.name, articles.price, users.zipcode FROM articles INNER JOIN
I have the following SQL Statement. I need to select the latest record for
I have the following query SELECT s.name, s.surname, s.id_nr, s.student_nr, s.createdate, s.enddate, (SELECT count(*)
I have created a procedure in dynamic SQL which has a select statement and
I have the following sql query for transforming data but is it possible to
I have the following Transact-Sql that I am trying to convert to LINQ ...

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.