0

我要返回一份最畅销产品的清单。目前,我的查询带回了过去 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();
        }
    }
4

2 回答 2

2

将代码更改为

 DateTime startDate = DateTime.Now.Date.AddDays(-13).Date;
于 2012-09-19T13:38:53.580 回答
1

你应该减去 7 天。如果添加 7 天,您将要求未来一周内销量最高的产品。

(虽然这将是非常有用的信息,但数据库无法预见未来。;)

您可以从结束日期计算开始日期:

DateTime? endDate = DateTime.Today.AddDays(-7);
DateTime? startDate = endDate.AddDays(-6);
于 2012-09-19T13:24:58.520 回答