我有一个数据库,包含按天汇总的各个零件的价格(PartPrices)。我想运行一个查询,以查看哪些部分的价格增长最多,哪个部分的价格在日期范围内下降最多。此日期范围可以是从开始日期到现在,也可以是开始日期到任何时间段。
架构如下所示:
$list = Get-PnPList -Identity Documents
$web = $list.ParentWeb
$folder = Ensure-PnPFolder -Web $list.ParentWeb -SiteRelativePath "Shared Documents/MoveTo"
$tofolder = Ensure-PnPFolder -Web $list.ParentWeb -SiteRelativePath "Shared Documents/MoveTwo"
function MoveFolder
{
[cmdletbinding()]
Param (
$web,
$fromFolder,
$toFolder
)
$fromFolder.Context.Load($fromFolder.Files)
$fromFolder.Context.Load($fromFolder.Folders)
$fromFolder.Context.ExecuteQuery()
foreach ($file in $fromFolder.Files)
{
$targetFileUrl = $file.ServerRelativeUrl.Replace($fromFolder.ServerRelativeUrl, $toFolder.ServerRelativeUrl);
$file.MoveTo($targetFileUrl, [Microsoft.SharePoint.Client.MoveOperations]::Overwrite);
}
$fromFolder.Context.ExecuteQuery();
foreach ($subFolder in $fromFolder.Folders)
{
$targetFolderUrl = $subFolder.ServerRelativeUrl.Replace($fromFolder.ServerRelativeUrl, $toFolder.ServerRelativeUrl);
$targetFolderRelativePath = $targetFolderUrl.SubString($web.RootFolder.ServerRelativeUrl.Length)
$tofolder = Ensure-PnPFolder -Web $list.ParentWeb -SiteRelativePath $targetFolderRelativePath
MoveFolder -Web $web -fromFolder $subFolder -toFolder $tofolder
}
}
$web.Context.Load($web.RootFolder)
$web.Context.ExecuteQuery()
MoveFolder -Web $web -fromFolder $folder -toFolder $tofolder
$folder.DeleteObject()
$web.Context.ExecuteQuery()
基本上我拥有的内容如下:
PartID | DateTime (Time stamp from start of day) | Price
我觉得这个查询没有被优化,这就是为什么我要联系SO社区进行输入。如果您认为我应该以不同方式处理这些数据,我也愿意接受建议。提前致谢。以下是PartPrices的一些原始样本数据(请记住,这些数据是夸大的,以避免添加大量数据):
DECLARE @StartDate AS DATETIMEOFFSET
DECLARE @EndDate AS DATETIMEOFFSET
WITH LastPartPrices AS (
SELECT *
FROM
(SELECT
PartID
,DateTime
,Price
FROM PartPrices
WHERE PartPrices.DateTime <= @EndDate
GROUP BY `PartID`, DateTime DESC, Price) t
GROUP BY `PartID`),
HighestPartPrices AS (
SELECT *
FROM
(SELECT
PartID
,DateTime
,Price
FROM PartPrices
WHERE PartPrices.DateTime BETWEEN @StartDate AND @EndDate
GROUP BY `PartID`, Price DESC, DateTime) t
GROUP BY `PartID`),
LowestPartPrices AS (
SELECT *
FROM
(SELECT
PartID
,DateTime
,Price
FROM PartPrices
WHERE PartPrices.DateTime BETWEEN @StartDate AND @EndDate
GROUP BY `PartID`, Price ASC, DateTime) t
GROUP BY `PartID`)
SELECT
Parts.ID
,Parts.Name
,Parts.Description
,LastPartPrices.Price AS LastPrice
,( (LastPartPrices.Price - HighestPartPrices.Price) / HighestPartPrices.Price ) AS HighCurrentDifference
,( (LastPartPrices.Price - LowestPartPrices.Price) / LowestPartPrices.Price ) AS LowCurrentDifference
FROM Parts
INNER JOIN LastPartPrices ON Parts.ID = LastPartPrices.PartID
INNER JOIN HighestPartPrices ON Parts.ID = HighestPartPrices.PartID
INNER JOIN LowestPartPrices ON Parts.ID = LowestPartPrices.PartID
我期望获得以下内容:
{1, '2016-03-01T00:00:00+00:00', 150.40 },
{1, '2016-03-02T00:00:00+00:00', 170.50 },
{1, '2016-03-03T00:00:00+00:00', 160.00 },
{2, '2016-03-01T00:00:00+00:00', 80.30 },
{2, '2016-03-02T00:00:00+00:00', 100.00 },
{2, '2016-03-03T00:00:00+00:00', 120.00 },
{3, '2016-03-01T00:00:00+00:00', 10.50 },
{3, '2016-03-02T00:00:00+00:00', 20.10 },
{3, '2016-03-03T00:00:00+00:00', 30.00 }