-2

This should be easy, but I can't figure it out. I have a product table which looks like this (very abbreviated for clarity)

product_quantity

So all I want to do is to aggragate the quantities for each product id eg product id 128 quantity of 37. All I have come up with is the following, which groups product_name but does not add the quantities correctly. This is Laravel Query builder code. But no matter, raw SQL is fine if you can help. Thank You !

Vince
  • 1,405
  • 2
  • 20
  • 33
  • 1
    Nothing in your code attempts to do any SUM. In SQL, the query would be `SELECT products.product_id, sum(orders.quantity) as qty FROM... GROUP BY products.product_id`. Where's your SUM()? – Ken White Jan 18 '18 at 01:14
  • 1
    See [Why should I provide an MCVE for what seems to me to be a very simple SQL query](http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query) – Strawberry Jan 18 '18 at 01:14
  • 1
    @KenWhite Thanks for your help. I don't know where to start. I have researched before posting. Won't your solution simply aggregate the totality of the quantity column? If so, this is not want I need. I need to first select all the product_id's of the same type, then sum those quantities. Then go onto the next product_id. I have not found it quite so easy. – Vince Jan 18 '18 at 01:21
  • Try writing the SQL itself first, and see how it works. Fine-tune that to suit your specific needs, and then work on translating that into Laravel. If you figure out the SQL first, it's much easier to use it. – Ken White Jan 18 '18 at 01:25
  • @KenWhite Thanks for your advice. I often use raw sql inside Laravel. My problem is that I do not know how to construct this query in SQL. Thank you for your positive attitude. I truly hate SO for the condemnation Newbs get. – Vince Jan 18 '18 at 01:33
  • I gave you a sample query in my original comment. Did you try it? GROUP BY does exactly what you're asking, if I understand you correctly. Just make sure you include all non-aggregated columns in the GROUP BY expression. – Ken White Jan 18 '18 at 01:36
  • You could have found the answer to this by googling 'aggregate', 'group' or 'sum' with sql' and reading any SQL intro. Yet you didn't. Yet you expect others to read your question and to write you an answer. It is poor attitudes & actions that draw poor receptions. – philipxy Jan 18 '18 at 07:10
  • @philipxy - Your assumption is wrong. Every single answer both on SO and Google refereed to aggregating a column as a whole NOT how to aggregate by selecting individual ids and then moving to the next id. It is exactly this patronising attitude towards newbs which causes so many to hate SO. Be part of the solution - not the problem. – Vince Jan 18 '18 at 18:46
  • I have no idea what "aggregating a column as a whole" or "aggregate by selecting individual ids and then moving to the next id" mean. Also I suggest you google my comments re 'google many variations of your question'. – philipxy Jan 18 '18 at 18:53
  • Well, it is clear then, that you did not understand the technical nature of my question in the first place. Be like Ken White, he understood the question and he offered positive help. – Vince Jan 18 '18 at 19:22

1 Answers1

-1

You need to use the sum aggregate function, here is a small sample

select ord.product,ord.quote_name,sum(ord.quantity) from order ord where ord.status = "open" groupBy(ord.product)
sam winston
  • 162
  • 1
  • 13