Skip to content
Advertisement

GROUP BY problem on WHEN in CASE statement

I’m trying to have the indirect and direct hours all show on one row and I know multiple rows are showing up because of the group by statement. However, I’ve tried removing the grouping and get an error that the l.CHG_TYPE is not included in an aggregate when removed.

Advertisement

Answer

You can use conditional aggregation as follows:

  • remove CHG_TYPE from the GROUP BY clause
  • move the CASE expression within the SUM()

Side note: it is not a good practice to use single quotes for column aliases. In standard SQL, single quotes are reserved for string litterals. I would suggest using the relevant quote character for your database (MySQL: backticks – SQL Server: square brackets – Postgres and Oracle: double quotes). Better yet, you can use column aliases that do not contain special characters, so they don’t need to be quoted.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement