Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8182427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:51:32+00:00 2026-06-07T00:51:32+00:00

I have some tables roughly like so: Client: id name Employee id name Email

  • 0

I have some tables roughly like so:

Client:
   id
   name

Employee 
   id 
   name

Email
   id
   to : Client [ForeignKey]
   from : Employee [ForeignKey]

EmailStats (Tracks the stats for a particular single email)
   id
   email : Email [OneToOne]
   curse_words : 10

What I want to do: I want to fetch all the employees that have written at least one email to a single client, along with the number of times they’ve cursed in any of their emails to that single client, i.e. for a particular Client return

[
    ('name' : 'Paul', 'total_curses' : 255),
    ('name' : 'Mary', 'total_curses' : 10),
]

What I’ve tried:

My understanding of SQL is quite weak as I’m used to using ORM’s. I’m having trouble understanding how the normal retrieval of Employees links into the counting of the curse words. Here’s what I’ve done (be kind!):

SELECT DISTINCT (
    SELECT SUM(EmailStats.curse_words)
    FROM EmailStats
    INNER JOIN (
      SELECT Email.id
      FROM Email
      INNER JOIN Employee
          ON Email.from = Employee.id
      WHERE Email.to = 5 // Assuming 5 is the client's id
    ) filtered_emails ON EmailStats.email = filtered_emails.id                
) AS 'total_curses', Employee.name
FROM Employee
INNER JOIN Email
   ON  Email.from = Employee.id 
WHERE Email.to = 5 // Assuming 5 is the client's id
ORDER_BY 'total_curses'

This isn’t working – it seems to fetch the correct Employees (those who have sent to the Client) but the curses count seems to be the total for all emails to that Client instead of just those curses from that Employee.

I’ve got a feeling that I’m gravely misunderstanding something here, so if anyone could provide an example of how to succesfully go about this I’d appreciate some pointers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:51:34+00:00Added an answer on June 7, 2026 at 12:51 am

    You want to group the result of joining your tables:

    SELECT   Employee.name, SUM(EmailStats.curse_words) total_curses
    FROM     Email
      JOIN EmailStats ON EmailStats.email = Email.id
      JOIN Employee   ON Employee.id      = Email.from
    WHERE    Email.to = 5
    GROUP BY Employee.id
    ORDER BY total_curses DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some tables: Employee: id, name, id_suc, id dep, id_sec Suc : id_suc,
I have some data in a table that looks roughly like the following: table
I have some tables that benefit from many-to-many tables. For example the team table.
I have some tables for which I have no reason to ever update from
I have a Client/Server application, where the Client and Server have some common tables
I have some tables in Oracle enviroment which I have found could benefit from
I have two already-existing tables which look (in part) roughly like this: CREATE TABLE
I have some tables like this: USERS TABLE: | id | created | active
I have multiple tables where there are roughly 10 common columns, but some tables
I have some fields in some tables that can contain only value from 0

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.