I am trying to do the following in a single select. We have a SAAS that has Company records and User records. A User
is a member of a Company
(Company.CompanyId = User.CompanyId
). In the User record we track bandwidth usage for that user (for the month).
I need to do a select that gives me all companies that have used over 1GB of bandwidth per user. I can get this company by company using:
select count(*) as numUsers,
sum(BandwidthThisMonth) as totalBandwidth
from [User]
where [User].CompanyId = @CompanyId
The problem with that approach is I have to first get the CompanyId of every company, and then one by one pull this data. And I only need to find the cases where the usage is over 1GB/user, which is rare.
Is there a way to do something like this:
select
CompanyId,
count(*) as numUsers,
sum(BandwidthThisMonth) as totalBandwidth
from Company, [User]
where totalBandwidth - (numUsers*1000000000))/1000000000 > 0
Where the count()
& sum()
are against just the User records where User.CompanyId = Company.CompanyId
?