此SQL Server查询正常工作
SELECT
dbo.sem_computer.COMPUTER_ID, COUNT(dbo.sem_computer.COMPUTER_ID) as Duplicate_Hardware_IDs
FROM
dbo.sem_computer, [dbo].[V_SEM_COMPUTER], dbo.SEM_CLIENT, dbo.SEM_AGENT, dbo.IDENTITY_MAP
WHERE
sem_computer.COMPUTER_ID = [dbo].[V_SEM_COMPUTER].COMPUTER_ID
and sem_computer.COMPUTER_ID = dbo.SEM_CLIENT.COMPUTER_ID
and sem_computer.COMPUTER_ID = dbo.SEM_AGENT.COMPUTER_ID
and dbo.SEM_CLIENT.GROUP_ID = IDENTITY_MAP.ID
and dbo.SEM_AGENT.TIME_STAMP > DATEDIFF(second, '19700101', DATEADD(day, -1, GETDATE())) * CAST(1000 as bigint)
GROUP BY dbo.sem_computer.COMPUTER_ID
HAVING COUNT(dbo.sem_computer.COMPUTER_ID) > 1
ORDER BY Duplicate_Hardware_IDs DESC;
但是我想要SELECT
个附加列(以显示哪些计算机有重复COMPUTER_ID
)
SELECT
dbo.sem_computer.COMPUTER_NAME
, [IP_ADDR1_TEXT]
, dbo.SEM_AGENT.AGENT_VERSION
, dbo.sem_computer.COMPUTER_ID, COUNT(dbo.sem_computer.COMPUTER_ID) as Duplicate_Hardware_IDs
FROM
dbo.sem_computer, [dbo].[V_SEM_COMPUTER], dbo.SEM_CLIENT, dbo.SEM_AGENT, dbo.IDENTITY_MAP
WHERE
sem_computer.COMPUTER_ID = [dbo].[V_SEM_COMPUTER].COMPUTER_ID
and sem_computer.COMPUTER_ID = dbo.SEM_CLIENT.COMPUTER_ID
and sem_computer.COMPUTER_ID = dbo.SEM_AGENT.COMPUTER_ID
and dbo.SEM_CLIENT.GROUP_ID = IDENTITY_MAP.ID
and dbo.SEM_AGENT.TIME_STAMP > DATEDIFF(second, '19700101', DATEADD(day, -1, GETDATE())) * CAST(1000 as bigint)
GROUP BY dbo.sem_computer.COMPUTER_ID
HAVING COUNT(dbo.sem_computer.COMPUTER_ID) > 1
ORDER BY Duplicate_Hardware_IDs DESC;
我收到错误
专栏' dbo.sem_computer.COMPUTER_NAME'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
如何解决?
更新:执行时
SELECT
dbo.sem_computer.COMPUTER_NAME
, [IP_ADDR1_TEXT]
, dbo.SEM_AGENT.AGENT_VERSION
, dbo.sem_computer.COMPUTER_ID, COUNT(dbo.sem_computer.COMPUTER_ID) as Duplicate_Hardware_IDs
FROM
dbo.sem_computer, [dbo].[V_SEM_COMPUTER], dbo.SEM_CLIENT, dbo.SEM_AGENT, dbo.IDENTITY_MAP
WHERE
sem_computer.COMPUTER_ID = [dbo].[V_SEM_COMPUTER].COMPUTER_ID
and sem_computer.COMPUTER_ID = dbo.SEM_CLIENT.COMPUTER_ID
and sem_computer.COMPUTER_ID = dbo.SEM_AGENT.COMPUTER_ID
and dbo.SEM_CLIENT.GROUP_ID = IDENTITY_MAP.ID
and dbo.SEM_AGENT.TIME_STAMP > DATEDIFF(second, '19700101', DATEADD(day, -1, GETDATE())) * CAST(1000 as bigint)
GROUP BY dbo.sem_computer.COMPUTER_NAME,[IP_ADDR1_TEXT], dbo.SEM_AGENT.AGENT_VERSION, dbo.sem_computer.COMPUTER_ID
HAVING COUNT(dbo.sem_computer.COMPUTER_ID) > 1
ORDER BY Duplicate_Hardware_IDs DESC;
导致
COMPUTER_NAME IP_ADDR1_TEXT AGENT_VERSION COMPUTER_ID Duplicate_Hardware_IDs
ABC 10.10.10.10 12.1 56604FEE0AF 3
但我正在寻找
COMPUTER_NAME IP_ADDR1_TEXT AGENT_VERSION COMPUTER_ID Duplicate_Hardware_IDs
ABC 10.10.10.10 12.1 56604FEE0AF 3
123 10.10.10.15 12.2 56604FEE0AF 3
XYZ 10.10.10.25 12.2 56604FEE0AF 3
更新2 :
我添加了FROM
和WHERE
答案 0 :(得分:3)
;WITH CTE AS
(
SELECT dbo.sem_computer.COMPUTER_NAME,
[IP_ADDR1_TEXT],
dbo.SEM_AGENT.AGENT_VERSION,
dbo.sem_computer.COMPUTER_ID,
N = COUNT(*) OVER(PARTITION BY dbo.sem_computer.COMPUTER_ID)
FROM
...
WHERE
...
)
SELECT *
FROM CTE
WHERE N > 1
ORDER BY N DESC
答案 1 :(得分:2)
只需将Select
列表中的列添加到Group By
列表
SELECT
dbo.sem_computer.COMPUTER_NAME
, [IP_ADDR1_TEXT]
, dbo.SEM_AGENT.AGENT_VERSION
, dbo.sem_computer.COMPUTER_ID, COUNT(dbo.sem_computer.COMPUTER_ID) as Duplicate_Hardware_IDs
FROM
...
WHERE
...
GROUP BY
dbo.sem_computer.COMPUTER_ID,
dbo.sem_computer.COMPUTER_NAME,
...
HAVING COUNT(dbo.sem_computer.COMPUTER_ID) > 1
ORDER BY Duplicate_Hardware_IDs DESC;`
答案 2 :(得分:1)
任何在select语句中但不在任何聚合函数(MIN,MAX,SUM,COUNT,AVG)中的列都应该包含在GROUP BY子句中。
SELECT
dbo.sem_computer.COMPUTER_NAME
,[IP_ADDR1_TEXT]
,dbo.SEM_AGENT.AGENT_VERSION
,dbo.sem_computer.COMPUTER_ID
,COUNT(dbo.sem_computer.COMPUTER_ID) as Duplicate_Hardware_IDs
FROM
...
WHERE
...
GROUP BY dbo.sem_computer.COMPUTER_NAME
,[IP_ADDR1_TEXT]
,dbo.SEM_AGENT.AGENT_VERSION
,dbo.sem_computer.COMPUTER_ID
HAVING COUNT(dbo.sem_computer.COMPUTER_ID) > 1
ORDER BY Duplicate_Hardware_IDs DESC;
答案 3 :(得分:0)
您可以像其他人提到的那样在GROUP BY
子句中添加列,或者如果您不希望使用这些列进行分组,请尝试以下
SELECT
dbo.sem_computer.COMPUTER_NAME,
[IP_ADDR1_TEXT],
dbo.SEM_AGENT.AGENT_VERSION,
dbo.sem_computer.COMPUTER_ID, Duplicate_Hardware_IDs
FROM dbo.sem_computer c
INNER JOIN
dbo.SEM_AGENT ON //add the join conditions
INNER JOIN
(SELECT
dbo.sem_computer.COMPUTER_ID, COUNT(dbo.sem_computer.COMPUTER_ID) as Duplicate_Hardware_IDs
FROM
...
WHERE
...
GROUP BY dbo.sem_computer.COMPUTER_ID
HAVING COUNT(dbo.sem_computer.COMPUTER_ID) > 1) temp
ON c.COMPUTER_ID=temp.COMPUTER_ID
ORDER BY Duplicate_Hardware_IDs DESC;
答案 4 :(得分:0)
您必须在group by子句中添加所有非聚合字段:
GROUP BY dbo.sem_computer.COMPUTER_NAME,[IP_ADDR1_TEXT]
,dbo.SEM_AGENT.AGENT_VERSION, dbo.sem_computer.COMPUTER_ID
SELECt dbo.sem_computer.COMPUTER_NAME,IP_ADDR1_TEXT,dbo.SEM_AGENT.AGENT_VERSION,dbo.sem_computer.COMPUTER_ID, COUNT(dbo.sem_computer.COMPUTER_ID) as Duplicate_Hardware_IDs FROM ... WHERE ...GROUP BY dbo.sem_computer.COMPUTER_NAME,IP_ADDR1_TEXT,dbo.SEM_AGENT.AGENT_VERSION, dbo.sem_computer.COMPUTER_ID HAVING COUNT(dbo.sem_computer.COMPUTER_ID) > 1ORDER BY Duplicate_Hardware_IDs DESc
答案 5 :(得分:0)
很难从您的查询中了解到,因为您没有显示您如何将SEM_AGENT与查询相关联。
一个选项是将字段添加到组中 - 只需添加逗号和字段名称 您还可以使用像LAST()这样的聚合函数来返回查询中的字段。 您也可以使用子选择返回该字段。不过,我不确定那个。
答案 6 :(得分:0)
您可以通过加入两个查询来解决此问题。以下内容应该有效。
;WITH Duplicates AS (
SELECT
sem_computer.COMPUTER_ID,
COUNT(sem_computer.COMPUTER_ID) as Duplicate_Hardware_IDs
FROM
sem_computer
WHERE
...
GROUP BY sem_computer.COMPUTER_ID
)
SELECT
sem_computer.COMPUTER_NAME,
sem_computer.[IP_ADDR1_TEXT],
SEM_AGENT.AGENT_VERSION,
sem_computer.COMPUTER_ID
FROM
sem_computer
INNER JOIN
Duplicates
ON sem_computer.COMPUTER_ID = Duplicates.COMPUTER_ID
INNER JOIN
SEM_AGENT
ON SEM_AGENT.COMPUTER_ID = sem_computer.COMPUTER_ID
WHERE
Duplicates.Duplicate_Hardware_IDs > 1
ORDER BY Duplicate_Hardware_IDs DESC;
首先找到重复项,然后加入这些重复项并返回其他信息。您还必须加入SEM_AGENT表。