troubleshooting Question

SQL GROUP BY WITH COUNT AND HAVING

Avatar of tim_carter
tim_carter asked on
Microsoft SQL ServerMySQL ServerDatabases
11 Comments1 Solution356 ViewsLast Modified:
Hi Guys, I am trying to make a Query that shows me a count of customers that has a office but I cant really get it to work. I have attached what I got. Hope someone can help me. All I keep getting is t1.Customerid is invalid in the select list because it is not in a group by. But I dont want to group by customerid I want a total of custom ids that either has a office or is a virtual. Does that make sense?

select
COUNT(t1.CustomerId),
CASE WHEN (SELECT COUNT(*) FROM OfficeSpace WHERE CustomerId=t1.CustomerId) > 0 THEN 'Office' ELSE 'Virtual' END as CustomerType
FROM
Customers t1

WHERE
t1.BusinessCenterId=3
AND t1.Active=1
GROUP BY
CustomerType
ASKER CERTIFIED SOLUTION
Russell Fox
Data Engineer
Join our community to see this answer!
Unlock 1 Answer and 11 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros