SQL GROUP BY WITH COUNT AND HAVING

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
LVL 1
tim_carterAsked:
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.

rjohnsonjrCommented:
What about this?

select 
CustomerType,

COUNT(CustomerType) as CTCount

FROM
Customers t1

WHERE
t1.BusinessCenterId=3
AND t1.Active=1

Group  BY
CustomerType, 

Open in new window

0
tim_carterAuthor Commented:
That will not group by office or virtual
0
Shaun KlineLead Software EngineerCommented:
Something like this?
SELECT CustomerType, COUNT(*)
FROM 
   (SELECT T1.CustomerID, CASE WHEN T2.CustomerID IS NULL THEN 'Virtual' ELSE 'Office' END CustomerType
   FROM Customers T1
       LEFT OUTER JOIN OfficeSpace T2 ON T1.CustomerID = T2.CustomerID
       WHERE t1.BusinessCenterId=3 AND t1.Active=1) Customer2Office
GROUP BY CustomerType

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Russell FoxDatabase DeveloperCommented:
This creates each as a sub-query:
SELECT
	(SELECT COUNT(DISTINCT t1.CustomerId)
	FROM OfficeSpace t1
		JOIN Customers t2
			ON t1.CustomerId = t2.CustomerId
	WHERE t2.BusinessCenterId=3
		AND t2.Active=1
	) AS PhysicalOffices,
	--	-------------------------------
	(SELECT COUNT(DISTINCT CustomerId)
	FROM Customers t1
		LEFT JOIN OfficeSpace t2
			ON t1.CustomerId = t2.CustomerId
	WHERE t1.BusinessCenterId=3
		AND t1.Active=1
		AND t2.CustomerId IS NULL -- no matches
	) AS PhysicalCustomers

Open in new window

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
tim_carterAuthor Commented:
This query is counting the rows in office space. A customer can be in that table more than once but should only be counted once
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>CASE WHEN (SELECT COUNT(*) FROM OfficeSpace WHERE CustomerId=t1.CustomerId) > 0 THEN 'Office' ELSE 'Virtual' END as CustomerType
The above line makes no sense.  GIve us a data mockup of what your data looks like, specifically how to tell the difference between Office and Virtual.
0
tim_carterAuthor Commented:
SELECT
      (SELECT COUNT(DISTINCT t1.CustomerId)
      FROM OfficeSpace t1
            JOIN Customers t2
                  ON t1.CustomerId = t2.CustomerId
      WHERE t2.BusinessCenterId=3
            AND t2.Active=1
      ) AS PhysicalOffices,
      --      -------------------------------
      (SELECT COUNT(DISTINCT CustomerId)
      FROM Customers t1
            LEFT JOIN OfficeSpace t2
                  ON t1.CustomerId = t2.CustomerId
      WHERE t1.BusinessCenterId=3
            AND t1.Active=1
            AND t2.CustomerId IS NULL -- no matches
      ) AS PhysicalCustomers

just gives me ambigious column name customerid
0
tim_carterAuthor Commented:
SELECT
      (SELECT COUNT(DISTINCT t1.CustomerId)
      FROM OfficeSpace t1
            JOIN Customers t2
                  ON t1.CustomerId = t2.CustomerId
      WHERE t2.BusinessCenterId=3
            AND t2.Active=1
      ) AS PhysicalOffices,
      --      -------------------------------
      (SELECT COUNT(DISTINCT CustomerId)
      FROM Customers t1
            LEFT JOIN OfficeSpace t2
                  ON t1.CustomerId = t2.CustomerId
      WHERE t1.BusinessCenterId=3
            AND t1.Active=1
            AND t2.CustomerId IS NULL -- no matches
      ) AS PhysicalCustomers


found it. should be like


SELECT
      (SELECT COUNT(DISTINCT t1.CustomerId)
      FROM OfficeSpace t1
            JOIN Customers t2
                  ON t1.CustomerId = t2.CustomerId
      WHERE t2.BusinessCenterId=3
            AND t2.Active=1
      ) AS PhysicalOffices,
      --      -------------------------------
      (SELECT COUNT(DISTINCT t1.CustomerId)
      FROM Customers t1
            LEFT JOIN OfficeSpace t2
                  ON t1.CustomerId = t2.CustomerId
      WHERE t1.BusinessCenterId=3
            AND t1.Active=1
            AND t2.CustomerId IS NULL -- no matches
      ) AS PhysicalCustomers
0
tim_carterAuthor Commented:
Jim Horn > what do you mean it makes no sense. it is clear as day. if the customer exist in the officespace table it is a office client, else it is a virtual
0
tim_carterAuthor Commented:
Thanks so much. works perfectly. So simple. haha, Sometimes it is so easy to overthink sql statements
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
If experts have to ask then no it's not clear.    That's why mockups have a lot of value in asking questions.
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.