我有一个已售商品表,其中有以下列,如
SoldItemID
SoldID
SoldAmount
DateOfPurchase
DateOfActivation
CreatedDate
如何获取SQL每周平均售出的商品数量?
任何帮助将不胜感激!
答案 0 :(得分:1)
我知道我参加晚会很晚,但是以防万一其他人正在寻找,这可能有效:
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
答案 1 :(得分:0)
对于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
答案 2 :(得分:0)
尝试以下查询:它将在sql服务器上工作
select datepart(week,DateOfPurchase),avg(item)
from
(
select DateOfPurchase,count(SoldItemID) item
from tablename group by DateOfPurchase) a
group by datepart(week,DateOfPurchase)