Ayk Badalyan Ayk Badalyan - 1 year ago 334
MySQL Question

Return a list of invoices

Given a table invoice_items with the following structure:

create table invoice_items (
inv_num integer not null,
item varchar(10) not null,
price integer not null

I should write a SQL query that returns a list of invoices (
) with the total price of each. The list should be ordered by inv_num (in descending order).
For example, given:


My query should return:


Answer Source

Continuing on what Jonathon said, you should include the proper tags. At least you gave us a sample table/output to work with but you should show some attempt at figuring out your issue. If you did attempt then show us what you tried. Did you try to google your issue? What you want is to GROUP BY inv_num and to get the SUM() of the price to get the sum. You also want to ORDER BY inv_num DESC. I just repeated what you wanted and got three key terms that give you the answer. Try this.

SELECT inv_num, SUM(price) FROM invoice_items GROUP BY inv_num ORDER BY inv_num DESC

And yes, it is not bad for us to share our knowledge but sharing is not the same thing as giving out. If you stay on this Website long enough you will see many people say "We are not a code writing website.". I think you could have figured this out yourself. Yes order can be annoying but if you know WHAT you need then figuring out where it goes should not be that big of a hassle.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download