0
試圖將相同行的總和顯示爲輸出。SQL Server:按行逐行輸出
下面是SQL代碼
SELECT
replace(replace(replace(replace(dbo.IDENTITY_MAP.Name,'My Company\',''),'-VLAN2',''),'.VLAN2\',''),'.Instr\','') as Site,
Count (CASE
WHEN dbo.SEM_AGENT.AGENT_VERSION LIKE '11.%'
AND dbo.SEM_COMPUTER.OPERATION_SYSTEM LIKE 'Windows%' THEN 1
END) AS 'Windows-SEP-11',
Count (CASE
WHEN dbo.SEM_AGENT.AGENT_VERSION LIKE '11.%'
AND dbo.SEM_COMPUTER.OPERATION_SYSTEM LIKE 'Mac%' THEN 1
END) AS 'Mac-SEP-11',
Count(CASE
WHEN dbo.SEM_AGENT.AGENT_VERSION LIKE '12.%'
AND dbo.SEM_COMPUTER.OPERATION_SYSTEM LIKE 'Windows%' THEN 1
END) AS 'Windows-SEP-12',
Count(CASE
WHEN dbo.SEM_AGENT.AGENT_VERSION LIKE '12.%'
AND dbo.SEM_COMPUTER.OPERATION_SYSTEM LIKE 'Mac%' THEN 1
END) AS 'Mac-SEP-12'
FROM
dbo.sem_computer
INNER JOIN
[dbo].[V_SEM_COMPUTER] ON [dbo].[V_SEM_COMPUTER].COMPUTER_ID = SEM_COMPUTER.COMPUTER_ID
INNER JOIN
dbo.SEM_AGENT ON SEM_COMPUTER.COMPUTER_ID = dbo.SEM_AGENT.COMPUTER_ID
INNER JOIN
dbo.SEM_CLIENT ON dbo.SEM_CLIENT.COMPUTER_ID = SEM_COMPUTER.COMPUTER_ID
INNER JOIN
dbo.IDENTITY_MAP ON dbo.SEM_CLIENT.GROUP_ID = IDENTITY_MAP.ID
INNER JOIN
dbo.PATTERN ON dbo.PATTERN.PATTERN_IDX = dbo.SEM_AGENT.PATTERN_IDX
WHERE
Name LIKE '%31r%'
GROUP BY
dbo.IDENTITY_MAP.Name
ORDER BY
Site
這是輸出
不過,我想輸出看起來像
而且我更新了SQL代碼通過添加SUM()
圍繞計數和按名稱分組,我得到錯誤
無法對包含聚合或子查詢的表達式執行聚合函數。
代碼:
SELECT
replace(replace(replace(replace(dbo.IDENTITY_MAP.Name,'My Company\',''),'-VLAN2',''),'.VLAN2\',''),'.Instr\','') as Site,
SUM(Count (CASE
WHEN dbo.SEM_AGENT.AGENT_VERSION LIKE '11.%'
AND dbo.SEM_COMPUTER.OPERATION_SYSTEM LIKE 'Windows%' THEN 1
END)) AS 'Windows-SEP-11',
SUM(Count (CASE
WHEN dbo.SEM_AGENT.AGENT_VERSION LIKE '11.%'
AND dbo.SEM_COMPUTER.OPERATION_SYSTEM LIKE 'Mac%' THEN 1
END)) AS 'Mac-SEP-11',
SUM(Count(CASE
WHEN dbo.SEM_AGENT.AGENT_VERSION LIKE '12.%'
AND dbo.SEM_COMPUTER.OPERATION_SYSTEM LIKE 'Windows%' THEN 1
END)) AS 'Windows-SEP-12',
SUM(Count(CASE
WHEN dbo.SEM_AGENT.AGENT_VERSION LIKE '12.%'
AND dbo.SEM_COMPUTER.OPERATION_SYSTEM LIKE 'Mac%' THEN 1
END)) AS 'Mac-SEP-12'
FROM
dbo.sem_computer
INNER JOIN
[dbo].[V_SEM_COMPUTER] ON [dbo].[V_SEM_COMPUTER].COMPUTER_ID = SEM_COMPUTER.COMPUTER_ID
INNER JOIN
dbo.SEM_AGENT ON SEM_COMPUTER.COMPUTER_ID = dbo.SEM_AGENT.COMPUTER_ID
INNER JOIN
dbo.SEM_CLIENT ON dbo.SEM_CLIENT.COMPUTER_ID = SEM_COMPUTER.COMPUTER_ID
INNER JOIN
dbo.IDENTITY_MAP ON dbo.SEM_CLIENT.GROUP_ID = IDENTITY_MAP.ID
INNER JOIN
dbo.PATTERN ON dbo.PATTERN.PATTERN_IDX = dbo.SEM_AGENT.PATTERN_IDX
WHERE
Name LIKE '%31r%'
GROUP BY
Name
謝謝!
感謝您的快速響應,它的工作原理 – Rhonda