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語句
我需要更新基於以下邏輯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語句的任何幫助。
創建一個sqlfiddle或post創建帶有示例行的表語句。 –
你需要在這裏使用「UPDATE FROM」和「IF ELSE」。 –
我已經添加了create table語句以及示例行。 – Raj