Win a copy of Mastering Non-Functional Requirements this week in the Design forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Get SUM of product quantity for product 'A' of Company 'XYZ' from database monthwise for whole year.  RSS feed

Mayur Saparia
Posts: 19
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a table which has product quantity column and the database has multiple entries in a single month. For eg: Date is in string format as i have used JXDatePicker so i was unable to store date in Date type in mySql . Date format is (yyyy-MM-dd) 2013-03-01 > 1200 (This is product quantity) 2013-03-05 >200

2013-04-05 > 500 2013-04-10 > 1000

2013-05-05 > 850 2013-05-10 > 50

so i want data as

for March(2013-03-01 to 2013-03-31) > SUM OF product i.e : 1400. April (2013-04-01 to 2013-04-30) > SUM OF product i.e : 1500. May (2013-05-01 to 2013-05-31) > SUM OF product i.e : 900.

also i have different kind of propducts so i will be getting it according to product , For example purpose consider this records are ONLY FOR Product 'A' of Customer 'XYZ', So please consider this in where clause.

I have tried getting the data month wise using query as follows:

select * from mm_inward Where inward_customerId=1 AND inward_productId=1 ORDER BY FROM_UNIXTIME(inward_dateofdc) ASC

and i get the data as follows

the date format have changed because I have pasted it from Excel but please keep the date format as mentioned above.

This is the actual data from my database where i have 2 entries of March 3/2/2013 > 1500 & 3/14/2013 > 120 so i want the sum of it and not differently displaying both the records.
T Mishra
Ranch Hand
Posts: 108
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ain't an expert in sql, but you may try this approach.

1. find distinct month, year from 'Date of deleivery reciept'
2. for each item, calculate sum of 'Product quantity' in the results above.
3. group by Product Name

It worked for me but perhaps you may want the advice of sql experts.
chris webster
Posts: 2407
Linux Oracle Postgres Database Python Scala
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can fetch the data you want in a single SELECT statement if you use a SQL GROUP BY to aggregate the data by company/product/month. If I were you, I'd use the actual month, so you will need to:

(a) convert your string to a proper date
(b) extract the month from that date
(c) GROUP BY the company, product and month
(d) SUM the quantity

See the MySQL website for various date-related functions to do this.

Of course, you could just substring your date string to get the month, but frankly I think you should suffer a little inconvenience for not storing your date values properly in the first place. If you store dates as strings then you prevent anybody else from doing useful date arithmetic on them easily via SQL, as they always have to convert your strings to a proper date first. This is inefficient and unnecessary. Store dates as dates.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!