SQL Group,获得首次购买,上次购买以及与上次购买相关的最后一位员工

时间:2012-11-13 18:48:07

标签: sql-server tsql

在我的数据库中,我有客户,他们进行购买,然后这些购买与协助购买的员工相关联。我在下面写了一个查询,它将向我提供一份客户列表,列出他们已经完成的购买总数,首次购买和上次购买。我还想要与上次购买相关联的员工姓名?

Customer
-cstId
-cstName

Purchase
-cstId
-soldDate
-empId

Employee
-FirstName
-LastName
-empId


SELECT customer.cstName, MAX(purchase.soldDate) AS [Last Purchase], MIN(purchase.soldDate) AS [First Purhcase], COUNT(invTruck.invId)
               AS [Total Purchases]
FROM  customer INNER JOIN
               purchase ON customer.cstId = purchase.cstId 
               INNER JOIN
               employee ON purchase.empId = employee.empId
GROUP BY customer.cstName

4 个答案:

答案 0 :(得分:1)

您可以使用存储过程吗?对于这种情况,我通常会回到内存表中。

Declare @tblCust TABLE (
    cstid int null,
    cstName varchar(50) null, 
    lastpurchase datetime null,
    firstpurchase datetime null, 
    empid varchar(50) null
    )
Insert into @tblCust(cstid, cstname, lastpurchase, firstpurchase)        
SELECT purchase.cstid, customer.cstName, MAX(purchase.soldDate) AS [Last Purchase], 
    MIN(purchase.soldDate) AS [First Purhcase]
FROM  customer INNER JOIN
    purchase ON customer.cstId = purchase.cstId 
GROUP BY purchase.cstId, customer.cstName

Update t set EmpId = p.EmpId
From @tblCust t
        INNER JOIN Purchase p ON t.cstId = p.cstid and t.LastPurchase = p.soldDate

您现在拥有临时表上的员工ID,您可以从中返回数据,或者加入到您可能需要的任何其他表中。

答案 1 :(得分:0)

你可以使用APPLY:http://msdn.microsoft.com/library/ms175156(v=sql.105).aspx

使用交叉申请获取购买清单和相应的员工,TOP 1并按soldDate desc排序

示例:

CROSS APPLY (
select top 1 p.empId
from purchase p
where p.cstId = customer.cstId
order by soldDate desc
) o (emp)

并将o.emp添加到您的选择

我不是100%确定语法是100%完美但是这个想法就在那里:P

答案 2 :(得分:0)

我假设你在购买桌上有一些主键。我把它命名为“purchaseID”:

SELECT customer.cstName, 
    MAX(purchase.soldDate) AS [Last Purchase], 
    MIN(purchase.soldDate) AS [First Purhcase], 
    COUNT(invTruck.invId)  AS [Total Purchases], 
    LastPurchase.empID     AS [Last Purchase Employee]
FROM  customer INNER JOIN
           purchase ON customer.cstId = purchase.cstId 
           INNER JOIN
           employee ON purchase.empId = employee.empId
           CROSS APPLY (
               SELECT TOP 1 * 
               FROM purchase px 
               WHERE px.purchaseID = purchase.purchaseID 
               ORDER BY px.soldDate DESC) AS LastPurchase
GROUP BY customer.cstName,
    LastPurchase.empID

CROSS APPLY所做的是在每个记录上运行随附的select语句,也使用内部的WHERE标准。它的行为类似于INNER JOIN相对于OUTER APPLY的行为类似于LEFT JOIN。

答案 3 :(得分:0)

您只需使用简单的SCALAR SUBQUERY

进行扩展即可
SELECT
    customer.cstName,
    MAX(purchase.soldDate) AS [Last Purchase],
    MIN(purchase.soldDate) AS [First Purhcase],
    COUNT(invTruck.invId) AS [Total Purchases],
    (SELECT TOP(1) e.lastname
     FROM purchase p
     INNER JOIN employee e ON p.empId = e.empId
     WHERE customer.cstId = p.cstId
     ORDER BY p.soldDate DESC) lastPurchaseEmployee
FROM  customer
INNER JOIN purchase
    ON customer.cstId = purchase.cstId 
INNER JOIN employee
    ON purchase.empId = employee.empId
GROUP BY
    customer.cstId, customer.cstName