473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Multiple Sum() values with GROUP BY Problem

Hi,
I am a newbie to sql,I have a problem with querying the intended result.What i need is to get the SUM() of daily income,cost im my result set, but it gives me unintended result set hence it gets duplicated by GROUP BY clause.please correct my sql.
Thank you.

this is what i want

+------------+--------+------+
| date | Income | Cost |
+------------+--------+------+
| 2010-05-12 | NULL | 1000 |
| 2010-06-10 | 1500 | 700 |
| 2010-07-07 | NULL | 1200 |
+------------+--------+------+

this is my query
----------------
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT U.date,SUM(I.amount)AS 'Income' ,SUM(A.amount) AS 'Cost' 
  2.  
  3. FROM  
  4. (SELECT DISTINCT date from `income`GROUP BY date
  5. UNION 
  6. SELECT DISTINCT date from `advertising`GROUP BY date
  7. ) U
  8.  
  9. LEFT JOIN `income` I
  10. ON U.date = I.date
  11. LEFT JOIN `advertising` A
  12. ON U.date = A.date
  13.  
  14.  GROUP BY U.date,I.amount,A.amount
  15.  
This is what it gives me
+------------+--------+------+
| date | Income | Cost |
+------------+--------+------+
| 2010-05-12 | NULL | 1000 |
| 2010-06-10 | 500 | 300 |
| 2010-06-10 | 500 | 400 |
| 2010-06-10 | 1000 | 300 |
| 2010-06-10 | 1000 | 400 |
| 2010-07-07 | NULL | 1200 |
+------------+--------+------+
CREATE/INSERT SCRIPT
--------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE IF NOT EXISTS `advertising` (
  2.   `id` int(11) NOT NULL AUTO_INCREMENT,
  3.   `date` date NOT NULL,
  4.   `newspaper` varchar(50) NOT NULL,
  5.   `details` text NOT NULL,
  6.   `issues_day` varchar(50) NOT NULL,
  7.   `amount` double NOT NULL,
  8.   `created_date` varchar(50) NOT NULL,
  9.   `created_by` varchar(20) NOT NULL,
  10.   PRIMARY KEY (`id`)
  11. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
  12.  
  13.  
  14.  
  15. INSERT INTO `advertising` (`id`, `date`, `newspaper`, `details`, `issues_day`, `amount`, `created_date`, `created_by`) VALUES
  16. (1, '2010-07-07', 'Silumina', 'IELTS\r\n5 day express course\r\n12500/ only', '2010/04/08', 1200, '2010-07-07', 'sa'),
  17. (5, '2010-06-10', 'Silumina', 'test test', '2010/04/05', 300, '2010-07-13', 'sa'),
  18. (3, '2010-06-10', 'sunday observer', 'ISRA\r\nUK,USA, Canada, Cyprus', '2010/04/05', 400, '2010-07-13', 'sa'),
  19. (4, '2010-05-12', 'sunday observer', 'test test', '2010/04/05', 1000, '2010-07-12', 'sa');
  20. ------------------------
  21.  
  22.  
  23. CREATE TABLE IF NOT EXISTS `income` (
  24.   `id` int(11) NOT NULL AUTO_INCREMENT,
  25.   `date` date NOT NULL,
  26.   `name` varchar(50) NOT NULL,
  27.   `city_country` varchar(50) NOT NULL,
  28.   `issues_day` varchar(50) NOT NULL,
  29.   `birth_day` varchar(50) NOT NULL,
  30.   `payment_date` varchar(50) NOT NULL,
  31.   `bank` varchar(50) NOT NULL,
  32.   `amount` double NOT NULL,
  33.   `created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  34.   `created_by` varchar(50) NOT NULL,
  35.   PRIMARY KEY (`id`)
  36. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
  37.  
  38.  
  39. INSERT INTO `income` (`id`, `date`, `name`, `city_country`, `issues_day`, `birth_day`, `payment_date`, `bank`, `amount`, `created_date`, `created_by`) VALUES
  40. (14, '2010-06-10', 'Anne', 'colombo', '2010/06/10', '1999/06/10', '2010/06/10', 'BOC', 500, '2010-07-12 13:28:53', 'sa'),
  41. (13, '2010-06-10', 'Susantha', 'colombo', '2010/06/10', '1999/06/10', '2010/06/10', 'BOC', 1000, '2010-07-12 13:27:58', 'sa');
  42.  
Jul 13 '10 #1
2 6857
code green
1,726 Expert 1GB
GROUP BY the date and SUM the amounts should do it.
Change GROUP BY
Expand|Select|Wrap|Line Numbers
  1. GROUP BY U.date
Don't see why you need DISTINCT in there either
Jul 13 '10 #2
Hi ,
I really appreciate your quick response,
anyway the problem was,I have missed the 'DISTINCT'keyword in every SUM() function,I can re-write the solution as follows, which gives me the exact functionality I need.

Thank you.

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT U.date,SUM(DISTINCT I.amount)AS 'Income' ,SUM(DISTINCT A.amount) AS 'Cost' 
  2.  
  3. FROM 
  4. (SELECT date from `income`GROUP BY date 
  5. UNION 
  6. SELECT date from `advertising`GROUP BY date 
  7. ) U 
  8.  
  9. LEFT JOIN `income` I 
  10. ON U.date = I.date 
  11. LEFT JOIN `advertising` A 
  12. ON U.date = A.date 
  13.  
  14. GROUP BY U.date 
solution 2
Expand|Select|Wrap|Line Numbers
  1. SELECT t.date, Income, Cost
  2. FROM
  3.   (SELECT date FROM income
  4.   UNION
  5.   SELECT date FROM advertising
  6.   ORDER BY
  7.     date
  8.   ) t
  9.   LEFT JOIN (SELECT date, SUM(amount) AS 'Income' FROM  income GROUP BY date) i ON t.date = i.date
  10.   LEFT JOIN (SELECT date, SUM(amount) AS 'Cost' FROM advertising GROUP BY date) a ON t.date = a.date;
  11.  
solution 3
Expand|Select|Wrap|Line Numbers
  1. SELECT Date
  2.      , SUM(ins) Income
  3.      , SUM(outs) Cost
  4.   FROM
  5.      ( SELECT date, amount ins, NULL outs FROM income
  6.        UNION
  7.        SELECT date, NULL, amount FROM advertising
  8.      ) x
  9.  GROUP 
  10.     BY Date;
  11.  
Jul 13 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
4
by: randy.p.ho | last post by:
Using JDBC, is there a way to call a stored procedure with multiple return values? Thanks.
4
by: cwwilly | last post by:
Hello, Thanks for taking a look at this! Problem: I'm trying to pass multiple dynamic values between a slaveform and a masterform. The problem I'm having is on the slaveform I loop through...
4
by: David Link | last post by:
Hi, Why does adding SUM and GROUP BY destroy performance? details follow. Thanks, David Link s1.sql: SELECT t.tid, t.title, COALESCE(s0c100r100.units, 0) as w0c100r100units,
1
by: Viktor Lakics | last post by:
Dear All, I would like to have multiple default values for a field in TableA, which are stored in a separate table (TableB), and can be automatically selected depending on the value of another...
1
by: dan_williams | last post by:
Is it possible to pass multiple discrete values to a report document so that I can export it to PDF? I've managed to perform the following code to display a Crystal Report Viewer ok, but i want...
8
by: aleksandar.ristovski | last post by:
Hello all, I have been thinking about a possible extension to C/C++ syntax. The current syntax allows declaring a function that returns a value: int foo(); however, if I were to return...
7
by: Andy Sears | last post by:
I am trying to SUM values in a column where values in another column are the same. Here is what I need, but I know the 5th line won't work: UPDATE manufacturing_detail SET item_lead_time_1 = ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.