SQL 服务器:汇总为单个结果
SQL Server: aggregate to single result
我有这个问题
SELECT Client.ClientNo,
Client.ContactName,
Deal.Currency,
MAX(Deal.DealDate)
FROM Deal
JOIN Client ON Deal.ClientNo = Client.ClientNo
GROUP BY Client.ClientNo, Client.ContactName, Deal.Currency;
这给了我一个结果
1 John Smith EUR 2014-10-07
1 John Smith GBP 2014-11-12
2 Jane Doe GBP 2014-09-17
2 Jane Doe USD 2014-12-23
1 John Smith USD 2013-11-13
2 Jane Doe EUR 2012-09-06
问题是,我需要每个客户的最新日期的汇总结果,如下所示:
1 John Smith GBP 2014-11-12
2 Jane Doe USD 2014-12-23
如何更改我的查询以实现此目的?
UPDATE 感谢 jarlh 的回答,但是我遗漏了一些东西 - 如果有重复的行 - 它会保留在结果中,如下所示:
1 John Smith GBP 2014-11-12
1 John Smith GBP 2014-11-12
2 Jane Doe USD 2014-12-23
有什么方法可以实现吗?
未经测试,但应该可以。如果客户在同一天有两笔(或更多笔)交易,那么将 return 为该客户记录几行。
SELECT Client.ClientNo,
Client.ContactName,
Deal.Currency,
Deal.DealDate
FROM Deal
JOIN Client ON Deal.ClientNo = Client.ClientNo
WHERE Deal.DealDate = (select max(DealDate) from Deal
where ClientNo = Client.ClientNo)
你可以这样做:
测试数据:
DECLARE @Deal TABLE(ClientNo INT,Currency VARCHAR(10),DealDate DATETIME)
DECLARE @Client TABLE(ClientNo INT,ContactName VARCHAR(100))
INSERT INTO @Deal
VALUES (1,'EUR','2014-10-07'),(1,'GBP','2014-11-12'),(2,'GBP','2014-09-17'),
(2,'USD','2014-12-23'),(1,'USD','2013-11-13'),(2,'EUR','2012-09-06')
INSERT INTO @Client
VALUES (1,'John Smith'),(2,'Jane Doe')
查询:
;WITH latestDeals
AS
(
SELECT
ROW_NUMBER() OVER(PARTITION BY ClientNo ORDER BY DealDate DESC) AS RowNbr,
Deal.*
FROM
@Deal AS Deal
)
SELECT
client.ClientNo,
client.ContactName,
latestDeals.Currency,
latestDeals.DealDate
FROM
@Client AS client
JOIN latestDeals
ON client.ClientNo=latestDeals.ClientNo
AND latestDeals.RowNbr=1
更新:
如果要使用常规查询。你可以这样做:
SELECT
client.ClientNo,
client.ContactName,
Latestdeal.maxDealDate as DealDate,
deal.Currency
FROM
@Client AS client
JOIN
(
SELECT
MAX(Deal.DealDate) AS maxDealDate,
Deal.ClientNo
FROM
@Deal AS Deal
GROUP BY
Deal.ClientNo
) AS Latestdeal
ON client.ClientNo=Latestdeal.ClientNo
JOIN @Deal as deal
ON client.ClientNo=deal.ClientNo
AND deal.DealDate=Latestdeal.maxDealDate
这将导致相同的输出
结果:
1 John Smith GBP 2014-11-12 00:00:00.000
2 Jane Doe USD 2014-12-23 00:00:00.000
试试这个,
测试数据:
CREATE TABLE #YourTable
(
CLIENT_NO INT,
CONTACT_NAME VARCHAR(20),
CURRENCY VARCHAR(10),
[DEAL_DATE] DATE
)
INSERT INTO #YourTable VALUES
(1,'John Smith','EUR','2014-10-07'),
(1,'John Smith','GBP','2014-11-12'),
(2,'Jane Doe','GBP','2014-09-17'),
(2,'Jane Doe','USD','2014-12-23'),
(1,'John Smith','USD','2013-11-13'),
(2,'Jane Doe','EUR','2012-09-06')
查询:
SELECT CLIENT_NO,CONTACT_NAME,CURRENCY,[DEAL_DATE]
FROM (SELECT *,
Row_Number()
OVER (
PARTITION BY CLIENT_NO
ORDER BY [DEAL_DATE] DESC) AS RN
FROM #YourTable)A
WHERE RN = 1
我有这个问题
SELECT Client.ClientNo,
Client.ContactName,
Deal.Currency,
MAX(Deal.DealDate)
FROM Deal
JOIN Client ON Deal.ClientNo = Client.ClientNo
GROUP BY Client.ClientNo, Client.ContactName, Deal.Currency;
这给了我一个结果
1 John Smith EUR 2014-10-07
1 John Smith GBP 2014-11-12
2 Jane Doe GBP 2014-09-17
2 Jane Doe USD 2014-12-23
1 John Smith USD 2013-11-13
2 Jane Doe EUR 2012-09-06
问题是,我需要每个客户的最新日期的汇总结果,如下所示:
1 John Smith GBP 2014-11-12
2 Jane Doe USD 2014-12-23
如何更改我的查询以实现此目的?
UPDATE 感谢 jarlh 的回答,但是我遗漏了一些东西 - 如果有重复的行 - 它会保留在结果中,如下所示:
1 John Smith GBP 2014-11-12
1 John Smith GBP 2014-11-12
2 Jane Doe USD 2014-12-23
有什么方法可以实现吗?
未经测试,但应该可以。如果客户在同一天有两笔(或更多笔)交易,那么将 return 为该客户记录几行。
SELECT Client.ClientNo,
Client.ContactName,
Deal.Currency,
Deal.DealDate
FROM Deal
JOIN Client ON Deal.ClientNo = Client.ClientNo
WHERE Deal.DealDate = (select max(DealDate) from Deal
where ClientNo = Client.ClientNo)
你可以这样做:
测试数据:
DECLARE @Deal TABLE(ClientNo INT,Currency VARCHAR(10),DealDate DATETIME)
DECLARE @Client TABLE(ClientNo INT,ContactName VARCHAR(100))
INSERT INTO @Deal
VALUES (1,'EUR','2014-10-07'),(1,'GBP','2014-11-12'),(2,'GBP','2014-09-17'),
(2,'USD','2014-12-23'),(1,'USD','2013-11-13'),(2,'EUR','2012-09-06')
INSERT INTO @Client
VALUES (1,'John Smith'),(2,'Jane Doe')
查询:
;WITH latestDeals
AS
(
SELECT
ROW_NUMBER() OVER(PARTITION BY ClientNo ORDER BY DealDate DESC) AS RowNbr,
Deal.*
FROM
@Deal AS Deal
)
SELECT
client.ClientNo,
client.ContactName,
latestDeals.Currency,
latestDeals.DealDate
FROM
@Client AS client
JOIN latestDeals
ON client.ClientNo=latestDeals.ClientNo
AND latestDeals.RowNbr=1
更新:
如果要使用常规查询。你可以这样做:
SELECT
client.ClientNo,
client.ContactName,
Latestdeal.maxDealDate as DealDate,
deal.Currency
FROM
@Client AS client
JOIN
(
SELECT
MAX(Deal.DealDate) AS maxDealDate,
Deal.ClientNo
FROM
@Deal AS Deal
GROUP BY
Deal.ClientNo
) AS Latestdeal
ON client.ClientNo=Latestdeal.ClientNo
JOIN @Deal as deal
ON client.ClientNo=deal.ClientNo
AND deal.DealDate=Latestdeal.maxDealDate
这将导致相同的输出
结果:
1 John Smith GBP 2014-11-12 00:00:00.000
2 Jane Doe USD 2014-12-23 00:00:00.000
试试这个,
测试数据:
CREATE TABLE #YourTable
(
CLIENT_NO INT,
CONTACT_NAME VARCHAR(20),
CURRENCY VARCHAR(10),
[DEAL_DATE] DATE
)
INSERT INTO #YourTable VALUES
(1,'John Smith','EUR','2014-10-07'),
(1,'John Smith','GBP','2014-11-12'),
(2,'Jane Doe','GBP','2014-09-17'),
(2,'Jane Doe','USD','2014-12-23'),
(1,'John Smith','USD','2013-11-13'),
(2,'Jane Doe','EUR','2012-09-06')
查询:
SELECT CLIENT_NO,CONTACT_NAME,CURRENCY,[DEAL_DATE]
FROM (SELECT *,
Row_Number()
OVER (
PARTITION BY CLIENT_NO
ORDER BY [DEAL_DATE] DESC) AS RN
FROM #YourTable)A
WHERE RN = 1