我正在使用Ms access 2010.我的数据如下:
PROJECT(主要是单词),其中有很多:
请求(大多数数字大约在1到20之间,不是按顺序排列,有些注册为“1和2”作为示例),每个请求都有很多:
谈判(日期)或更确切地说,“谈判日期结束”,它们不按时间顺序排列。
我需要的是将数据保存在原始订单中,但条目不是其协商中的最新日期。
我有什么:
List<Parent>
我需要什么:
id | PROJECT NAME| REQUEST| DATE
1 ----- bla ------- 3 ----- 1-2-17
2 ----- bla ------- 3 ----- 4-2-17
3 ----- bla ------- 3 ----- 2-2-17
4 ----- bla ------- 5 ----- 4-2-17
5 ----- bla ------- 5 ----- 6-2-17
6 ----- ble ------- 1 ----- 6-2-17
7 ----- ble ------- 1 ----- 9-2-17
8 ----- ble ------- 3 ----- 5-3-17
9 ----- ble ------- 3 ----- 6-3-17
10 ---- ble ------- 3 ----- 5-3-17
我不知道从哪里开始。
答案 0 :(得分:0)
declare @example as table (
ExampleID int identity(1,1) not null primary key clustered
, ProjectName nvarchar(3) not null
, Request int not null
, Date_ date not null
);
insert into @example (ProjectName, Request, Date_)
select 'bla', 3, eomonth(sysdatetime(), -7) union all
select 'bla', 3, eomonth(sysdatetime(), -3) union all
select 'bla', 3, eomonth(sysdatetime(), -4) union all
select 'bla', 5, eomonth(sysdatetime(), -7) union all
select 'bla', 5, eomonth(sysdatetime(), -6) union all
select 'ble', 1, eomonth(sysdatetime(), -6) union all
select 'ble', 1, eomonth(sysdatetime(), -5) union all
select 'ble', 3, eomonth(sysdatetime(), -2) union all
select 'ble', 3, eomonth(sysdatetime(), -1) union all
select 'ble', 3, eomonth(sysdatetime(), -2);
;with cte as (
select ProjectName
, Request
, max(date_) over(partition by projectname, request order by projectName, Request ) Date_
from @example
)
select distinct ExampleID
, a.ProjectName
, a.Request
, a.Date_
from @example a
join cte b
on a.Date_ = b.Date_
and a.ProjectName = b.ProjectName
and a.Request = b.Request
您的逻辑或数据存在根本问题。
在你的例子中:
id | PROJECT NAME| REQUEST| DATE
1 ----- bla ------- 1 ----- 1-2-17
2 ----- bla ------- 2 ----- 4-2-17
3 ----- bla ------- 3 ----- 2-2-17
该解决方案将解析ID,该ID假设您的代理键是按时间顺序,与日期相对,并且根据项目名称排除日期小于前一记录的上一个日期的位置。 (按时间顺序并不意味着它是连续的)
当你有类似的东西时,逻辑会崩溃:
id | PROJECT NAME| REQUEST| DATE
1 ----- bla ------- 1 ----- 1-2-17
2 ----- bla ------- 2 ----- 4-2-17
3 ----- bla ------- 3 ----- 2-2-17
4 ----- bla ------- 6 ----- 6-2-17
现在您应该期待以下内容:
id | PROJECT NAME| REQUEST| DATE
4 ----- bla -------- 6 ---- 6-2-17
这是否是可接受的结果集?
答案 1 :(得分:0)
考虑使用聚合查询加入单元级别查询,返回等于 MaxDate 的行:
SELECT n.ID, p.[PROJECT NAME], r.[REQUEST], n.[DATE]
FROM ((PROJECTS p
INNER JOIN REQUESTS r ON p.ProjectID = r.ProjectID)
INNER JOIN NEGOTIATIONS n ON r.RequestID = n.RequestID)
INNER JOIN
(SELECT p.[PROJECT NAME], r.[REQUEST], MAX(n.[DATE]) AS MAX_DATE
FROM ((PROJECTS p
INNER JOIN REQUESTS r ON p.ProjectID = r.ProjectID)
INNER JOIN NEGOTIATIONS n ON r.RequestID = n.RequestID)
GROUP BY p.[PROJECT NAME], r.[REQUEST]
) AA aggdf
ON p.[PROJECT NAME] = aggdf.[PROJECT NAME]
AND r.[REQUEST] = aggdf.[REQUEST]
AND n.[DATE] = aggdf.MAX_DATE
ORDER BY n.ID, p.[PROJECT NAME], r.[REQUEST], n.[DATE]