Solved

How to make a Store Procedure that sums in MySQL

Posted on 2011-03-04
4
373 Views
Last Modified: 2012-05-11
Hello, I need to build a store procedure that sums the results of a query. I need to make a sum of  "SubTotal" and return it. How can I do this?
 
Thanks.
SELECT 
  assignment.TskCod,
  task.TskHHP,
  IF(AssType = 'install', task.TskHHP * task.TskI_Percentage, task.TskHHP * task.TskR_Percentage) AS SubTotal
FROM
  assignment
  INNER JOIN task ON (assignment.TskCod = task.TskCod)
WHERE
  AssDate >= '2011-01-01' AND 
  AssDate <= '2011-01-31'

Open in new window

0
Comment
Question by:IvanGarcete
  • 2
4 Comments
 
LVL 8

Expert Comment

by:pdd1lan
Comment Utility
here is an example:

CREATE PROCEDURE store_procedure_name
      
AS
BEGIN

      SET NOCOUNT ON;

    WITH A AS
    (
      SELECT field1, field2
      from table1
      where some condition
     )
     
     SELECT sum(field1) as mytotal
     FROM a
     group by field2
   
END
GO
0
 

Author Comment

by:IvanGarcete
Comment Utility
I've got this following your example. SET NOCOUNT ON didn't work, and then it didn't like WITH a AS either. What can be the problem?

Thanks.

BEGIN
       /* It did not recognize this, so I comment it */
	/*SET NOCOUNT ON; */
    
    WITH a AS(
		SELECT 
			IF(AssType = 'install', task.TskHHP * task.TskI_Percentage, task.TskHHP * task.TskR_Percentage) as SubTotal
		FROM
			assignment
			INNER JOIN task ON (assignment.TskCod = task.TskCod)
		WHERE
			AssDate >= '2011-01-01' AND 
			AssDate <= '2011-01-31';
    )
    
    SELECT SUM(SubTotal)
    FROM a;
END

Open in new window

0
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
Comment Utility
WITH is a MS SQL command, not MySQL

Try like below.

If not please post some sample data and the expected result.
SELECT 
		sum(IF(AssType = 'install', task.TskHHP * task.TskI_Percentage, task.TskHHP * task.TskR_Percentage))
	FROM assignment
	INNER JOIN task ON (assignment.TskCod = task.TskCod)
	WHERE 	AssDate >= '2011-01-01' AND 
		AssDate <= '2011-01-31'

Open in new window

0
 

Author Comment

by:IvanGarcete
Comment Utility
It worked perfectly ralmada!
Thanks!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now