SQL Server SUM GROUP BY Help

I have a table that contains an orderid, an inventoryid, and a quantity -- its a line items table. The database is SQL Server 2008.

What I need to know is how to write a SQL statement that returns the sums of quantities for that itemid at that order, not counting orders that have larger orderids than it. It must return the orderid, itemid, and total.

Any help? Thanks!

Answers


Guess:

SELECT
    SUM(quantity) AS total, --"sums of quantities for that itemid at that order"
    orderid, inventoryid --"It must return the orderid, itemid"
WHERE
    orderid < (some larger order id value) --"not counting orders that have larger orderids"
GROUP BY
    orderid, inventoryid

Need Your Help

Data files when using intepreted languages

interpreted-language data-driven compiled-language

Say I have some data which I want several kinds of. I think the easiest example could be tiles in a game: I would have grass, rock, tree, etc. each with different sets of values.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.