2017-02-09 13 views
1

このレポートにDRMLの2つの数字を組み合わせる方法を理解できないようです。現在、私は避難所とLSCCを組み合わせる必要があるときに私に5行を与えています。私は間違って何をしていますか?Group By 'Then'ステートメント

SELECT SUM(service.ServiceTotal) as servTotal, 
CASE 
WHEN ProgramName = 'Crossing Operations' THEN 'Crossing' 
WHEN ProgramName = 'Shelter' or ProgramName = 'LSCC' THEN 'DRML' 
WHEN ProgramName = 'Farm Operations' THEN 'Harvest Farm' 
WHEN ProgramName = 'Champa Operations' THEN 'Champa' 
ELSE ProgramName 
END 
as ProgName, 
ProgramName, CONVERT(date,service.BeginDate) as BeginDate 
FROM dbo.Service 
JOIN Enrollment on service.EnrollmentID = Enrollment.EnrollmentID 
JOIN Program on Enrollment.ProgramID = program.ProgramID 
WHERE dbo.service.ServiceTypeID IN (264,429,430,266,431,432,265,273,433,434,493,494,495,500,501,502,496,498,497,499, 270,436,462,435,272,438,463,437,271,460,441,461,442,267,446,448,447,445,443,444,459,269,457,458,456,454,455,268,452,453,451,449,450,514, 
275,276,277) and service.DeletedDate > GETDATE() and DATEDIFF(day,Service.BeginDate, GETDATE()) = 1 --and service.ProvidedToEntityID = 161737 
and Enrollment.programID IN (84,87,118,126,119,150) 
GROUP BY ProgramName, CONVERT(date,service.BeginDate) 
ORDER BY CONVERT(date, service.BeginDate), ProgramName 
+0

あなたのグループで同じcase文を置くことで...またはより優れた入力データを変換し、そのCTE以上でグループを使用するCTEを使用します。 –

+0

なぜあなたは 'ProgramName'をそのままで、その値から' CASE'列を選択していますか? –

答えて

1

あなたgroup byselectリストのうちProgramNameを取ります。 group bycase式を追加してください。 のProgramNameからProgNameに変更してください。

SELECT 
    servTotal = SUM(service.ServiceTotal) 
    , ProgName = CASE 
     WHEN ProgramName = 'Crossing Operations' THEN 'Crossing' 
     WHEN ProgramName = 'Shelter' or ProgramName = 'LSCC' THEN 'DRML' 
     WHEN ProgramName = 'Farm Operations' THEN 'Harvest Farm' 
     WHEN ProgramName = 'Champa Operations' THEN 'Champa' 
     ELSE ProgramName 
     END 
--, ProgramName 
    , BeginDate = CONVERT(date,service.BeginDate) 
FROM dbo.Service 
JOIN Enrollment on service.EnrollmentID = Enrollment.EnrollmentID 
JOIN Program on Enrollment.ProgramID = program.ProgramID 
WHERE dbo.service.ServiceTypeID IN (264,429,430,266,431,432,265,273,433,434,493,494,495,500,501,502,496,498,497,499, 270,436,462,435,272,438,463,437,271,460,441,461,442,267,446,448,447,445,443,444,459,269,457,458,456,454,455,268,452,453,451,449,450,514, 
275,276,277) and service.DeletedDate > GETDATE() and DATEDIFF(day,Service.BeginDate, GETDATE()) = 1 --and service.ProvidedToEntityID = 161737 
and Enrollment.programID IN (84,87,118,126,119,150) 
GROUP BY 
    --ProgramName 
    CASE 
    WHEN ProgramName = 'Crossing Operations' THEN 'Crossing' 
    WHEN ProgramName = 'Shelter' or ProgramName = 'LSCC' THEN 'DRML' 
    WHEN ProgramName = 'Farm Operations' THEN 'Harvest Farm' 
    WHEN ProgramName = 'Champa Operations' THEN 'Champa' 
    ELSE ProgramName 
    END 
    , CONVERT(date,service.BeginDate) 
ORDER BY CONVERT(date, service.BeginDate), ProgName 
+0

私はこれを試みましたが、列 "Program.ProgramName"は、集計関数またはGROUP BY句に含まれていないため、ORDER BY句では無効であると言います。 – Ashley

+0

@Ashleyあなたの 'order by'で' ProgramName'を 'ProgName'に変更してください – SqlZim

+0

ありがとう!!!!!!!!!!!!! – Ashley

1

あなたはProgramNameするのではなく、あなたのCASE式をGROUP BYする必要があります。

SELECT SUM(s.ServiceTotal) servTotal, 
     CASE 
      WHEN ProgramName = 'Crossing Operations' THEN 'Crossing' 
      WHEN ProgramName IN ('Shelter','LSCC') THEN 'DRML' 
      WHEN ProgramName = 'Farm Operations' THEN 'Harvest Farm' 
      WHEN ProgramName = 'Champa Operations' THEN 'Champa' 
      ELSE ProgramName 
     END ProgName, 
     CONVERT(date,s.BeginDate) BeginDate 
