SQL Server 2008
SQL查询显示IP地址(如果存在)
SELECT [IP_ADDR1_TEXT], COUNT(distinct [IP_ADDR1_TEXT]) as Instances
FROM sem_computer, [dbo].[V_SEM_COMPUTER]
WHERE sem_computer.COMPUTER_ID = [dbo].[V_SEM_COMPUTER].COMPUTER_ID
AND [IP_ADDR1_TEXT] = '10.10.10.10'
GROUP BY [IP_ADDR1_TEXT]
输出
IP_ADDR1_TEXT Instances
10.10.10.10 1
如果IP地址计数为0,则输出为空。
如何修改查询,以便实例输出0,在不存在的IP地址旁边,即
SELECT [IP_ADDR1_TEXT], COUNT(distinct [IP_ADDR1_TEXT]) as Instances
FROM sem_computer, [dbo].[V_SEM_COMPUTER]
WHERE sem_computer.COMPUTER_ID = [dbo].[V_SEM_COMPUTER].COMPUTER_ID
AND [IP_ADDR1_TEXT] = '10.10.10.11'
GROUP BY [IP_ADDR1_TEXT]
应输出
IP_ADDR1_TEXT Instances
10.10.10.11 0
编辑
VIEW的屏幕截图
IP_ADDR1_TEXT是ALIAS to
色谱柱 CAST(IP_ADDR1< 0 THEN 0xFFFFFFFF + IP_ADDR1 ELSE IP_ADDR1 END / 256/256/256& 0xFF AS VARCHAR)+'。' + CAST(IP_ADDR1< 0 THEN 0xFFFFFFFF + IP_ADDR1 ELSE IP_ADDR1 END / 256/256& 0xFF AS VARCHAR)+'。' + CAST(IP_ADDR1< 0 THEN 0xFFFFFFFF + IP_ADDR1 ELSE IP_ADDR1 END / 256& 0xFF AS VARCHAR)+'。' + CAST(IP_ADDR1< 0 THEN 0xFFFFFFFF + IP_ADDR1 ELSE IP_ADDR1 END& 0xFF AS VARCHAR)
答案 0 :(得分:1)
SELECT [IP_ADDR1_TEXT], COALESCE(COUNT(distinct [IP_ADDR1_TEXT]),0) as Instances
FROM sem_computer LEFT JOIN [dbo].[V_SEM_COMPUTER]
ON sem_computer.COMPUTER_ID = [dbo].[V_SEM_COMPUTER].COMPUTER_ID
AND [IP_ADDR1_TEXT] = '10.10.10.11'
GROUP BY [IP_ADDR1_TEXT]
答案 1 :(得分:0)
也许试试这个:
SELECT [IP_ADDR1_TEXT], COUNT(distinct [IP_ADDR1_TEXT]) as Instances
FROM sem_computer
Left JOin [dbo].[V_SEM_COMPUTER] On sem_computer.COMPUTER_ID = [dbo].[V_SEM_COMPUTER].COMPUTER_ID
WHERE [IP_ADDR1_TEXT] = '10.10.10.10'
GROUP BY [IP_ADDR1_TEXT]