0

I want to select state or the workstate column. Preference is to first use the state value. If its null or empty then i want to use the workstate value.

I have the following sql:

SELECT        
COUNT(id) AS participants, 

COALESCE(NULLIF(state,''), workState, (select state from dbo.training where id =(SELECT top(1) trainingId
    FROM dbo.Xref_Participant_Training
    WHERE (participantId = dbo.Participant.id) )) ) as state

FROM dbo.Participant
WHERE EXISTS
    (SELECT 1
        FROM dbo.Xref_Participant_Training
        WHERE (participantId = dbo.Participant.id) AND (dbo.Participant.country = 'United States'))
GROUP by COALESCE(NULLIF(state,''), workState, (select state from dbo.training where id =(SELECT top(1) trainingId
        FROM dbo.Xref_Participant_Training
        WHERE (participantId = dbo.Participant.id) )) )

I get the following error:

Msg 144, Level 15, State 1, Line 15
Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause.
4
  • 1
    include workState in the groupings. Commented Aug 2, 2017 at 1:39
  • Specify the same COALESCE expression in the GROUP BY. Commented Aug 2, 2017 at 1:43
  • You may want to change the final part of a coalesce to an outer apply instead of a subquery so that the result can be included in the group by. Commented Aug 2, 2017 at 2:22
  • Take a look at CTEs for Sql Server. You can treat each of your sub-queries as tables, join across the pieces and produce a cleaner statement at the end. Commented Aug 2, 2017 at 4:04

2 Answers 2

2

THIS IS THE ANSWER TO THE ORIGINAL QUESTION.

The issue is that you need to put the entire expression in the group by:

SELECT COUNT(id) AS participants, 
       COALESCE(NULLIF(p.state,''), p.workState) as state
FROM dbo.Participant p
WHERE EXISTS (SELECT 1
              FROM dbo.Xref_Participant_Training pt
              WHERE pt.participantId = p.id
             ) AND
      p.country = 'United States'
GROUP BY COALESCE(NULLIF(p.state, ''), p.workState);

The problem with your query is that GROUP BY state really means GROUP BY p.state. However, you want to aggregate by the column defined in the SELECT.

In addition:

  • Table aliases make the query easier to write and to read.
  • You don't need a column name for an EXISTS subquery.
Sign up to request clarification or add additional context in comments.

2 Comments

ok so what would happen if i had 3 options. See my updated query
@codeNinja . . . Changing the question after it has been answered is rude. It invalidates answers and that can draw downvotes. If you have another question, you should ask another question.
0

change the GROUP BY to include the entire expression

SELECT        
       COUNT(id) AS participants, 
       COALESCE(NULLIF(state,''), workState) as state
FROM   dbo.Participant
WHERE EXISTS
    (SELECT participantId
        FROM dbo.Xref_Participant_Training
        WHERE (participantId = dbo.Participant.id) AND (dbo.Participant.country = 'United States'))
GROUP BY COALESCE(NULLIF(state,''), workState)

for your updated query, when the query gets complex, it is easier to use derived table or cte

example syntax for derived table

select col1, col2, count(*)
from
(
    select col1, col2 = coalesce(col2, col3), col4, col5
    from   sometable
) d
group by col1, col2

1 Comment

yup that works. I want to extend it a bit further to the final state. See my updated query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.