SQL Query issues - different conditions in the SELECT list

mmwillingham 1 Reputation point
2021-04-09T15:21:01.847+00:00

In SQL Server, I have several tables that I need to join together to create one query that looks like this:
Country EmailCount RecordCount PercentRecords
USA 50000 5000 10.00
Brazil 40000 7500 18.75
Germany 30000 25000 83.33

Tables and applicable columns
MailboxSummary Contains Counts by RecordTypes for user mailbox
Count: Count of emails
MailboxFK: Link to MailboxID in the Mailboxes table
RecordTypeFK: Link to RecordTypeID in RecordTypes table
Mailboxes Contains details about each user, including profile (Country)
MailboxID: MailboxID PK
ProfileFK: Link to ProfileID in Profiles table
Profiles Contains details about each profile (Country)
ProfileID: ProfileID PK
Name: Name of Country
RecordTypes Contains details about each RecordType
RecordTypeID: RecordTypeID PK
ZoneFK: Link to ZoneID in Zones table (not listed)

I can get the results easily with two queries:
-- Count by Country
SELECT pf.Name, sum(mbs.Count) As TotalCount
FROM MailboxSummary mbs
INNER JOIN Mailboxes mb ON mbs.MailboxFK = mb.MailboxID
INNER JOIN Profiles pf ON mb.ProfileFK = pf.ProfileID
INNER JOIN RecordTypes rt ON mbs.RecordTypeFK = rt.RecordTypeID
GROUP BY pf.Name
ORDER BY pf.Name

-- RecordCount By Country
SELECT pf.Name, sum(mbs.Count) As TotalRecordCount
FROM MailboxSummary mbs
INNER JOIN Mailboxes mb ON mbs.MailboxFK = mb.MailboxID
INNER JOIN Profiles pf ON mb.ProfileFK = pf.ProfileID
INNER JOIN RecordTypes rt ON mbs.RecordTypeFK = rt.RecordTypeID
WHERE rt.ZoneFk = 3
GROUP BY pf.Name
ORDER BY pf.Name

I can get the desired results by user ID with one query, but not when grouping by Country.

Any ideas?
Thanks!

Developer technologies Transact-SQL
SQL Server Other
{count} votes

1 answer

Sort by: Most helpful
  1. EchoLiu-MSFT 14,621 Reputation points
    2021-04-12T03:16:45.087+00:00

    Hi @mmwillingham ,

    Welcome to the microsoft TSQL Q&A forum!Your question involves tsql, my colleague helped you add the tsql tag.

    86580-image.png
    If you use user-id and country as the grouping conditions to get different results, I guess it may be that one user id corresponds to multiple countries,the country and user id should not correspond one-to-one.But since you did not provide specific data, these are just guesses.

    If this does not solve your problem,please us your table structure (CREATE TABLE …) and some sample data(INSERT INTO …)along with your expected result. So that we’ll get a right direction and make some test.

    If you have any question, please feel free to let me know.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.