2015-01-02 23 views
-1

是否有可能將行與相關數據組合在一起,下面的示例將清楚說明。我使用這個腳本:SQL Sever 2008 - 如何將行與相關數據組合

SELECT AlertName, COUNT(AlertName) as NumAlerts 
FROM Alerts 
GROUP BY AlertName 
ORDER BY AlertName 

和結果是:

AlertName    NumAlets 
------------------------|--------------- 
... 
Windows Services SQL  9 
... 
Windows Services - Core 7 
Windows Services Core 271 
Windows Services: Core 90 
... 

但我想結合(組),這些行和總結NumAlets得到這樣的結果:

AlertName    NumAlets 
------------------------|--------------- 
... 
Windows Services SQL  9 
Windows Services (Core) 368 
... 

我該怎麼做?

預先感謝您!

回答

0
SELECT rs.AlertName, COUNT(1) Totals 
FROM (
    SELECT CASE 
     WHEN AlertName LIKE 'Windows Services%Core' 
      THEN 'Windows Services (Core)' 
     WHEN AlertName LIKE 'Windows Services%SQL' 
      THEN 'Windows Services (SQL)' 
     ELSE AlertName END AlertName 
    from ALERTS) RS 
Group By rs.AlertName 
4

你需要一個表的各種拼法轉化爲一個:

DECLARE @Translation TABLE (
    AlertName varchar(100), 
    CommonAlertName varchar(100) 
) 

INSERT INTO @Translation (AlertName, CommonAlertName) 
    VALUES ('Windows Services SQL', 'Windows Services SQL'), 
      ('Windows Services - Core', 'Windows Services (Core)'), 
      ('Windows Services Core', 'Windows Services (Core)'), 
      ('Windows Services: Core', 'Windows Services (Core)') 

SELECT T.CommonAlertName, SUM(A.NumAlerts) AS NumAlerts 
FROM Alerts A 
INNER JOIN @Translation T ON A.AlertName = T.AlertName 
GROUP BY T.CommonAlertName 
+0

它只分組的行ANS顯示跳過所有其餘:(但是如果你使用'LEFT JOIN'代替它顯示分組者和'NULL'爲所有的休息!如何使它顯示分組行與其餘所有行? –

+0

這是一個指針,其餘的由您決定以適應您的需求。SO不是免費的代碼寫入服務。 –

1

佐夫的回答是合理的,但邏輯應該是left join所以翻譯不具有轉換表中存在:

with translation as (
     select 'Windows Services SQL' as AlertName, 'Windows Services SQL' as CommonAlertName union ll 
     select 'Windows Services - Core', 'Windows Services (Core)' union all 
     select 'Windows Services Core', 'Windows Services (Core)' union all 
     select 'Windows Services: Core', 'Windows Services (Core)' 
    ) 
SELECT COALESCE(T.CommonAlertName, A.AlertName), SUM(A.NumAlerts) AS NumAlerts 
FROM Alerts A LEFT JOIN 
    Translation T 
    ON A.AlertName = T.AlertName 
GROUP BY COALESCE(T.CommonAlertName, A.AlertName); 
1

你也可以用一個函數來完成這個功能,該函數可以過濾掉所有非字母數字字符,這樣像「Windows服務 - 核心」和「Windows服務(核心)」這樣的字符串都變成「WindowsServicesCore」並且相互匹配。

下面的函數來自http://blog.sqlauthority.com/2007/05/13/sql-server-udf-function-to-parse-alphanumeric-characters-from-string/

CREATE FUNCTION dbo.UDF_ParseAlphaChars 
(
@string VARCHAR(8000) 
) 
RETURNS VARCHAR(8000) 
AS 
BEGIN 
    DECLARE @IncorrectCharLoc SMALLINT 
    SET @IncorrectCharLoc = PATINDEX('%[^0-9A-Za-z]%', @string) 

    WHILE @IncorrectCharLoc > 0 
    BEGIN 
     SET @string = STUFF(@string, @IncorrectCharLoc, 1, '') 
     SET @IncorrectCharLoc = PATINDEX('%[^0-9A-Za-z]%', @string) 
    END 

    SET @string = @string 
    RETURN @string 
END 

和查詢看起來像下面的一個。

select min(AlertName) as AlertName, COUNT(*) as NumAlerts 
from Alerts 
group by dbo.UDF_ParseAlphaChars(AlertName) 
相關問題