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

  • Home
  • SEARCH
  • 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 155733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:12:28+00:00 2026-05-11T10:12:28+00:00

I have this database structure: TBL_A | TBL_B | TBL_C | TBL_D | TBL_E

  • 0

I have this database structure:

TBL_A  |  TBL_B  |  TBL_C  |  TBL_D  | TBL_E -------+---------+---------+---------+---------- id     | id_tbla | id_tbla | id_tbla | id name   | id_user | id_user | id_user | name_tbla ...    | is_bool |         | weight  | id_user 

Here is what I’m trying to achieve :

SELECT      a.id,      a.name,      b.is_bool,     count(c.id_user) AS nb_views,      sum(d.weight) AS total_weight,     count(distinct e.id_user) AS distinct_users, FROM TBL_A AS a  LEFT JOIN (TBL_B AS b) on (b.id_tbla = a.id) LEFT JOIN (TBL_C AS c) on (c.id_tbla = a.id) LEFT JOIN (TBL_D AS d) on (d.id_tbla = a.id) LEFT JOIN (TBL_E AS e) on (e.name_tbla = a.name) where a.id = 1 and e.id_user = 1 

The query is performed but the results (nb_views, total_weight, distinct_users) are wrong. Any idea why?

  • 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. 2026-05-11T10:12:28+00:00Added an answer on May 11, 2026 at 10:12 am

    You’re trying to compute too many aggregates in one query.

    Enita non sunt multiplicanda praeter necessitatem

    (Latin, ‘entities are not to be multiplied beyond necessity’)

    Your tables B, C, D, and E are produced Cartesian Products against each other. Suppose the given row in A matches:

    • 3 rows in B
    • 6 rows in C
    • 4 rows in D
    • 1 row in E

    The total number of rows in the result is 3 * 6 * 4 * 1 = 72 rows. So your count(c.id_user) is 12 times what it should be, your sum(d.weight) is 18 times what it should be, etc.

    The simplest remedy is to compute each of these aggregates in a separate query:

    SELECT a.id, a.name, COALESCE(b.is_bool, FALSE) AS is_bool FROM TBL_A AS a LEFT JOIN TBL_B AS b ON (b.id_tbla = a.id) WHERE a.id = 1;  SELECT a.id, COUNT(c.id_user) AS nb_views FROM TBL_A AS a LEFT JOIN TBL_C AS c ON (c.id_tbla = a.id) WHERE a.id = 1;  SELECT a.id, SUM(d.weight) AS total_weight, FROM TBL_A AS a LEFT JOIN TBL_D AS d ON (d.id_tbla = a.id) WHERE a.id = 1;  SELECT a.id, COUNT(DISTINCT e.id_user) AS distinct_users, FROM TBL_A AS a LEFT JOIN TBL_E AS e    ON (e.name_tbla = a.name AND e.id_user = 1) WHERE a.id = 1; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 201k
  • Answers 201k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Version number is better because no matter what format of… May 12, 2026 at 8:05 pm
  • Editorial Team
    Editorial Team added an answer Take a look at this (it's your question exactly) and… May 12, 2026 at 8:05 pm
  • Editorial Team
    Editorial Team added an answer I've searched for something similar but this is what I've… May 12, 2026 at 8:05 pm

Related Questions

The problem I have is that I have an old database where there typically
I'm developing an Excel 2007 add-in using Visual Studio Tools for Office (2008). I
I have an SQL Server database, with over 60 tables in it. This database
I have this query: select distinct id,name from table1 For a given ID, the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.