Link to home
Start Free TrialLog in
Avatar of tim_carter
tim_carter

asked on

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
Avatar of Randy Johnson
Randy Johnson
Flag of United States of America image

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

Avatar of tim_carter
tim_carter

ASKER

That will not group by office or virtual
Avatar of Shaun Kline
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

ASKER CERTIFIED SOLUTION
Avatar of Russell Fox
Russell Fox
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
>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.
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
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
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
Thanks so much. works perfectly. So simple. haha, Sometimes it is so easy to overthink sql statements
If experts have to ask then no it's not clear.    That's why mockups have a lot of value in asking questions.