如何根據生效日期獲取當前記錄?我應該使用子查詢嗎? MAX有什麼我可以使用的嗎?如何根據生效日期獲取當前記錄?
我有這些表格的例子。
ResourceID is the ID number of the Resource.
OrganizationId is the current Organization or Department of the Resource.
Effective Date is the start date or the first day of the Resource in an Organization.
ResourceID OrganizationID EffectiveDate
VC1976 INTIN1HTHWYAMM 2009-12-23 00:00:00.000
VC1976 INTIN1LGAMMAMS 2011-07-01 00:00:00.000
VC1976 SMESM1HTOVEOVE 2012-07-01 00:00:00.000
VC1976 APCAP1HTOVEOVE 2012-07-09 10:17:56.000
ResourceID OrganizationID EffectiveDate
JV2579 VNMVN1HTHWYCMM 2009-07-01 00:00:00.000
JV2579 INTIN1HTHWYCMM 2011-07-02 00:00:00.000
JV2579 SMESM1HTOVEOVE 2012-07-01 00:00:00.000
ResourceID OrganizationID EffectiveDate
RJ1939 INTIN1HTOVEOVE 1995-01-30 00:00:00.000
RJ1939 INTIN1HTOVEOVE 2007-07-25 00:00:00.000
RJ1939 SMESM1HTOVEOVE 2012-07-01 00:00:00.000
ResourceID OrganizationID EffectiveDate
PJ8828 AREAR1HTHWYRHD 2012-04-01 00:00:00.000
PJ8828 SMESM1HTOVEOVE 2012-07-01 00:00:00.000
ResourceID OrganizationID EffectiveDate
RS1220 INTIN1HTHWYCMM 1981-01-06 00:00:00.000
RS1220 SMESM1HTOVEOVE 2012-07-01 00:00:00.000
我的目標是讓所有誰目前屬於該用戶將在OrganizationUnit工時單的資源的。例如,如果用戶將SMESM1HTOVEOVE在OrganizationID參數則使出渾身ReourceID那是目前在SMESM1HTOVEOVE。到目前爲止我的MAX查詢不起作用。
select OrganizationID, ResourceID, MAX(EffectiveDate) as EffectiveDate from ResourceOrganization
where OrganizationID = 'SMESM1HTOVEOVE'
group by OrganizationID, ResourceID, EffectiveDate
以下是我上面簡短的MAX查詢的結果。這是錯誤的,因爲ResourceID VC1976目前屬於APCAP1HTOVEOVE,2012-07-09 10:17:56.000生效。
OrganizationID ResourceID EffectiveDate
SMESM1HTOVEOVE JV2579 2012-07-01 00:00:00.000
SMESM1HTOVEOVE PJ8828 2012-07-01 00:00:00.000
SMESM1HTOVEOVE RJ1939 2012-07-01 00:00:00.000
SMESM1HTOVEOVE RS1220 2012-07-01 00:00:00.000
SMESM1HTOVEOVE VC1976 2012-07-01 00:00:00.000
有人可以幫忙提供他們的意見嗎?因爲我將在下面使用這個存儲過程。我還會包括我的proc,供你自己細讀。
謝謝!
create table #Resources
(
ResourceID nvarchar(30),
OrganizationID nvarchar(15),
EffectiveDate datetime,
TimeEntryDate datetime
)
if @ResourceID <> ''
begin
insert into #Resources (ResourceID,OrganizationID,EffectiveDate)
select ro.ResourceID, ro.OrganizationID, ro.EffectiveDate from ResourceOrganization ro,
(select ResourceID, MAX(EffectiveDate) as maxEffectivedate from dbo.ResourceOrganization
where ResourceID = @ResourceID
group by ResourceID) as maxresults
where ro.ResourceID = maxresults.ResourceID
and ro.EffectiveDate = maxresults.maxEffectivedate
end
else if @OrgUnit <> ''
begin
insert into #Resources (ResourceID,OrganizationID,EffectiveDate)
select ro.ResourceID, ro.OrganizationID, ro.EffectiveDate from ResourceOrganization ro,
(select ResourceID, MAX(EffectiveDate) as maxEffectivedate from dbo.ResourceOrganization
where OrganizationID like '' + @OrgUnit + '%'
group by ResourceID) as maxresults
where ro.ResourceID = maxresults.ResourceID
and ro.EffectiveDate = maxresults.maxEffectivedate
else if @ResourceID <> '' and @OrgUnit <> ''
begin
insert into #Resources (ResourceID,OrganizationID,EffectiveDate)
select ro.ResourceID, ro.OrganizationID, ro.EffectiveDate
from ResourceOrganization ro,
(select ResourceID, MAX(EffectiveDate) as maxEffectivedate from dbo.ResourceOrganization
where ResourceID = @ResourceID
group by ResourceID) as maxresults
where ro.ResourceID = maxresults.ResourceID
and ro.EffectiveDate = maxresults.maxEffectivedate
end
您的表格中是否有任何唯一鍵?這將使它更容易。 – Jade
嗨玉。不,它沒有任何獨特的表格。 –