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 341681
In Process

The Archive Base Latest Questions

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

I have a table (dump) with transactions, and I want to list the total

  • 0

I have a table (“dump”) with transactions, and I want to list the total amount, grouped by category, per month, like: Month | Category | Category ID | SUM. The tables involved looks like this:

TABLE dump:
id INT
date DATE
event VARCHAR(100)
amount DECIMAL(10, 2)
TABLE dump_cat:
id INT
did INT (id in dump)
cid INT (id in categories)
TABLE categories:
id INT
name VARCHAR(100)

Now the query I’m trying to use is:

SELECT SUBSTR(d.date,1,7) AS month, c.name, c.id AS catid, SUM(d.amount) AS sum
 FROM dump as d, dump_cat as dc, categories AS c
 WHERE dc.did = d.id AND c.id = dc.cid AND SUBSTR(d.date, 1, 7) >= '2008-08'
 GROUP BY month, c.name ORDER BY month;

But the sum for most categories is twice as big as it should be. My guess is that this is because the join returns multiple rows, but adding “DISTINCT d.id” in the field part doesn’t make any difference. An example of what the query returns is:

+---------+--------------------------+-------+-----------+
| month   | name                     | catid | sum       |
+---------+--------------------------+-------+-----------+
| 2008-08 | Cash                     |    21 |  -6200.00 | 
| 2008-08 | Gas                      |     8 |  -2936.19 | 
| 2008-08 | Rent                     |     1 | -15682.00 | 

where as

SELECT DISTINCT d.id, d.amount FROM dump AS d, dump_cat AS dc
 WHERE d.id = dc.did AND SUBSTR(d.date, 1, 7) ='2008-08' AND dc.cid = 21;

returns

+------+----------+
| id   | amount   |
+------+----------+
| 3961 |  -600.00 | 
| 2976 |  -200.00 | 
| 2967 |  -400.00 | 
| 2964 |  -200.00 | 
| 2957 |  -300.00 | 
| 2962 | -1400.00 | 
+------+----------+

That makes a total of 3100, half of the sum listed above. If I remove “DISTINCT d.id” from the last query, every row is listed twice. This I think is the problem, but I need help to figure out how to solve it. Thanks in advance.

Added: If I collect the dump and dump_cat tables into one, with

CREATE table dumpwithcat SELECT DISTINCT d.id, d.date, d.event, d.amount, dc.cid
  FROM dump AS d, dump_cat AS c WHERE c.did = d.id;

and do the query on that table, everything works fine with correct sum. Is there a way to do this in the original query, with a subquery or something like that?

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

    That makes a total of 3100, half of the sum listed above. If I remove “DISTINCT d.id” from the last query, every row is listed twice.

    While you may have only one category per dump, you therefore must have multiple rows in dump_cat per dump. You should consider defining a UNIQUE constraint to ensure only one row exists per pair of did, cid:

    ALTER TABLE dump_cat ADD CONSTRAINT UNIQUE (did, cid);
    

    I predict this statement will fail given the current data in your table. It can’t create a unique constraint when these columns already contain duplicates!

    You can remove duplicates this way, for instance:

    DELETE dc1 FROM dump_cat dc1 JOIN dump_cat dc2 USING (did, cid)
    WHERE dc1.id > dc2.id; -- only delete the second duplicate entry
    

    edit: By the way, don’t mark my question accepted until you have verified that I’m correct! 🙂

    You can verify that there are in fact duplicates as I suggest by using a query like the following:

    SELECT did, COUNT(*)
    FROM dump_cat
    GROUP BY did
    HAVING COUNT(*) > 1;
    

    Another possibility: you have more than one category with the same name? (sorry my first try at this query was wrong, here’s an edited version)

    SELECT c.name, GROUP_CONCAT(c.id) AS cat_id_list, COUNT(*) AS c
    FROM category c
    GROUP BY c.name
    HAVING COUNT(*) > 1;
    

    FWIW, I did test the DELETE command I showed:

    INSERT INTO dump_cat (did, cid) VALUES (1, 2), (3,4), (3,4); -- duplicates!
    
    DELETE dc1 FROM dump_cat dc1 JOIN dump_cat dc2 USING (did, cid) WHERE dc1.id > dc2.id
    Query OK, 1 row affected (0.00 sec)
    

    PS: This is tangential to your question, but the DISTINCT query modifier always applies to the whole row, not just the first column. This is a common misunderstanding of many SQL programmers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have a table like this: id user score It's a dump of
I have a table that looks like this: category category_id name category_seo_friendly_url left_id right_id
I have an Oracle table dump containing insert statements. Some of the tables contain
I have made a dump of my database in which one table is pretty
I have a .sql file from a MySQL dump containing tables, definitions and data
I have table View with grouped style with three sections. Get information from dictionary
I have a MySQL table holding lots of records that i want to give
I have like 10 table inserts and each insert has 1 either 1000 or
I have a two table's ,where i have to dump the info into the
I've written a procedure to dump table data out to a text file like

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.