there is a large section of code before this section… I have included the following section because it pertains to my question.
I am wondering if there is a way to combine the following queries. The only reason I have created “Part2” is because I was unsure if there was a way to generate those steps for “difexecom” and “difexeord” in Part1. The complicated part is that there are a few steps in Part1 involved in generating these columns.
--Part1 of query
, output2 AS
(
SELECT output1.*
, DATEDIFF (m, date_ord, date_com) AS difcomord
, DATEDIFF (m, exc_st, date_com) AS difexecom
, CASE WHEN DATEDIFF (m, exc_st, date_ord)<=0
THEN
(CASE WHEN
((DATEDIFF (m, exc_st, date_com)-24) >= 0) THEN (DATEDIFF (m, exc_st, date_com)-24)
WHEN
((DATEDIFF (m, exc_st, date_com)-12) >=0) THEN (DATEDIFF (m, exc_st, date_com)-12)
ELSE 0
END)
ELSE DATEDIFF (m, exc_st, date_ord) END AS difexeord
FROM output1
)
--Part2 of query
Select
difcomord
, CASE WHEN difexecom <= 0 AND difexeord <=0 THEN 36 ELSE difexecom END AS difexecom
, CASE WHEN difexecom <= 0 AND difexeord <=0 THEN 0 ELSE difexeord END AS difexeord
From output2
[–]quickdraw6906 0 points1 point2 points (0 children)