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

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

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

  1. SoldItemID
  2. SoldID
  3. SoldAmount
  4. DateOfPurchase
  5. DateOfActivation
  6. CreatedDate

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

gkl3eglg

gkl3eglg1#

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

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

cyej8jka2#

对于sql server

  1. WITH CTE AS(
  2. SELECT
  3. *
  4. FROM (
  5. VALUES
  6. ('2018-08-23', 'A', 10),
  7. ('2018-08-23', 'B', 10),
  8. ('2018-08-24', 'A', 14),
  9. ('2018-08-31', 'A', 8),
  10. ('2018-08-31', 'B', 10)
  11. ) as list (Date_, Item, Amount)
  12. )
  13. select
  14. WKnum, MIN(Date_), MAX(Date_), Item, AVG(Amount)
  15. from(
  16. select
  17. *,
  18. DATEPART(WK, Date_) WKnum
  19. from CTE
  20. ) as A
  21. group by WKnum, Item
展开查看全部
kpbwa7wx

kpbwa7wx3#

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

  1. select datepart(week,DateOfPurchase),avg(item)
  2. from
  3. (
  4. select DateOfPurchase,count(SoldItemID) item
  5. from tablename group by DateOfPurchase) a
  6. group by datepart(week,DateOfPurchase)

相关问题