目前我的NumberOfDaysBetweenPurchases
每ItemNumber
AccountNumber
位ItemNumber|AVERAGENumberOfDaysBetweenPurchases
。我试图联合NumberOfDaysBetweenPurchases
我有两个问题。 1,我找不到一种方法来获得订单之间所有天数的平均值。 2,如果没有按帐号或购买日期订购/分组,我无法显示。因此,每次客户订购产品时,它总是会显示数百万行。
以下是我用来获取datediff(day, lag(OrderDate,1) over (partition by AccountNumber
order by OrderDate), OrderDate) as [NumOfDaysBetweenOrdersByAccountNum]
:
ItemNumbers
如何整合ItemNumber | AvgNumberOfDaysBetweenOrdersByAccountNumber
12345 6
452234 45
5235 3
以便它们只显示一次以及按帐号排序的平均天数?我希望它能像这样出现:
ItemNumber | NumberDays(...) | OrderDate | AccountNumber
123 0 ---- 101010
123 1 ---- 101010
123 4 ---- 101010
123 7 ---- 101010
123 8 ---- 101010
以下是我当前信息的示例(数百万行):
$list = ["fa1", "fa2", "fa3", "fa4", "fa5"];
PutFileContentList($list);
function PutFileContentList(array $list) {
globals $file, $pcarray, $voiparray;
foreach ($list as $varname) {
$line = isset($GLOBALS[$varname]) ? $GLOBALS[$varname] : "";
if ($line == "PC") {
file_put_contents($file, $pcarray . FILE_APPEND);
}
if ($line == "VOIP") {
file_put_contents($file, $voiparray . FILE_APPEND);
}
}
}
答案 0 :(得分:1)
根据我对你的问题的理解,试试这个。
WITH cte AS (
SELECT ItemNumber, ItemNumber, AccountNumber,
datediff(day, lag(OrderDate,1) over (partition by AccountNumber order by OrderDate), OrderDate) as [NumOfDaysBetweenOrdersByAccountNum]
)
SELECT
ItemNumber
, AVG(NumberOfDaysBetweenOrdersByAccountNum)
FROM
cte
GROUP BY
ItemNumber