2014-03-26 150 views
0
DimAward 
-------- 
AwardKey (PK) 
TypeCode 
SourceCode 
SourceKey 

CREATE TABLE [Final].[DimAward](
    [AwardKey] [int] IDENTITY(1,1) NOT NULL, 
    [SourceKey] [varchar](10) NOT NULL, 
    [SourceCode] [varchar](10) NOT NULL, 
    [TypeCode] [varchar](10) NOT NULL, 
CONSTRAINT [pk_DimAward] PRIMARY KEY NONCLUSTERED 
(
    [AwardKey] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY], 
CONSTRAINT [uq_DimAward_SourceKey] UNIQUE CLUSTERED 
(
    [SourceKey] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
ON [PRIMARY] 
) ON [PRIMARY] 

GO 

FactAwardDetail 
---------------- 
AwardKey (FK to DimAward.AwardKey) 
AidYearkey 
StudentKey 
TermKey 
AwardLevelKey 

CREATE TABLE [Final].[FactAwardDetail](
    [AwardDetailKey] [int] IDENTITY(1,1) NOT NULL, 
    [AwardKey] [int] NOT NULL, 
    [AidYearKey] [int] NOT NULL, 
    [StudentKey] [int] NOT NULL, 
    [TermKey] [int] NOT NULL, 
    [AwardLevelKey] [int] NOT NULL, 
CONSTRAINT [pk_FactAwardDetail] PRIMARY KEY NONCLUSTERED 
(
    [AwardDetailKey] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY], 
CONSTRAINT [uq_FactAwardDetail_CompoundID] UNIQUE NONCLUSTERED 
(
    [StudentKey] ASC, 
    [AidYearKey] ASC, 
    [TermKey] ASC, 
    [AwardKey] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

FactAwardDetail對於給定的StudentKey/AidYearkey/Termkey多個記錄(一個學生可以在一個給定的aidyear /學期以上的獎勵) - 可能需要按StudentKey ,AidYearkey,TermKey。更新與SQL GROUP表BY CASE語句

FactAward Sample data DimAward Sample data

我需要更新基於以下邏輯FactAwardDetail.AwardKey FactAwardDetail.AwardLevelKey。

如果給定StudentKey/AidYearKey/TermKey組合的任何記錄滿足以下:

SELECT 1 
From DimAward a INNER JOIN 
FactAwardDetail ad on a.AwardKey = ad.AwardKey 
Where a.SourceKey = 'powell' 
group by ad.StudentKey, ad.AidYearKey, ad.TermKey 

然後

set ad.AwardLevelkey = ad.awardkey 
set ad.AwardLevelKey for remaining records for the same StudentKey/AidYearkey/Termkey combination to -1 

如果沒有記錄滿足上述條件,則尋找新的條件如下: 如果給定StudentKey/AidYearkey/Termkey組合的任何記錄滿足以下條件:

SELECT 1 
From DimAward a INNER JOIN 
FactAwardDetail ad on a.AwardKey = ad.AwardKey 
Where a.SourceKey = 'regt' 
group by ad.StudentKey, ad.AidYearKey, ad.TermKey 

然後

set ad.AwardLevelkey = ad.awardkey 
set rest of ad.AwardLevelKey for remaining records for the same StudentKey/AidYearkey/Termkey combination to -1 

如果沒有記錄滿足上述兩個條件,則尋找新的條件如下:

如果給定StudentKey/AidYearkey/Termkey組合的任何記錄滿足以下:

SELECT 1 
From DimAward a INNER JOIN 
FactAwardDetail ad on a.AwardKey = ad.AwardKey 
Where a.SourceKey = 'presm' 
group by ad.StudentKey, ad.AidYearKey, ad.TermKey 

然後 組ad.AwardLevelkey = ad.awardkey ad.AwardLevelKey的剩餘記錄FO 組其餘如果沒有記錄滿足上述3個條件,我將需要再次重複該模式幾次。

我將不勝感激編寫t-sql語句的任何幫助。

+0

創建一個sqlfiddle或post創建帶有示例行的表語句。 –

+0

你需要在這裏使用「UPDATE FROM」和「IF ELSE」。 –

+0

我已經添加了create table語句以及示例行。 – Raj

回答

1

你不能在if-then-else之類的結構中出現。 SQL是爲集合操作而設計的,因此可以這樣使用它。首先,你需要有SourceKeys的有序列表:

declare @SourceKeyPriority table 
(
    Priority int, SourceKey varchar(10) 
) 
insert into @SourceKeyPriority values (1,'powell') 
insert into @SourceKeyPriority values (2,'regt') 
insert into @SourceKeyPriority values (3,'presm') 

它並不需要是表變量,只是無論你的作品。然後將所有記錄更新爲-1我們隨後將更新那些具有匹配獎勵的行。然後,您需要找到適合給定ad.StudentKey,ad.AidYearKey,ad.TermKey組合的最佳(最高優先級)。將其封裝在CTE中,並用它來更新您的表格:

with cte as 
(
    select 
    ad.StudentKey, 
    ad.AidYearKey, 
    ad.TermKey, 
    min(x.Priority) as Priority 
    from 
    @SourceKeyPriority x 
    inner join DimAward a 
     on a.SourceKey = x.SourceKey 
    inner join FactAwardDetail ad 
     on a.AwardKey = ad.AwardKey 
    group by 
    ad.StudentKey, ad.AidYearKey, ad.TermKey 
) 
update ad 
set AwardLevelKey = ad.AwardKey 
from FactAwardDetail ad 
inner join cte 
    on cte.StudentKey = ad.StudentKey 
    and cte.AidYearKey = ad.AidYearKey 
    and cte.TermKey = ad.TermKey 
inner join @SourceKeyPriority x 
    on x.Priority = cte.priority 
inner join DimAward a 
    on a.SourceKey = x.SourceKey 
    and a.AwardKey = ad.AwardKey 
+0

謝謝。這正是我所期待的。 – Raj