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

  • Home
  • SEARCH
  • 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 509125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:58:42+00:00 2026-05-13T06:58:42+00:00

I have the following query: SELECT dev.DeviceName, Count(dom.DomainID) AS CountOfDomains FROM tblDevices dev JOIN

  • 0

I have the following query:

SELECT 
    dev.DeviceName, Count(dom.DomainID) AS CountOfDomains 
FROM 
    tblDevices dev 
JOIN 
    tblIPNumbers ip ON dev.DeviceName = ip.ServerName 
JOIN 
    tblDomains dom ON dom.IPNumberID = ip.IPNumberID  
WHERE 
    dom.PointerTo=0 
    AND dev.DeviceType='3' 
    AND (dev.[System]='32' OR dev.[System]='33') 
    AND dom.ClosedDate IS NULL AND dev.Active=1 
GROUP BY 
    dev.DeviceName 
ORDER BY 
    Count(dom.DomainID)

The tables look like:

tblDomains
==========
DomainID        int
IPNumberID      int
ClosedDate      datetime
PointerTo       int

tblIPNumbers
============
IPNumberID      int
ServerName      varchar(200)

tblDevices
==========
DeviceID        int
DeviceName      varchar(200)
System          varchar(10)
DeviceType      varchar(10)
Active          bit

Sample Data:

tblDomains:
===========
DomainID: 1234  IPNumberID: 1000    ClosedDate: NULL   PointerTo: 0

tblIPNumbers:
=============
IPNumberID: 1000  ServerName: WIN2008-01

tblDevices:
===========
DeviceID: 1    DeviceName: WIN2008-01     System: 32    Active: 1  DeviceType: 3

The problem is that if there are no rows in tblDomains that match an IPNumberID in tblIPNumbers I get no rows returned. I’d like the query to return a single row of 0 for Count(dom.DomainID) AS CountOfDomains in this case.

I’ve tried various combinations of LEFT and RIGHT joins and it seems like a simple problem but my SQL-fu is low today.

  • 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-13T06:58:42+00:00Added an answer on May 13, 2026 at 6:58 am

    You should use LEFT JOINs instead of JOINs, and also beware that when a LEFT JOIN doesn’t find a matching row in the right table, it’ll return NULLs for any field from that right table.

    That means you issue comes from your WHERE clause that references dom.pointerto in a non-NULL-safe way, which will effectively void the benefits of the LEFT JOIN.

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

Sidebar

Ask A Question

Stats

  • Questions 265k
  • Answers 265k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If I understand you correctly, you want to iterate through… May 13, 2026 at 12:27 pm
  • Editorial Team
    Editorial Team added an answer The 'conflicts' version will use the specified copy for the… May 13, 2026 at 12:27 pm
  • Editorial Team
    Editorial Team added an answer this will help http://people.csail.mit.edu/celiu/pdfs/SIFTflow.pdf May 13, 2026 at 12:27 pm

Related Questions

I am using WMI to detect a number of items about a network adapter's
I have a website built using Asp.net and LinqToSql for Data Access. In a
I have a database that stores products available on the market and products still
I have a table that has 8 million records, with many fields, including lat/long
I am asking this question on behalf of a small group of my users

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.