Search code examples
sqlgroup-bysql-order-bysnowflake-cloud-data-platformmin

SQL get the date with the least profit made


Currently I am working on a query to get the Date with the least made profit. The table, that I am using for this is this one:

enter image description here

The query I came up with finds the date with the least made profit, but doesnt show the SUM of the amount made in that day:

SELECT MIN(PRICE*QUANTITY) AS UMSATZ, ORDER_DATE
FROM FACT_ORDER
GROUP BY ORDER_DATE
ORDER BY UMSATZ
LIMIT 1;

So the result is: enter image description here

Which query could show the SUM amount of the profit made that day in addition (not with an additional column)? Thank you very much in advance!


Solution

  • Which query could show the SUM amount of the profit made that day in addition (not with an additional column)?

    To get the total:

    SELECT DISTINCT ORDER_DATE,
         SUM(PRICE*QUANTITY) OVER(PARTITION BY ORDER_DATE) AS UMSATZ 
    FROM FACT_ORDER
    QUALIFY (PRICE*QUANTITY) = MIN(PRICE*QUANTITY) OVER(PARTITION BY ORDER_DATE);