2015-01-02 1 views
-1

행을 관련 데이터와 결합 할 수 있습니까? 예를 들어 알 수 있습니다. 나는이 스크립트를 사용하고 있습니다 :SQL Sever 2008 - 행을 관련 데이터와 결합하는 방법

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

하지만이 결과를 얻으려면 결합 좋아 (그룹)이 행과 NumAlets을 요약 다음과 같습니다 :

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

그리고 결과는

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를 그룹화 표시 나머지는 모두 건너 뛰고 :(하지만! 어떻게 그룹화 행을 표시 할 수 있습니다 모든 나머지 열이 있습니까? –

+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 

에서 오는 쿼리는 아래와 같습니다. 당신이 왼쪽 JOIN`하는 대신 모든 나머지`NULL`을 사람들을 그룹화 및 디스플레이`사용하는 경우

select min(AlertName) as AlertName, COUNT(*) as NumAlerts 
from Alerts 
group by dbo.UDF_ParseAlphaChars(AlertName) 
관련 문제