Solved

Select statement for statistical use

Posted on 2011-09-08
2
267 Views
Last Modified: 2012-05-12
I have a table with two rows, a date and a number:

2011-01-01, 45
2011-03-01, 50
2011-03-01, 55

The output I need is:

January, 45
February, 0
Mars, 105

Closest i get is with this select:
SELECT MONTHNAME(date), SUM(number) FROM tableA

GROUP BY MONTH(date)

This gives me:

January, 45
Mars, 105

February is missing - and i need it to feed a diagram.
0
Comment
Question by:Sunsales
2 Comments
 
LVL 15

Accepted Solution

by:
Minh Võ Công earned 500 total points
ID: 36507808
CREATE PROCEDURE DateList()
BEGIN
  DECLARE v1 INT DEFAULT 1;
 WHILE v1 <13 DO
 SELECT MONTHNAME(date), SUM(number) FROM tableA
 where MONTH(data) = v1
 GROUP BY MONTH(date)
 v1 = v1 +1;
  END WHILE;
END;
0
 
LVL 10

Expert Comment

by:OnALearningCurve
ID: 36509454
Hi Sunsales,,

I might be missing something here but the examples you give seem to be correct.

In your sample table data you have two entries for the month of March

2011-03-01, 50
2011-03-01, 55

and your result set shows a total of 105 for March.

Surely the fix would be to edit the source data?

Hope this make sense,

Mark.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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://…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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…
A simple description of email encryption using a secure portal service. This is one of the choices offered by The Email Laundry for email encryption. The other choices are pdf encryption which creates an encrypted pdf of your email and any attachmen…

914 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

16 Experts available now in Live!

Get 1:1 Help Now