I have two database tables, project
and story
. The one project having different types of story. I wanted to display all project with each type story count.
This is my SQL:
SELECT
p.id,
p.proj_id,
p.proj_title,
p.proj_status,
COUNT(CASE WHEN s.story_type = 'TY_FEATURES' THEN 1 END) AS FEATURES,
COUNT(CASE WHEN s.story_type = 'TY_ENHANCEMENTS' THEN 1 END) AS ENHANCEMENTS,
COUNT(CASE WHEN s.story_type = 'TY_DEPENDENCIES' THEN 1 END) AS DEPENDENCIES,
COUNT(CASE WHEN s.story_type = 'TY_ADHOC' THEN 1 END) AS ADHOC,
COUNT(CASE WHEN s.story_type = 'TY_DEMO' THEN 1 END) AS DEMO,
COUNT(CASE WHEN s.story_type = 'TY_IMPROVEMENTS' THEN 1 END) AS IMPROVEMENTS
FROM
project p LEFT JOIN story s
ON
p.proj_id = s.proj_id
GROUP BY
p.id,
p.proj_id,
p.proj_status,
p.proj_title
The SQL Query working fine, but I wanted to display all column from project
table. If the column added in SELECT
like p.newExistingInTable
. I need to add in GROUP BY
too, how can I make flexible? or any alternative optimized SQL query?
Error showing if new column added only in SELECT
:
Column 'project.newExistingInTable' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.