Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > MySQL
|
MySQL General discussion about the MySQL database.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the MySQL section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 21st, 2011, 09:18 PM
Registered User
 
Join Date: May 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Question subtract and total query

Hello, could anyone help me with the following please:

I have a table with the following values​​:

date, value, event, element
2011-2-2, 1000 , 'input', 1
2011-7-6, 450 , 'input', 3
2011-3-2, 2000 , 'output', 1
2011-5-4, 200 , 'input', 1
2011-8-8, 500 , 'output', 3
2011-6-7, 450 , 'output', 1
2011-1-3, 230 , 'input', 2
2011-9-4, 1000 , 'input', 3
2011-3-4, 800 , 'input', 4
2011-4-9, 530 , 'output', 2
2011-5-1, 850 , 'output', 4

some rules to understand the context:

Every event 'output' has its event 'input' in an earlier date
Not all 'input' have an 'output' yet
Always 'output' value is greater than the 'input' value related.

What I need here is to generate a query that returns me the results of subtraction coupled between inputs and outputs, for example should return:

total element
1250, 1
300,2
50,3
50,4

for example:

element 1 output value 2000 in 2011-3-2 minus input value 1000 in 2011-2-2 equal subtotal 1000 plus the same element 1 output value 450 in 2011-6-7 minus input value 200 in 2011-5-4 equal subtotal 250, total: 1250.

I spend days in this and my brain is in pain.

I add some data for the soul who want to help me

PHP Code:
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- 
Table structure for events
-- ----------------------------
DROP TABLE IF EXISTS `events`;
CREATE TABLE `events` (
 `idINT(10UNSIGNED NOT NULL AUTO_INCREMENT,
 `dateDATE NOT NULL,
 `valueINT(11NOT NULL,
 `eventVARCHAR(20COLLATE utf8_spanish_ci NOT NULL,
 `elementINT(11NOT NULL,
 PRIMARY KEY (`id`)
ENGINE=INNODB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;

-- ----------------------------
-- 
Records 
-- ----------------------------
INSERT INTO `eventsVALUES ('1''2011-02-02''1000''input''1');
INSERT INTO `eventsVALUES ('2''2011-07-06''450''input''3');
INSERT INTO `eventsVALUES ('3''2011-03-02''2000''output''1');
INSERT INTO `eventsVALUES ('4''2011-05-04''200''input''1');
INSERT INTO `eventsVALUES ('5''2001-08-08''500''output''3');
INSERT INTO `eventsVALUES ('6''2011-06-07''450''output''1');
INSERT INTO `eventsVALUES ('7''2011-01-03''230''input''2');
INSERT INTO `eventsVALUES ('8''2011-09-04''1000''input''3');
INSERT INTO `eventsVALUES ('9''2011-03-04''800''input''4');
INSERT INTO `eventsVALUES ('10''2011-04-09''530''output''2');
INSERT INTO `eventsVALUES ('11''2011-05-01''850''output''4'); 
Thanks in advance

Last edited by Tokkaido; May 21st, 2011 at 09:33 PM..
 
Old May 26th, 2011, 03:07 AM
Friend of Wrox
 
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

Use a stored procedure;
Code:
DELIMITER //
DROP PROCEDURE IF EXISTS GetValue //
CREATE PROCEDURE GetValue(OUT total INT)
BEGIN
	DECLARE total_one INT default 0;
	DECLARE total_two INT default 0;
	SELECT sum(value) INTO total_one FROM events WHERE event='input' AND element=1;
	SELECT sum(value) INTO total_two FROM events WHERE event='output' AND element=1;
	
	SELECT SUM(total_two - total_one) INTO total;
	end //
DELIMITER ;
and call it with the following:
Code:
call GetValue(@total);
select @total
 
Old May 26th, 2011, 10:59 PM
Registered User
 
Join Date: May 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your answer and time, currently I solve this problem adding a column with timestamp value wich I create in the moment when I add an "input" row, and when I add a "output" row I copy that value therefore I "know" exactly wich pair of rows are related, but I will study the code You so kindly share with me. Cordially

HSO





Similar Threads
Thread Thread Starter Forum Replies Last Post
Total Query kbsudhir Access 1 February 16th, 2008 05:41 AM
Problem in calculating total sales in MDX query jaee SQL Server 2000 0 March 9th, 2007 11:10 AM
Problems with multiple total query. cstooch Classic ASP Databases 0 May 14th, 2006 02:09 PM
run total in query stoneman Access 1 December 23rd, 2003 04:43 AM
total columns in sql query nlicata SQL Server ASP 1 August 4th, 2003 06:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.