如何在sql中获得每周平均售出的商品数量?

w1e3prcc  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(286)

我有一个表的销售项目,其中有如下列,如

SoldItemID
SoldID 
SoldAmount
DateOfPurchase
DateOfActivation 
CreatedDate

如何在sql中获得每周平均售出的商品数量?
任何帮助都将不胜感激!

bwitn5fc

bwitn5fc1#

对于sql server

WITH CTE AS(

    SELECT 
        *
    FROM (
        VALUES  
            ('2018-08-23', 'A', 10),
            ('2018-08-23', 'B', 10),
            ('2018-08-24', 'A', 14),
            ('2018-08-31', 'A', 8),
            ('2018-08-31', 'B', 10)
        ) as list (Date_, Item, Amount)
)

select 
    WKnum, MIN(Date_), MAX(Date_), Item, AVG(Amount)    
from(
    select 
        *,
        DATEPART(WK, Date_) WKnum
    from CTE
    ) as A
group by WKnum, Item
tag5nh1u

tag5nh1u2#

我知道我参加聚会迟到了,但万一有人在找我,这也许行得通:

select date_part('week', transaction_date) week_of_the_year, item_id,
    avg(sold_qty) from your_table group by 1, 2 order by 1
gmxoilav

gmxoilav3#

尝试下面的查询:它将在sql server上工作

select datepart(week,DateOfPurchase),avg(item)
from
(
select DateOfPurchase,count(SoldItemID) item
from tablename group by DateOfPurchase) a
group by datepart(week,DateOfPurchase)

相关问题