[英]How to Select the most recent items by dates from another table
I have two tables Projects and Plans, and I need get recent projects from the last plan by create date: 我有两个表“项目”和“计划”,我需要按创建日期从上一个计划中获取最近的项目:
Projects:
ProjectId, PlanId, StartDate, EndDate
(guid) (guid) (datetime) (datetime)
-------------------------------------
00001, 00001, 1/1/2015 31/1/2015
00001, 00002, 3/2/2015 15/2/2015
00002, 00001, 1/2/2015 20/2/2015
00002, 00002, 1/2/2015 21/2/2015
00003, 00001, 1/3/2015 10/3/2015
Plans:
PlanId, CreateDate
(guid) (datetime)
--------------------
00001, 1/1/2015
00002, 5/2/2015
I wrote query that take single project from the last plan, but i can't write query to get many projects by single query. 我写了从上一个计划中提取单个项目的查询,但是我无法通过单个查询编写查询来获取多个项目。
Here my query: 这是我的查询:
SELECT TOP 1 pr.ProjectId,
pl.CreateDate,
pr.StartDate,
pr.EndDate
FROM Projects pr
INNER JOIN Plans pl
ON pr.PlanId = pl.PlanId
WHERE ProjectId = '000002'
ORDER BY pl.CreateDate DESC
Desired result is (all projects from the last plans): 所需的结果是(来自上一个计划的所有项目):
ProjectId, PlanId, StartDate, EndDate
--------------------------------------
00001, 00002, 3/2/2015, 15/2/2015
00002, 00002, 1/2/2015, 21/2/2015
00003, 00001, 1/3/2015, 10/3/2015
UPDATE: Gordon Linoff gave the good answer, but it wasn't solved my question, because both his queries don't take '00003' project (its last plan is '00001'). 更新:戈登·利诺夫(Gordon Linoff)给出了很好的答案,但这并没有解决我的问题,因为他的两个查询都没有采用“ 00003”项目(其最后一个计划是“ 00001”)。
I wrote my query with ' OVER Clause ' (Stanislovas Kalašnikovas note about it). 我用“ OVER Clause ”(StanislovasKalašnikovas的注释)写了我的查询。
So I post full answer that solves my question for future googlers: 因此,我发布了完整的答案,为以后的Google员工解决了我的问题:
SELECT * FROM
(SELECT
result.ProjectId,
result.CreateDate,
result.StartDate,
result.EndDate,
ROW_NUMBER() OVER (PARTITION BY ProjectId ORDER BY CreateDate DESC) AS RowNumber
FROM (
SELECT pr.ProjectId AS ProjectId,
pl.CreateDate AS CreateDate,
pr.StartDate AS StartDate,
pr.EndDate AS EndDate
FROM Projects pr
INNER JOIN Plans pl ON pr.PlanId = pl.PlanId
--WHERE ProjectId IN ('000001', '000003') --Filter
) AS result
) AS result
WHERE result.RowNumber = 1
You can use a subquery to get the most recent plan. 您可以使用子查询来获取最新计划。 Then just join this to projects:
然后将其加入项目:
SELECT pr.ProjectId, pl.CreateDate, pr.StartDate, pr.EndDate
FROM (SELECT TOP 1 pl.*
FROM plans pl
ORDER BY pl.CreateDate DESC
) pl JOIN
Projects pr
ON pr.PlanId = pl.PlanId;
WHERE ProjectId = '000002'
An alternative method is to just use TOP WITH TIES
: 另一种方法是只使用
TOP WITH TIES
:
SELECT TOP 1 WITH TIES pr.ProjectId, pl.CreateDate, pr.StartDate, pr.EndDate
FROM plans pl
Projects pr
ON pr.PlanId = pl.PlanId;
WHERE ProjectId = '000002'
ORDER BY pl.CreateDate DESC
This is example of ROW_NUMBER
with 1 table, easy you can use It in your case. 这是带有1个表的
ROW_NUMBER
示例,很容易在您的情况下使用它。
CREATE TABLE #Test
(
Id NVARCHAR(100),
Data DATE
)
INSERT INTO #Test VALUES ('1', '2015-01-04'), ('1', '2015-01-07'), ('2', '2015-01-05'), ('2', '2015-01-08')
SELECT Id, Data
FROM (
SELECT Id, Data, ROW_NUMBER() OVER (PARTITION BY Id ORDER BY Data DESC) rn
FROM #Test
)x
WHERE rn > 1
DROP TABLE #Test
you can use datediff() eg if you want to take the entries of last 10 days use: 您可以使用datediff()例如,如果您想获取最近10天的条目,请使用:
SELECT pr.ProjectId,
pl.CreateDate,
pr.StartDate,
pr.EndDate
FROM Projects pr
INNER JOIN Plans pl
ON pr.PlanId = pl.PlanId
WHERE datediff(day,pl.CreateDate,getdate())<10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.