最高销售产品的SQL

时间:2013-10-31 05:24:01

标签: sql sql-server oracle

订购样品数据:

ORDER_DAY   ORDER_ID  PRODUCT_ID QUANTITY   PRICE
---------- --------- ----------- ---------- --------- 
01-JUL-11  O1        P1          5               5
01-JUL-11  O2        P2          2              10
01-JUL-11  O3        P3          10             25
01-JUL-11  O4        P1          20             5
02-JUL-11  O5        P3          5              25
02-JUL-11  O6        P4          6              20
02-JUL-11  O7        P1          2              5
02-JUL-11  O8        P5          1              50
02-JUL-11  O9        P6          2              50
02-JUL-11  O10       P2          4              10

问:两天都能获得销量最高的产品(数量*价格)

期望的输出:

DATE      PRODUCT_ID SOLD_AMOUNT
01-JUL-11      P3        250
02-JUL-11      P3        125

9 个答案:

答案 0 :(得分:1)

尝试以下查询:

select order_day, product_id, totalsale
from (select order_day,
           product_id,
           nvl(QUANTITY, 0) * PRICE as totalsale,
           dense_rank() over(partition by ORDER_DAY order by(nvl(QUANTITY, 0) * PRICE) desc) as maxsum
      from orders )
where maxsum = 1;

答案 1 :(得分:0)

您将从获得每日销售量和产品开始。使用此数据,您首先选择每天的最大利润,然后选择与此匹配的条目:

WITH PRODUCT_PER_DAY AS
(
  SELECT ORDER_DAY, PRODUCT_ID, SUM(QUANTITY * PRICE) AS SOLD_AMOUNT
  FROM MYTABLE 
  GROUP BY ORDER_DAY, PRODUCT_ID
)
SELECT ORDER_DAY, PRODUCT_ID, SOLD_AMOUNT
FROM PRODUCT_PER_DAY
WHERE (ORDER_DAY, SOLD_AMOUNT) IN
(
  SELECT ORDER_DAY, MAX(SOLD_AMOUNT)
  FROM PRODUCT_PER_DAY 
  GROUP BY ORDER_DAY
)
ORDER BY ORDER_DAY, PRODUCT_ID;

答案 2 :(得分:0)

 select b.product_id,a.order_day,a.total_price from(select order_day, max(quantity*price)as total_price from order group by order_day)a 
join (select product_id , quantity * price as total_amount from order)b on 
a.total_price= b.total_price

答案 3 :(得分:0)

尝试一下(根据需要修改表名和列名):

    Select a.order_day, b.product_id, a.sales 
    from  
          (select order_day, max(quantity*price) as sales
           from ordr
           group by order_day) a
    inner join
         (select order_day, product_id, quantity*price as sales
          from ordr) b
    on a.order_day = b.order_day and a.sales = b.sales;

答案 4 :(得分:0)

with temp as 
(select order_day,product_id,sum(quantity*price) qp
  from orders_amazn
  group by order_day,product_id
)
select * from (select order_day,product_id,qp,
      dense_rank() over (partition by order_day order by qp desc) rnk
from temp)
where rnk=1;

答案 5 :(得分:-1)

    select t.ORDER_DAY as date1 ,t.PRODUCT_ID,max(t.quantity * t.price) 
as sold_amount from table t group by t.ORDER_DAY,t.PRODUCT_ID 

答案 6 :(得分:-1)

选择date,product_id,max(product_id * price)作为sold_amount表单 按order_day分组 按order_day订购

答案 7 :(得分:-2)

请使用此查询,您将成为所需的输出。我在MySQL中尝试过。

SELECT
order_date,
Product_id,
sold_amount
FROM
(
    SELECT
        date_format(order_day,'%d-%b-%y') AS order_date,
        Product_id,
        SUM(price*quantity) AS sold_amount
    FROM
        ecommerce
    GROUP BY
        date_format(order_day,'%d-%b-%y'),
        product_id) a
WHERE
(
    order_date,sold_amount) IN
(
    SELECT
        DATE,
        MAX(sold_amt) AS sold_amount
    FROM
        (
            SELECT
                date_format(order_day,'%d-%b-%y') AS DATE,
                product_id,
                SUM(price*quantity) AS sold_amt
            FROM
                ecommerce
            GROUP BY
                date_format(order_day,'%d-%b-%y'),
                product_id)a
    GROUP BY
        DATE)

答案 8 :(得分:-3)

select order_day as orderdate,product_id,max(quantity * price) as sold_amount
from order
group by order_day
order by product_id