Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to make a Store Procedure that sums in MySQL

Posted on 2011-03-04
4
Medium Priority
?
381 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
ID: 35036634
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
ID: 35036755
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 2000 total points
ID: 35036904
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
ID: 35037474
It worked perfectly ralmada!
Thanks!
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

886 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