How to SQL Count rows from Join Table?

I have 2 tables left outer joined by ID. One is a header and one is a detail. I want return from the select 2 columns, Column#1 is ID, Column #2 is a dynamic column either displaying the 'name' value from the detail(if only 1 detail row and that row has a name value > '') or 'multi' if there are multiple detail rows(that have a name value > '') or just '' if the name value is null.

-- Header
CREATE TABLE [dbo].[a_header](
      [id] [int] NULL
) ON [PRIMARY]
INSERT INTO a_header VALUES(1)
INSERT INTO a_header VALUES(2)
INSERT INTO a_header VALUES(3)
INSERT INTO a_header VALUES(4)
INSERT INTO a_header VALUES(5)
      

-- Detail
CREATE TABLE [dbo].[a_detail](
      [id] [int] NULL,
      [name] [varchar](50) NULL
) ON [PRIMARY]
INSERT INTO a_detail VALUES(1,'susan')
INSERT INTO a_detail VALUES(2,'john')
INSERT INTO a_detail VALUES(2,'mary')
INSERT INTO a_detail VALUES(2,'mark')
INSERT INTO a_detail VALUES(3,'jim')
INSERT INTO a_detail VALUES(3,'paul')
INSERT INTO a_detail VALUES(3,'kathy')
INSERT INTO a_detail VALUES(4,NULL)

--Select * from ....
--(id),(value)
1,'susan'
2,'multi'
3,'multi'
4,''
5,''
byteboy11Asked:
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.

Patrick MatthewsCommented:
SELECT h.id, CASE COUNT(d.[name]) WHEN 0 THEN NULL WHEN 1 THEN MAX(d.[name] ELSE 'multi' END AS [name]
FROM [dbo].[a_header] h LEFT JOIN
    [dbo].[a_detail] d ON h.id = d.id
GROUP BY h.id
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
byteboy11Author Commented:
thank you
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 2005

From novice to tech pro — start learning today.