# Toad, Teradata, SQL, CASE Staements

**URL:** <https://forums.toadworld.com/t/toad-teradata-sql-case-staements/7497>\
**Category:** Toad Data Point & Toad Intelligence Central\
**Created:** [January 24, 2013, 8:10am UTC](https://forums.toadworld.com/t/toad-teradata-sql-case-staements/7497 "2013-01-24T08:10:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpoling\_953](https://avatars.discourse-cdn.com/v4/letter/w/f1d935/32.png) [@wpoling\_953](https://forums.toadworld.com/u/wpoling_953)\
**Post date:** [January 24, 2013, 8:10am UTC](https://forums.toadworld.com/t/toad-teradata-sql-case-staements/7497/1 "2013-01-24T08:10:18Z")

</div>

Hi, is there a way to add a second or third CASE clause in a script like this below?

I have dichotomous outcome currently and need a third outcome that makes a field in certain records blank,

like CASE1 when “somthing” then 0, CASE2 when “something else” then ‘blank’ ELSE 1

specifically the second case would be

CASE WHEN SvcPlace\_id = 37 and Stay\_id = -2 THEN ‘’ ELSE 1

I can do an update after the fact but seems like this would be more elegant?

Thanks for any thoughts!

WHP

Current script:

CREATE TABLE ar\_asrh\_dm\_etl\_beacon.Flg\_OV\_Algo1a3 AS  
(SELECT Claim\_id, Mem\_id, Dte\_SvcFrom\_id AS “DOS or Admit”, Dte\_Disch\_id AS discharge,

MIN (Dte\_SvcFrom\_id) over (partition BY Mem\_id ORDER BY Mem\_id, Dte\_SvcFrom\_id  
ROWS between 1 following and 1 following) AS “Office Visit Date”,

```
MIN (Dte_SvcFrom_id) over (partition BY Mem_id ORDER BY Mem_id, Dte_SvcFrom_id
ROWS between 1 following and 1 following) - Dte_Disch_id AS "Day Diff",

```

/\*I’m guessing I could obtain the next row’s SvcPlace\_id as below. \*/

MIN (SvcPlace\_id) over (partition BY Mem\_id ORDER BY Mem\_id, Dte\_SvcFrom\_id  
ROWS between 1 following and 1 following) /_- Dte\_Disch\_id_/ AS “NextSvcPlace\_id”,

/\*With that available, the CASE statement can be altered as below. \*/

CASE WHEN SvcPlace\_id = 36 and Stay\_id \<\> -2 and “Day Diff” BETWEEN 0 and 15  
and “NextSvcPlace\_id” = 37  
THEN 0 ELSE 1

END AS “14DyOfficeVisitFLG”, Dte\_SvcFrom\_id, Dte\_Admit\_id, Dte\_Disch\_id,  
Stay\_id, SvcPlace\_id, ClaimCategory\_id, Dx\_Cd, Dx\_Dl, PatStat\_dl, PatStat\_id,  
ProvRend\_LastName, ProvRend\_Id, MSDRG\_id, ProvSpc\_id, ProvSpc\_ds

FROM ar\_asrh\_dm\_etl\_beacon.Flg\_OV\_Algo1a2)

WITH DATA;

---

<div class="post-metadata">

**Author:** ![wpoling\_953](https://avatars.discourse-cdn.com/v4/letter/w/f1d935/32.png) [@wpoling\_953](https://forums.toadworld.com/u/wpoling_953)\
**Post date:** [January 24, 2013, 11:12am UTC](https://forums.toadworld.com/t/toad-teradata-sql-case-staements/7497/2 "2013-01-24T11:12:43Z")

</div>

Hi I got it, thanks.

CREATE TABLE ar\_asrh\_dm\_etl\_beacon.Flg\_OV\_Algo1a3 AS  
(SELECT Claim\_id, Mem\_id, Dte\_SvcFrom\_id AS “DOS or Admit”, Dte\_Disch\_id AS discharge,

MIN (Dte\_SvcFrom\_id) over (partition BY Mem\_id ORDER BY Mem\_id, Dte\_SvcFrom\_id  
ROWS between 1 following and 1 following) AS “Office Visit Date”,

```
MIN (Dte_SvcFrom_id) over (partition BY Mem_id ORDER BY Mem_id, Dte_SvcFrom_id
ROWS between 1 following and 1 following) - Dte_Disch_id AS "Day Diff",

```

MIN (SvcPlace\_id) over (partition BY Mem\_id ORDER BY Mem\_id, Dte\_SvcFrom\_id  
ROWS between 1 following and 1 following) /_- Dte\_Disch\_id_/ AS “NextSvcPlace\_id”,

CASE  
WHEN SvcPlace\_id = 36 AND Stay\_id \<\> -2 AND “Day Diff” BETWEEN 0 and 15 AND “NextSvcPlace\_id” = 37  
THEN 0  
WHEN PatStat\_id IN (3, 8, 11, 12) and SvcPlace\_id = 36  
THEN 0  
WHEN SvcPlace\_id = 36 AND Stay\_id \<\> -2 AND “NextSvcPlace\_id” = 36  
THEN NULL  
WHEN SvcPlace\_id = 37 and Stay\_id = -2  
THEN NULL  
WHEN “Office Visit Date” BETWEEN Dte\_Admit\_id AND Dte\_Disch\_id  
THEN NULL  
WHEN “Day Diff” \< 0  
THEN NULL  
ELSE 1  
END AS “14DyOfficeVisitFLG”, Dte\_SvcFrom\_id, Dte\_Admit\_id, Dte\_Disch\_id,  
Stay\_id, SvcPlace\_id, ClaimCategory\_id, Dx\_Cd, Dx\_Dl, PatStat\_dl, PatStat\_id,  
ProvRend\_LastName, ProvRend\_Id, MSDRG\_id, ProvSpc\_id, ProvSpc\_ds

FROM ar\_asrh\_dm\_etl\_beacon.Flg\_OV\_Algo1a2)

WITH DATA;
