[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

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
0
tim_carter
Asked:
tim_carter
1 Solution
 
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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

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

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now