1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] Using GROUP BY to find the total QTY of an item on its first ever order?

Discussão em 'Outras Linguagens' iniciado por Stack, Outubro 17, 2024 às 18:32.

  1. Stack

    Stack Membro Participativo

    I'm trying to come up with a query that will pull the total quantity of an item from its first ever order. I understand how to pull out each item and their first order number was, but I'm trying to see each items first order, and the total amount of that item on that specific order. The item can have multiple lines on the same order which seems to be throwing me off. Here is an example table

    Item​
    Order#​
    Quantity
    A​
    1​
    10
    A​
    1​
    15
    A​
    2​
    5
    A​
    2​
    5
    B​
    3​
    50​

    I want the results to be like this where it just takes the items first order ( min() ) and sums the QTYs of the lines for that item on that order only:

    Item​
    Order#​
    Quantity
    A​
    1​
    25
    B​
    3​
    50​

    I did something simple like this:

    SELECT
    item,
    min(order),
    sum(quantity)

    FROM orders

    GROUP BY item


    It will show me the min order of course, but how do I get the sum to only sum the quantity of that specific item on that specific first order? Currently when I run it shows the first order with the item, but it sums the qty across all orders for the item and I'm having trouble. Any help would be appreciated. Thanks!

    Continue reading...

Compartilhe esta Página