如何修复此 SQL GROUP BY 查询?

发布于 2024-08-13 04:00:31 字数 1398 浏览 5 评论 0原文

我有以下查询:

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)

表格如下所示:

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

示例数据:

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

问题是,如果 tblDomains 中没有与 tblIPNumbers 中的 IPNumberID 匹配的行code> 我没有返回任何行。在这种情况下,我希望查询为 Count(dom.DomainID) AS CountOfDomains 返回一行 0

我尝试过 LEFT 和 RIGHT 连接的各种组合,这似乎是一个简单的问题,但今天我的 SQL-fu 很低。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流云如水 2024-08-20 04:00:31

您应该使用 LEFT JOIN 而不是 JOIN,并且还要注意,当 LEFT JOIN 在右侧找不到匹配的行时表,它将为该右表中的任何字段返回 NULL。

这意味着您的问题来自您的 WHERE 子句,该子句以非 NULL 安全的方式引用 dom.pointerto,这将有效地消除好处LEFT JOIN 的。

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.

爱人如己 2024-08-20 04:00:31

将您的 JOIN 更改为 LEFT JOIN,它们也会包含不匹配的记录。

Change your JOINs to LEFT JOIN and they'll include non-matching records as well.

夏九 2024-08-20 04:00:31

尝试对表 tbldomains 使用外连接


SELECT  
    dev.DeviceName, Count(dom.DomainID) AS CountOfDomains  
FROM  
    tblDevices dev  
JOIN  
   tblIPNumbers ip ON dev.DeviceName = ip.ServerName  
outer 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) 

Try Using outer join for table tbldomains


SELECT  
    dev.DeviceName, Count(dom.DomainID) AS CountOfDomains  
FROM  
    tblDevices dev  
JOIN  
   tblIPNumbers ip ON dev.DeviceName = ip.ServerName  
outer 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) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文