2015-12-22 78 views
0

我有一個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 statementservice 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 
+0

這是一個很好的開始。 http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ –

+0

無法想象你想要什麼...你可以添加示例數據,當前結果和期望的結果? –

+0

我添加了示例數據。希望能幫助到你。 – NihS

回答

0

變化需要:

1上月表LEFT JOIN當月

2在您需要檢索previousmonth.id select語句,previousmonth.date_key

select 
    PreviousMonth.ID, 
    PreviousMonth.DateKey, 
    Current_month.ID, 
    Current_month.DateKey 
    , 
    CASE WHEN Current_month.Status = PreviousMonth.Status THEN 0 
    WHEN Current_month.Status <> PreviousMonth.Status AND Current_month.Status = 'A' AND PreviousMonth.Status = 'N' then 115 
    WHEN Current_month.Status <> PreviousMonth.Status AND Current_month.Status = 'N' AND PreviousMonth.Status = 'A' then 150 
    END [Service_Action_type] 

    from 
    (
    select '2653467' ID, 'jan' Datekey ,''Service_Action_type,'A'Status 
    union 
    select '2523379' ID,'Feb' Datekey ,''Service_Action_type,'B' 
    union 
    select '2523379' ID,'march' Datekey ,''Service_Action_type,'N' 
    ) PreviousMonth 
    Left Join 
    (
    select '2523379' ID,'Apr' Datekey ,''Service_Action_type,'N' Status 
    union 
    select '2653467' ID,'Apr' Datekey ,''Service_Action_type,'N' Status 
    ) Current_month 

    on Current_month.ID = PreviousMonth.ID 

希望這工作!

0

我找到了解決方案。我只需將case statement分成三個不同的case statements,然後UNPIVOT那三個三列不同的case statements

感謝大家的時間和幫助。

+0

您應該將此標記爲解決問題的答案。 –