jeudi 17 novembre 2016

SQL Group by statement where value is selected by greatest quantity

I apologize in advance if this has already been discussed in a different thread. I did perform several searched but was unable to locate one that would assist me with my needs. I have a table that is called t_Item_log. I need to write a query that will pull the data, group by item, sum the total quantities for the item, and select the code that has the greatest qty associated with that item.

Table Example:

ID  ITEM    CODE    QTY
1       12345   X       1
2       12345   X       2
3       12345   Y       1
4       12345   Z       3
5       6789    X       1
6       6789    X       2
7       6789    Y       1
8       6789    Z       2
9       6789    X       3
10      12345   Y       4

Desired Result

ITEM        CODE    QTY
12345   Y       11
6789    X       9

If anyone can point me to a thread that already covered this or provide a foundation for how to write this statement it would be appreciated.

Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire