我有一個sql
查詢比較當前月份和上個月值。 下面是我的查詢比較並插入列
DECLARE @CurrentDate VARCHAR(100)
DECLARE @Year VARCHAR(100)
DECLARE @Month VARCHAR(100)
SET @CurrentDate = (SELECT MAX(Date_Key) AS CurrentMonth FROM LoanFact)
SET @Year = (SELECT CASE WHEN SUBSTRING(@CurrentDate,5,2) = 1 THEN (SUBSTRING(@CurrentDate,1,4) - 1) ELSE SUBSTRING(@CurrentDate,1,4) END)
SET @Month = (SELECT CASE WHEN SUBSTRING(@CurrentDate,5,2) = 1 THEN '12' ELSE (SUBSTRING(@CurrentDate,5,2) - 1) END)
;WITH cte AS (
SELECT DISTINCT
CurrentMonth.ID,
CurrentMonth.Date_Key,
--CurrentMonth.Status AS CurrentMonth_Status,
--previousMonth.Status AS PreviousMonth_Status,
--CurrentMonth.System_Key AS CurrentMonth_System_Key,
--previousMonth.System_Key AS previousMonth_System_Key,
CASE WHEN CurrentMonth.Status = PreviousMonth.Status THEN 0
WHEN CurrentMonth.Status <> PreviousMonth.Status AND CurrentMonth.Status = 'A' AND PreviousMonth.Status = 'N' then 115
WHEN CurrentMonth.Status <> PreviousMonth.Status AND CurrentMonth.Status = 'N' AND PreviousMonth.Status = 'A' then 150
WHEN CurrentMonth.System_Key = previousMonth.System_Key THEN 0
WHEN CurrentMonth.System_Key <> previousMonth.System_Key AND CurrentMonth.System_Key <=10 AND previousMonth.System_Key >=10 THEN 304
WHEN CurrentMonth.System_Key <> previousMonth.System_Key AND CurrentMonth.System_Key >=10 AND previousMonth.System_Key <=10 THEN 303
WHEN ((ISNULL(SeedAndChemYesNo, 'No') = 'Yes')) THEN '559'
END [Service_Action_type]
FROM
(
SELECT
ld.ID,
ld.Status ,
SUBSTRING(lf.System_Key, 3, 2) AS System_Key,
lf.Date_Key
FROM
Fact lf
INNER JOIN Dimension ld ON lf.LKey = ld.LKey
LEFT JOIN CustomerFact ltcf ON lf.LKey = ltcf.LKey AND lf.Date_Key = ltcf.Date_Key
LEFT JOIN OrganizationDimension od ON od.Organization_Key = ltcf.Organization_Key
WHERE
SUBSTRING(CAST(lf.Date_Key AS VARCHAR(100)),1,6) = SUBSTRING(@CurrentDate,1,6)
) CurrentMonth
INNER JOIN
(
SELECT
ld.ID,
ld.Status ,
SUBSTRING(lf.System_Key, 3, 2) AS System_Key,
lf.Date_Key
FROM
Fact lf
INNER JOIN Dimension ld ON lf.L_Key = ld.L_Key
LEFT JOIN CustomerFact ltcf ON lf.L_Key = ltcf.L_Key AND lf.Date_Key = ltcf.Date_Key
LEFT JOIN OrganizationDimension od ON od.Organization_Key = ltcf.Organization_Key
WHERE
SUBSTRING(CAST(lf.Date_Key AS VARCHAR(100)),1,4) = @Year
AND CASE WHEN SUBSTRING(CAST(lf.Date_Key AS VARCHAR(100)),5,1) = 0
THEN REPLACE(SUBSTRING(CAST(lf.Date_Key AS VARCHAR(100)),5,2), '0','')
ELSE SUBSTRING(CAST(lf.Date_Key AS VARCHAR(100)),5,2) END = @Month) PreviousMonth
ON CurrentMonth.LoanNumber = PreviousMonth.LoanNumber
LEFT JOIN [SeedAndChem] Issac ON PreviousMonth.ID = Issac.ID
眼下因爲case statement
的service action type
被更新,如果一個條件得到滿足,並檢查其他條件。
我希望能夠根據所有條件更新service action type
,如果滿足多個條件,我希望通過創建重複id來更新代碼列(如有必要)。
下面是我現在正在獲取的示例數據。
樣本數據:
ID Service_Action_type
2653467 150
2523379 150
但我希望數據能像下面如果超過一個條件滿足。
ID Service_Action_type
2653467 150
2653467 303
2653467 559
2523379 150
2523379 304
2523379 559
這是一個很好的開始。 http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ –
無法想象你想要什麼...你可以添加示例數據,當前結果和期望的結果? –
我添加了示例數據。希望能幫助到你。 – NihS