SQL Case Statement

I have a column called group number. We have several group numbers. Too many to list in a cast statement.

I have one column where I want to sum all group numbers in my Business Intelligence Software.

I used the following logic when I wanted sum just a few group numbers. I create what software calls a calculated field, and then I formatted it for SUM. This one works:

case
when NATIONAL_ACCOUNTS.group_nbr in ('03','04','04FT','05','05FT','05FT2','08','08FT','12','07')
then 1
Else 0
End)


Now I want to do the same thing for any group number (too many to list). I used the same logic, but changed it a little. It does not work. When I format the Calculated field to SUM, it shows 0:

case
when NATIONAL_ACCOUNTS.group_nbr in '*'
then 1
Else 0
End)

W
LVL 1
isamesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

ste5anSenior DeveloperCommented:
First of all: It's the CASE expression.

A case statement would be control flow construct like IF-ELSE, which does not exist in T-SQL.

Using placeholders requires the LIKE operator, but then the placeholder for all is %:

 NATIONAL_ACCOUNTS.group_nbr LIKE '%'

Open in new window


But I don't see the sense in this kind of test. Maybe you're looking for

SUM(
  CASE WHEN NATIONAL_ACCOUNTS.group_nbr IS NULL THEN 0 ELSE 1 END
)

Open in new window

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
PortletPaulEE Topic AdvisorCommented:
FYI:
or you could just count instead

COUNT(NATIONAL_ACCOUNTS.group_nbr)

COUNT() does not increment for a NULL value
isamesAuthor Commented:
Thanks Paul.

I actually tried the Count function before posting this, and it allowed me to see a total at the grouping level, but not an over-all total.
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.