SQL Server 2012 合并 OUTER APPLY 相关的记录 table
SQL Server 2012 Merge Records in OUTER APPLY related table
我有一个包含客户详细信息的 Table "Customers" 和 Table "CALLS" 我存储每个电话的结果的地方
当我需要获取必须呼叫的客户列表时,我使用此查询
SELECT *
FROM (
SELECT TOP (50) S.ID,S.URL,S.Phone,S.Email
FROM dbo.Customers AS S
WHERE URL is not null and City like 'Berl%'
ORDER BY S.ID
) AS S
OUTER APPLY (
SELECT TOP (3) I.CalledOn, I.Answer
FROM dbo.Calls AS I
WHERE S.URL = I.URL
ORDER BY I.CalledOn DESC
) AS I;
我在哪里可以得到城市中所有客户的名单以及最后 3 个答案。
但这 returns 每个客户最多 3 条记录,而我只有一条
并将CalledOn和Answer的3个值汇总到同一条记录中
更清楚一点:
现在:
+-----------+---------------+-------------+------------------+
|Customer 1 | 555-333 333 | 02-10-17 | Call Tomorrow |
+-----------+---------------+-------------+------------------+
|Customer 2 | 555-444 333 | 02-10-17 | Call Tomorrow |
+-----------+---------------+-------------+------------------+
|Customer 1 | 555-333 333 | 02-11-17 | Call Tomorrow |
+-----------+---------------+-------------+------------------+
|Customer 1 | 555-333 333 | 02-12-17 | Stop Calling |
+-----------+---------------+-------------+------------------+
预计
+-----------+---------------+--------------------------------+
|Customer 1 | 555-333 333 | 02-12-17 : Call Stop Calling |
| | | 02-11-17 : Call Tomorrow |
| | | 02-10-17 : Call Tomorrow |
+-----------+---------------+-------------+------------------+
|Customer 2 | 555-444 333 | 02-10-17 | Call Tomorrow |
+-----------+---------------+-------------+------------------+
目前我正在使用服务器端逻辑实现此目的,但我确信使用 TSQL 可以更轻松、更好地完成它
可以指点方向吗?
谢谢
对于 SQL-Server 2012
SELECT TOP (50) S.ID, S.URL, S.Phone, S.Email,
STUFF((SELECT CHAR(10) + concat (I.CalledOn, ' ', I.Answer)
FROM dbo.Calls AS I
WHERE S.URL = I.URL
ORDER BY I.CalledOn DESC
FOR XML PATH('')
), 1, 1, '') AS CallAnswer
FROM dbo.Customers AS S
WHERE URL is not null and City like 'Berl%'
ORDER BY S.ID
v下一个:
SELECT TOP (50) S.ID, S.URL, S.Phone, S.Email,
(SELECT TOP (3) STRING_AGG(CONCAT(I.CalledOn, ' ', I.Answer), CHAR(13))
FROM dbo.Calls AS I
WHERE S.URL = I.URL
ORDER BY I.CalledOn DESC
) AS CallAnswer
FROM dbo.Customers AS S
WHERE URL is not null and City like 'Berl%'
ORDER BY S.ID
我有一个包含客户详细信息的 Table "Customers" 和 Table "CALLS" 我存储每个电话的结果的地方
当我需要获取必须呼叫的客户列表时,我使用此查询
SELECT *
FROM (
SELECT TOP (50) S.ID,S.URL,S.Phone,S.Email
FROM dbo.Customers AS S
WHERE URL is not null and City like 'Berl%'
ORDER BY S.ID
) AS S
OUTER APPLY (
SELECT TOP (3) I.CalledOn, I.Answer
FROM dbo.Calls AS I
WHERE S.URL = I.URL
ORDER BY I.CalledOn DESC
) AS I;
我在哪里可以得到城市中所有客户的名单以及最后 3 个答案。
但这 returns 每个客户最多 3 条记录,而我只有一条 并将CalledOn和Answer的3个值汇总到同一条记录中
更清楚一点: 现在:
+-----------+---------------+-------------+------------------+
|Customer 1 | 555-333 333 | 02-10-17 | Call Tomorrow |
+-----------+---------------+-------------+------------------+
|Customer 2 | 555-444 333 | 02-10-17 | Call Tomorrow |
+-----------+---------------+-------------+------------------+
|Customer 1 | 555-333 333 | 02-11-17 | Call Tomorrow |
+-----------+---------------+-------------+------------------+
|Customer 1 | 555-333 333 | 02-12-17 | Stop Calling |
+-----------+---------------+-------------+------------------+
预计
+-----------+---------------+--------------------------------+
|Customer 1 | 555-333 333 | 02-12-17 : Call Stop Calling |
| | | 02-11-17 : Call Tomorrow |
| | | 02-10-17 : Call Tomorrow |
+-----------+---------------+-------------+------------------+
|Customer 2 | 555-444 333 | 02-10-17 | Call Tomorrow |
+-----------+---------------+-------------+------------------+
目前我正在使用服务器端逻辑实现此目的,但我确信使用 TSQL 可以更轻松、更好地完成它
可以指点方向吗?
谢谢
对于 SQL-Server 2012
SELECT TOP (50) S.ID, S.URL, S.Phone, S.Email,
STUFF((SELECT CHAR(10) + concat (I.CalledOn, ' ', I.Answer)
FROM dbo.Calls AS I
WHERE S.URL = I.URL
ORDER BY I.CalledOn DESC
FOR XML PATH('')
), 1, 1, '') AS CallAnswer
FROM dbo.Customers AS S
WHERE URL is not null and City like 'Berl%'
ORDER BY S.ID
v下一个:
SELECT TOP (50) S.ID, S.URL, S.Phone, S.Email,
(SELECT TOP (3) STRING_AGG(CONCAT(I.CalledOn, ' ', I.Answer), CHAR(13))
FROM dbo.Calls AS I
WHERE S.URL = I.URL
ORDER BY I.CalledOn DESC
) AS CallAnswer
FROM dbo.Customers AS S
WHERE URL is not null and City like 'Berl%'
ORDER BY S.ID