FROM dbo.Service s 
INNER JOIN Enrollment e 
    ON s.EnrollmentID = e.EnrollmentID 
INNER JOIN Program p 
    ON e.ProgramID = p.ProgramID 
WHERE s.ServiceTypeID IN (264,429,430,266,431,432,265,273,433,434,493,494,495,500, 
501,502,496,498,497,499, 270,436,462,435,272,438,463,437,271,460,441,461,442,267, 
446,448,447,445,443,444,459,269,457,458,456,454,455,268,452,453,451,449,450,514, 
275,276,277) 
AND s.DeletedDate > GETDATE() AND DATEDIFF(DAY,s.BeginDate, GETDATE()) = 1 --and service.ProvidedToEntityID = 161737 
AND Enrollment.programID IN (84,87,118,126,119,150) 
GROUP BY CASE 
      WHEN ProgramName = 'Crossing Operations' THEN 'Crossing' 
      WHEN ProgramName IN ('Shelter','LSCC') THEN 'DRML' 
      WHEN ProgramName = 'Farm Operations' THEN 'Harvest Farm' 
      WHEN ProgramName = 'Champa Operations' THEN 'Champa' 
      ELSE ProgramName 
     END, 
     CONVERT(date,service.BeginDate) 
ORDER BY CONVERT(date, service.BeginDate), 
     CASE 
      WHEN ProgramName = 'Crossing Operations' THEN 'Crossing' 
      WHEN ProgramName IN ('Shelter','LSCC') THEN 'DRML' 
      WHEN ProgramName = 'Farm Operations' THEN 'Harvest Farm' 
      WHEN ProgramName = 'Champa Operations' THEN 'Champa' 
      ELSE ProgramName 
     END; 
0

あなたがいないあなたが指定しているCASE値によって、直接ProgramName列のGROUPING BY全て異なる値です。

まず、サブクエリまたはCTEのすべての値を選択し、その結果をSUMにします。

SELECT SUM(service.ServiceTotal) as servTotal, ProgName, BeginDate 
FROM(
    SELECT service.ServiceTotal, 
    CASE 
    WHEN ProgramName = 'Crossing Operations' THEN 'Crossing' 
    WHEN ProgramName = 'Shelter' or ProgramName = 'LSCC' THEN 'DRML' 
    WHEN ProgramName = 'Farm Operations' THEN 'Harvest Farm' 
    WHEN ProgramName = 'Champa Operations' THEN 'Champa' 
    ELSE ProgramName 
    END 
    as ProgName, 
    CONVERT(date,service.BeginDate) as BeginDate 
    FROM dbo.Service 
    JOIN Enrollment on service.EnrollmentID = Enrollment.EnrollmentID 
    JOIN Program on Enrollment.ProgramID = program.ProgramID 
    WHERE dbo.service.ServiceTypeID IN (264,429,430,266,431,432,265,273,433,434,493,494,495,500,501,502,496,498,497,499, 270,436,462,435,272,438,463,437,271,460,441,461,442,267,446,448,447,445,443,444,459,269,457,458,456,454,455,268,452,453,451,449,450,514, 
    275,276,277) and service.DeletedDate > GETDATE() and DATEDIFF(day,Service.BeginDate, GETDATE()) = 1 --and service.ProvidedToEntityID = 161737 
    and Enrollment.programID IN (84,87,118,126,119,150)) AS t 
GROUP BY ProgName, BeginDate 
ORDER BY BeginDate, ProgName 

または

WITH cte(ServiceTotal, ProgName, BeginDate) AS 
(
    SELECT service.ServiceTotal, 
    CASE 
    WHEN ProgramName = 'Crossing Operations' THEN 'Crossing' 
    WHEN ProgramName = 'Shelter' or ProgramName = 'LSCC' THEN 'DRML' 
    WHEN ProgramName = 'Farm Operations' THEN 'Harvest Farm' 
    WHEN ProgramName = 'Champa Operations' THEN 'Champa' 
    ELSE ProgramName 
    END 
    as ProgName, 
    CONVERT(date,service.BeginDate) as BeginDate 
    FROM dbo.Service 
    JOIN Enrollment on service.EnrollmentID = Enrollment.EnrollmentID 
    JOIN Program on Enrollment.ProgramID = program.ProgramID 
    WHERE dbo.service.ServiceTypeID IN (264,429,430,266,431,432,265,273,433,434,493,494,495,500,501,502,496,498,497,499, 270,436,462,435,272,438,463,437,271,460,441,461,442,267,446,448,447,445,443,444,459,269,457,458,456,454,455,268,452,453,451,449,450,514, 
    275,276,277) and service.DeletedDate > GETDATE() and DATEDIFF(day,Service.BeginDate, GETDATE()) = 1 --and service.ProvidedToEntityID = 161737 
    and Enrollment.programID IN (84,87,118,126,119,150) 
) 

SELECT SUM(service.ServiceTotal) as servTotal, ProgName, BeginDate 
FROM cte 
GROUP BY ProgName, BeginDate 
ORDER BY BeginDate, ProgName