Solved

How to make a Store Procedure that sums in MySQL

Posted on 2011-03-04
4
376 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 500 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 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