寻找客户最后支付的价格

时间:2015-07-08 10:01:29

标签: sql sql-server-2008 group-by

我试图找到一种方法让客户支付最后的价格,我们目前的代码是:

SELECT Product.ProductCode,
       COUNT(Product.ProductCode) AS [Qty Baught],
       Product.Description,
       Customer.CustomerCode,
       Customer.Name,
       MAX(OrderHeader.DateTimeCreated) AS [Date],
       OrderLine.UnitSellPriceInCurrency AS Sell
FROM Customer
  INNER JOIN OrderHeader ON Customer.CustomerID = OrderHeader.CustomerID
  INNER JOIN OrderLine ON OrderHeader.OrderID = OrderLine.OrderID
  INNER JOIN Product ON OrderLine.ProductID = Product.ProductID
GROUP BY Product.Description,
         Product.ProductCode,
         Customer.CustomerCode,
         Customer.Name,
         OrderLine.UnitSellPriceInCurrency
HAVING (Product.ProductCode = 'bcem002')
   AND (Customer.CustomerCode = '1000')
ORDER BY MAX(OrderHeader.DateTimeCreated) DESC  

此代码显示每次价格更改但我只想查看最后价格,但DateCreated和支付的价格(UnitSellPriceInCurrency)在不同的表上。

是否可以通过(DateCreated)或其他方式对Group(UnitSellPriceInCurrency)进行分组。

我对此很新,所以如果有明显的方法可以做到这一点抱歉。

编辑:我现在用新代码得到的东西,我起床的大部分价格与产品无关

enter image description here

我想要的只是获得最后支付的价格,但在某种程度上我可以改变客户和我正在搜索的产品。

2 个答案:

答案 0 :(得分:1)

一个选项可能是使用TOP的子选项来指定您只想检索一条记录,并使用ORDER BY确保它是“最新的”:< / p>

SELECT Product.ProductCode,
       COUNT(Product.ProductCode) AS [Qty Baught],
       Product.Description,
       Customer.CustomerCode,
       Customer.Name,
       MAX(OrderHeader.DateTimeCreated) AS [Date],
         (SELECT TOP 1 O.UnitSellPriceInCurrency 
          FROM OrderLine O 
          INNER JOIN OrderHeader OH ON O.OrderID = OH.OrderID 
          WHERE OH.CustomerID = Customer.CustomerID AND O.ProductID = Product.ProductID
          ORDER BY OH.DateTimeCreated DESC) AS LatestPrice
FROM Customer
  INNER JOIN OrderHeader ON Customer.CustomerID = OrderHeader.CustomerID
  INNER JOIN OrderLine ON OrderHeader.OrderID = OrderLine.OrderID
  INNER JOIN Product ON OrderLine.ProductID = Product.ProductID
WHERE  (Customer.CustomerCode = '1000') AND (Product.ProductCode = 'bcem002')
GROUP BY Product.Description,
         Product.ProductCode,
         Product.ProductID,
         Customer.CustomerCode,
         Customer.Name,
         Customer.CustomerID
ORDER BY [Date] DESC

在此示例中,LatestPrice将包含最后为每个客户和产品插入的UnitSellPriceInCurrency 。我不确定查询是否对您的数据有意义(为什么只获得最后的价格),但这是我解释您的请求的方式。该查询仍会返回所有OrderHeader。

答案 1 :(得分:0)

请你检查一下这项工作是否合适。如果你发布了当前结果的快照和你需要的预期结果会更好。

            Select 
        *
        FROM
        (
        SELECT Product.ProductCode,
               COUNT(Product.ProductCode) AS [Qty Baught],
               Product.Description,
               Customer.CustomerCode,
               Customer.Name,
               OrderHeader.DateTimeCreated AS [Date],
               OrderLine.UnitSellPriceInCurrency AS Sell
               RANK() OVER (PARTITION BY Customer.CustomerCode,Customer.Name, OrderLine.UnitSellPriceInCurrency ORDER BY OrderHeader.DateTimeCreated) rnk
        FROM Customer
          INNER JOIN OrderHeader ON Customer.CustomerID = OrderHeader.CustomerID
          INNER JOIN OrderLine ON OrderHeader.OrderID = OrderLine.OrderID
          INNER JOIN Product ON OrderLine.ProductID = Product.ProductID
        WHERE Product.ProductCode = 'bcem002'
        AND   Customer.CustomerCode = '1000'
        GROUP BY Product.Description,
                 Product.ProductCode,
                 Customer.CustomerCode,
                 Customer.Name,
                 OrderLine.UnitSellPriceInCurrency
        ) tbl1 
        WHERE tbl1.rnk =1 
        ;