Sql查询:下一个最后一行

时间:2017-07-17 08:06:30

标签: sql sql-server tsql

我有CustomerCalls这样的表:

客户表:

  • CustomerId:uniqueidentifier
  • BusinessName:nvarchar(MAX)

致电表:

  • CallId:uniqueidentifier
  • CallDate:datetime
  • CustomerId:uniqueidentifier
  • 状态:int(呼叫已被回答或否)

我想让所有客户获得最后一次调用和下一次调用列。

Last Called是status = 1的最后一次通话。Next Call是状态= 0和Calldate > now日期的通话。如果没有,则为空。这是一个非工作示例,只是为了显示格式

Select * 
From Customers,
       Calls.CallId as LastCalledId   
       Calls.CallDate as LastCalledDate
       Calls.CallId as NextCallId
       Calls.CallDate as NextCallDate
LEFT JOIN 
    Calls ON Customers.CustomerId = Calls.CustomerId

我该怎么做?

5 个答案:

答案 0 :(得分:1)

这是你的意思吗?

<强>更新

select c.id
, lastcall.calldate
, lastcall.id
, firstcall.calldate
, firstcall.id
from @customers c
outer apply (select top 1 calls.calldate, calls.id from @calls calls where calls.custid = c.id and calldate < getdate() order by calldate desc) lastcall(calldate, id)
outer apply (select top 1 calls.calldate, calls.id from @calls calls where calls.custid = c.id and calldate >= getdate() order by calldate asc) firstcall(calldate, id)

原始答案:

select c.id
, answered.calldate LastCalledDate
, answered.id LastCalledID
, unaswered.calldate NextCalledDate
, unaswered.id NextCalledID
from @customers c
left join @calls answered ON answered.custid = c.id AND status = 1
left join @calls unaswered ON unaswered.custid = c.id AND unaswered.status = 0 and unaswered.calldate > getdate()

答案 1 :(得分:0)

您可以使用 LEFT JOIN 中的 WHERE 子句来获取所需的数据行。您可以使用带有Calls.status = 0或1的where子句(根据您的要求) )

答案 2 :(得分:0)

您可以在加入查询中使用 AND 。然后订购电话并获得最后一个限制。

SELECT * 
FROM Customers,
       Calls.CallId as LastCalledId   
       Calls.CallDate as LastCalledDate
       Calls.CallId as NextCallId
LEFT JOIN 
    Calls ON (Customers.CustomerId = Calls.CustomerId AND Calls.Status = 1)
ORDER BY Calls.CallDate DESC
UNION
SELECT * 
FROM Customers,
       Calls.CallId as LastCalledId   
       Calls.CallId as NextCallId
       Calls.CallDate as NextCallDate
LEFT JOIN 
    Calls ON (Customers.CustomerId = Calls.CustomerId AND Calls.Status = 0 AND CallDate > NOW())
ORDER BY Calls.CallDate DESC;

答案 3 :(得分:0)

由于您没有为样本数据提供DDL和DML,因此我无法测试我的答案,因此无法保证产生所需的结果。
话虽如此,我的尝试应该适用于2008或更高版本。

使用cte计算每个客户的最后一个应答呼叫(假设calldate是唯一的)和几个左连接我想出了这个:

;WITH LastCall AS
(
    SELECT  CustomerId,
            CallId,
            CallDate
    FROM Calls t0
    WHERE [Status] = 1
    AND CallDate = 
    (
        SELECT MAX(CallDate)
        FROM Calls t1
        WHERE [Status] = 1
        AND t1.CustomerId = t0.CustomerId
    )
)

SELECT  cu.CustomerId, 
        cu.Name,
        lc.CallId as LastCalledId,
        lc.CallDate as LastCalledDate,
        nc.CallId as NextCallId,
        nc.CallDate as NextCallDate
FROM Customers as cu
LEFT JOIN LastCall lc ON cu.CustomerId = lc.CustomerId 
LEFT JOIN Calls nc ON cu.CustomerId = nc.CustomerId AND nc[Status] = 0 AND nc.CallDate > GETDATE()

请注意,对于2012及更高版本,您可以使用FIRST_VALUELAST_VALUE内置功能。<​​/ p>

答案 4 :(得分:0)

您可以使用不同的方式执行此操作:

    Select cu.CustomerId,LastCall.Callid,LastCall.calldate,NextCall.Callid,NextCall.calldate from Customers cu,
    (select max(callid) Callid,max(calldate) calldate,CustomerId from Calls c where status = 1 group by CustomerId) LastCall,
    (select max(callid) Callid,max(calldate) calldate,CustomerId from Calls c where status = 0 and calldate > sysdate group by CustomerId) NextCall
    where cu.CustomerId = LastCall.CustomerId and cu.CustomerId = NextCall.CustomerId
    order by 1

Or by replacing in your query :

    Select (select max(callid) from Calls c where c.CustomerId = Cu.CustomerId and status = 1) as LastCalledId,
       (select max(CallDate) from Calls c where c.callid = (select max(callid) from Calls c where c.CustomerId = Cu.CustomerId and status = 1)) as LastCalledDate,
       (select max(callid) from Calls c where c.CustomerId = Cu.CustomerId and status = 0 and CallDate > sysdate ) as NextCallId,
       (select max(CallDate) from Calls c where c.callid = (select max(callid) from Calls c where c.CustomerId = Cu.CustomerId and status = 0 and CallDate > sysdate )) as NextCallDate 
    From Customers Cu,Calls c ON Cu.CustomerId = c.CustomerId