我有一个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 Service_Action_type
2653467 150
2523379 150
,如果满足多个条件,我希望代码列通过在必要时创建重复ID来更新为这些值。
以下是我现在收到的示例数据。
示例数据:
ID Service_Action_type
2653467 150
2653467 303
2653467 559
2523379 150
2523379 304
2523379 559
但如果有多个条件满足,我希望数据如下所示。
<div id="main">
<div class="width-container">
<div id="training-posts-about" class="about-widget-container">
<div class="training-posts-about-img">
<img src="images/demo/trainer-2.jpg" alt="pro-trainer">
</div>
<div class="about-training-slider-wrapper">
<div class="flexslider about-trainers-slider fiziko-slider-pro">
<ul class="slides">
<li>
<div class="about-slider-container">
<h2 class="home-widget">We believe in...</h2>
<p class="home-widget-caption">Cras libero ante, tincidunt nec ornare vitae, dignissim vel neque. </p>
<p class="home-widget-description">Suspendisse egestas semper t. </p>
<a class="ls-sc-button default">Learn More</a>
</div>
</li>
<li>
<div class="about-slider-container">
<h2 class="home-widget">A message from our staff</h2>
<p class="home-widget-caption">Cras libero ante, tincidunt nec ornare vitae, dignissim vel neque. </p>
<p class="home-widget-description"> id vestibulum massa nunc nec enim. Cras. </p>
<a class="ls-sc-button default">Learn More</a>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="clearfix"></div>
</div><!-- close .width-container -->
<div class="clearfix"></div>
</div><!-- close #main -->
答案 0 :(得分:0)
需要进行更改:
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
希望这有效!
答案 1 :(得分:0)
我找到了解决方案。我只需要将case statement
分成三个不同的case statements
,然后UNPIVOT
来自三个不同case statements
的三个列。
感谢大家的时间和帮助。