• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 19
Android PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 108
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic