我要返回一份最畅销产品的清单。目前,我的查询带回了过去 7 天的订单总额。
我还需要带回前 7 天的项目数量总和,我想知道我将如何去做?我在想它只是在@startDate 上增加 7 天,在@endDate 上增加 13 天(这已经将日期设置为今天日期之后的 7 天)
当前在sum(amzOrdersItem.itemQty) AS Orders行中获取项目数量。这实际上返回了产品订单的计数。
ALTER PROCEDURE [dbo].[GetHighestSellingItems]
@startDate datetime,
@endDate datetime
AS
BEGIN
SET NOCOUNT ON;
SELECT TOP (20) twProducts.sku,
twProducts.title + ' / ' + LTRIM(CAST(twProducts.strength AS varchar(15))) + '%' AS Title,
sum(amzOrdersItem.itemQty) AS Orders,
twProducts.stock, aboProducts.asin, aboProducts.amzPrice
FROM twProducts INNER JOIN
amzOrdersItem ON twProducts.sku = amzOrdersItem.productSku INNER JOIN
amzOrders ON amzOrdersItem.amzOrderId = amzOrders.amzOrderId INNER JOIN
aboProducts ON twProducts.sku = aboProducts.sku
where (amzOrders.orderDate between @startDate and @endDate) and amzOrders.cancelled = 0
group by twProducts.sku, twProducts.title, twProducts.strength,
twProducts.stock, aboProducts.asin, aboProducts.amzPrice
ORDER BY sum(amzOrdersItem.itemQty) DESC
END
认为这可能很方便,这是我填充到查询中并获得结果的.net 代码。
public List<GetHighSelling> GetHighSellingWeek()
{
DateTime? endDate = DateTime.Now.Date;
DateTime? startDate = DateTime.Now.Date.AddDays(-6).Date;
using (aboDataDataContext dc = new aboDataDataContext())
{
var query = from o in dc.GetHighestSellingItems(startDate, endDate)
select new GetHighSelling
{
sku = o.sku,
title = o.Title,
itemQty = o.Orders,
stock = o.stock,
amzPrice = o.amzPrice.ToString(),
asin = o.asin
};
return query.ToList();
}
}