1

在我的数据库中,我有进行购买的客户,然后这些购买与协助购买的员工相关联。我在下面写了一个查询,它将为我提供一个客户列表,其中包括他们总共购买了多少次、他们的第一次购买和最后一次购买。我还想要与上次购买关联的员工姓名?

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

4 回答 4

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,您可以从该表中返回您的数据,或者加入您可能需要的任何其他表。

于 2012-11-13T19:14:16.727 回答
0

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

使用 cross apply 获取购买列表和相应员工,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

于 2012-11-13T19:14:13.703 回答
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 条件。相对于 OUTER APPLY,它的行为类似于 INNER JOIN,而 OUTER APPLY 的行为类似于 LEFT JOIN。

于 2012-11-13T19:23:20.020 回答
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
于 2012-11-13T21:13:45.530 回答