SQL Selecting multiple sums?

后端 未结 5 2091
无人及你
无人及你 2021-02-08 17:00

Let\'s say I have a table:

SELECT  SUM(quantity) AS items_sold_since_date,
        product_ID
FROM    Sales
WHERE order_date >= \'01/01/09\'
GROUP BY product_         


        
相关标签:
5条回答
  • 2021-02-08 17:34

    something like this?:

    SELECT  SUM(quantity) AS items_sold_since_date,
            total_items_sold = (SELECT SUM(quantity) from Sales GROUP BY product_ID),
            product_ID
    FROM    Sales
    WHERE order_date >= '01/01/09'
    GROUP BY product_ID
    
    0 讨论(0)
  • 2021-02-08 17:39

    You could use GROUP BY to split up the Sales based on date. In Oracle you could say:

    select count(*)
          ,case when order_date >= '01/01/09' then 'after' else 'before' end
    from   log 
    group by case when order_date >= '01/01/09' then 'after' else 'before' end;
    
    0 讨论(0)
  • 2021-02-08 17:42

    you can write

    SELECT  SUM(quantity) AS items_sold_since_date,(SELECT  SUM(quantity) AS items_sold_since_date FROM    Sales
    GROUP BY product_ID) as items_sold,
            product_ID
    FROM    Sales
    WHERE order_date >= '01/01/09'
    GROUP BY product_ID
    
    0 讨论(0)
  • 2021-02-08 17:45
    SELECT  SUM(CASE WHEN order_date >= '01/01/09' THEN quantity ELSE 0 END) AS items_sold_since_date,
            SUM(quantity) AS items_sold_total,
            product_ID
    FROM    Sales
    GROUP BY product_ID
    
    0 讨论(0)
  • 2021-02-08 17:52

    If you like to see total sales alongside, then you would use sum(sale_amt), and in the group by add the sale_amt. I hope it helps.

    0 讨论(0)
提交回复
热议问题