SQL Server 2005 Group by on Subselect in Case

Hi Experts,
I have a group by  that has a subquery in a case statement.  The select runs buts does not do a "final" group by.  Thanks!

Result now:
NUMBER       [User Interface-2]          [Doc ID-4]
----------          ------------------ --------------------
898G6A3292                  NULL             43100328
898G6A3292            SAP GUI                     NULL  

Desired result:
NUMBER          [User Interface-2]          [Doc ID-4]
----------              ------------------ --------------------
898G6A3292                SAP GUI           43100328

SELECT * 
FROM (
 
SELECT  INCIDENTS.NUMBER
 
		,CASE INCIDENT_CUSTOM_FIELDS.ID_FIELD WHEN 2 THEN 
		(SELECT ICF.DATA
		FROM INCIDENT_CUSTOM_FIELDS ICF
		WHERE ICF.ID_INCIDENT = INCIDENT_CUSTOM_FIELDS.ID_INCIDENT
		AND ICF.ID_FIELD = INCIDENT_CUSTOM_FIELDS.ID_FIELD)  ELSE '' END AS [User Interface-2]  
 
		,CASE INCIDENT_CUSTOM_FIELDS.ID_FIELD WHEN 4 THEN  
		(SELECT ICF.DATA
		FROM INCIDENT_CUSTOM_FIELDS ICF
		WHERE ICF.ID_INCIDENT = INCIDENT_CUSTOM_FIELDS.ID_INCIDENT
		AND ICF.ID_FIELD = INCIDENT_CUSTOM_FIELDS.ID_FIELD)  ELSE '' END  AS [Doc ID-4]   
FROM    PROBLEM_RELATED_INCIDENTS INNER JOIN
        INCIDENTS ON PROBLEM_RELATED_INCIDENTS.ID_INCIDENT = INCIDENTS.ID INNER JOIN
        INCIDENT_CUSTOM_FIELDS ON INCIDENTS.ID = INCIDENT_CUSTOM_FIELDS.ID_INCIDENT INNER JOIN
        CUSTOM_FIELD_DEFS ON INCIDENT_CUSTOM_FIELDS.ID_FIELD = CUSTOM_FIELD_DEFS.ID
WHERE   (PROBLEM_RELATED_INCIDENTS.ID_PROBLEM = 42)
GROUP BY INCIDENTS.NUMBER,INCIDENT_CUSTOM_FIELDS.INCIDENT_NUMBER,
         INCIDENTS.ID,INCIDENT_CUSTOM_FIELDS.ID_INCIDENT
        ,INCIDENT_CUSTOM_FIELDS.ID_FIELD
 ) temp
 
 GROUP BY temp.NUMBER,temp.[User Interface-2],temp.[Doc ID-4]

Open in new window

LVL 3
AveeightusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

momi_sabagCommented:
select NUMBER         ,max([User Interface-2]    ), max(   [Doc ID-4])

from (
SELECT *
FROM (
 
SELECT  INCIDENTS.NUMBER
 
            ,CASE INCIDENT_CUSTOM_FIELDS.ID_FIELD WHEN 2 THEN
            (SELECT ICF.DATA
            FROM INCIDENT_CUSTOM_FIELDS ICF
            WHERE ICF.ID_INCIDENT = INCIDENT_CUSTOM_FIELDS.ID_INCIDENT
            AND ICF.ID_FIELD = INCIDENT_CUSTOM_FIELDS.ID_FIELD)  ELSE '' END AS [User Interface-2]  
 
            ,CASE INCIDENT_CUSTOM_FIELDS.ID_FIELD WHEN 4 THEN  
            (SELECT ICF.DATA
            FROM INCIDENT_CUSTOM_FIELDS ICF
            WHERE ICF.ID_INCIDENT = INCIDENT_CUSTOM_FIELDS.ID_INCIDENT
            AND ICF.ID_FIELD = INCIDENT_CUSTOM_FIELDS.ID_FIELD)  ELSE '' END  AS [Doc ID-4]  
FROM    PROBLEM_RELATED_INCIDENTS INNER JOIN
        INCIDENTS ON PROBLEM_RELATED_INCIDENTS.ID_INCIDENT = INCIDENTS.ID INNER JOIN
        INCIDENT_CUSTOM_FIELDS ON INCIDENTS.ID = INCIDENT_CUSTOM_FIELDS.ID_INCIDENT INNER JOIN
        CUSTOM_FIELD_DEFS ON INCIDENT_CUSTOM_FIELDS.ID_FIELD = CUSTOM_FIELD_DEFS.ID
WHERE   (PROBLEM_RELATED_INCIDENTS.ID_PROBLEM = 42)
GROUP BY INCIDENTS.NUMBER,INCIDENT_CUSTOM_FIELDS.INCIDENT_NUMBER,
         INCIDENTS.ID,INCIDENT_CUSTOM_FIELDS.ID_INCIDENT
        ,INCIDENT_CUSTOM_FIELDS.ID_FIELD
 ) temp
 
 GROUP BY temp.NUMBER,temp.[User Interface-2],temp.[Doc ID-4]
) t
group by NUMBER          
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AveeightusAuthor Commented:
Thanks!!!   I see my error.  I did not give the GROUP BY a 'handle' on the columns I needed to 'compress'.  Simply create a SELECT shell and Group By on the single returned  value.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.