?
Solved

Select statement for statistical use

Posted on 2011-09-08
2
Medium Priority
?
280 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Accepted Solution

by:
Minh Võ Công earned 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

777 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