How to execute multiple case when statements for each iteration in SAS? - sql

I'm using proc sql, and using multiple case when statements to add columns with either a 0 or 1 if the condition is met. It's a big bottleneck right now since it has to scan through each id for each case when statement. So I'm trying to figure out a way to somehow nest the case statements to perform each iteration, instead of having to iterate for all case statements.
This is an example of my code that is taking too long right now.
SELECT *,
CASE WHEN loannumber IN (
SELECT loannumber FROM PREPAY_LOAN_IDS
) THEN 1
ELSE 0 END AS PREPAY_FLAG,
CASE WHEN loannumber IN (
SELECT loannumber FROM DPD_30_IDS
) THEN 1
ELSE 0 END AS DPD_30_FLAG,
CASE WHEN loannumber IN (
SELECT loannumber FROM DPD_60_IDS
) THEN 1
ELSE 0 END AS DPD_60_FLAG,
CASE WHEN loannumber IN (
SELECT loannumber FROM DPD_90_IDS
) THEN 1
ELSE 0 END AS DPD_90_FLAG,
CASE WHEN loannumber IN (
SELECT loannumber FROM DPD_120_IDS
) THEN 1
ELSE 0 END AS DPD_120_FLAG,
CASE WHEN loannumber IN (
SELECT loannumber FROM FORECLOSURE_IDS
) THEN 1
ELSE 0 END AS FORECLOSURE_FLAG
FROM(
SELECT *
FROM MORTGAGES
)

The below query will work faster than the one you have posted as the input table is not completely access to retrieve the results. Try running this query and see how it performs.
SELECT M.*,
CASE WHEN PLI.loannumber IS NOT NULL THEN 1
ELSE 0 END AS PREPAY_FLAG,
CASE WHEN D3I.loannumber IS NOT NULL THEN 1
ELSE 0 END AS DPD_30_FLAG,
CASE WHEN D6I.loannumber IS NOT NULL THEN 1
ELSE 0 END AS DPD_60_FLAG,
CASE WHEN D9I.loannumber IS NOT NULL THEN 1
ELSE 0 END AS DPD_90_FLAG,
CASE WHEN D12I.loannumber IS NOT NULL THEN 1
ELSE 0 END AS DPD_120_FLAG,
CASE WHEN FCI.loannumber IS NOT NULL THEN 1
ELSE 0 END AS FORECLOSURE_FLAG
FROM MORTGAGES M
LEFT JOIN
PREPAY_LOAN_IDS PLI
ON M.loannumber = PLI.loannumber
LEFT JOIN
DPD_30_IDS D3I
ON M.loannumber = D3I.loannumber
LEFT JOIN
DPD_30_IDS D6I
ON M.loannumber = D6I.loannumber
LEFT JOIN
DPD_90_IDS D9I
ON M.loannumber = D9I.loannumber
LEFT JOIN
DPD_90_IDS D12I
ON M.loannumber = D12I.loannumber
LEFT JOIN
FORECLOSURE_IDS FCI
ON M.loannumber = FCI.loannumber
;

Since you're using SAS, here's a data step alternative, assuming that each of your datasets is already either sorted by or has an index on loannumber:
data want;
merge MORTGAGES(in = Mortgages)
PREPAY_LOAN_IDS(in = PLIDs keep = loannumber)
/*etc*/
;
by loannumber;
if Mortgages;
PREPAY_FLAG = PLIDs;
/*etc*/
run;
N.B. You will get duplicated records from MORTGAGES if you have duplicates in any of your other tables.

Related

Postgres: missing FROM-clause entry for table - SQL

good afternoon I have a query that I am not able to show the column of the table (dpa.view_workflowjob_client) column (f_name), when I place (c.f_name) do not select to view the error (missing entry from the FROM clause for the table). see below:
select
wfjc.f_agent_name,
wfjc.f_policy_name,
wfjc.f_workflow_name,
wfjc.f_workflow_jobid,
wfjc.f_status,
wfjc.f_completion_status,
wfjc.f_completion_report,
wfjc.f_starttime,
wfjc.f_endtime,
wfjc.f_missed_clients,
wfjc.f_disabled_clients,
c.f_name
from (
select *
from dpa.view_workflowjob wfj
left join
(
select
c.f_workflowjob_id,
case when count(c.dis) > 0
then count(c.dis)
else null
end f_disabled_clients,
case when count(c.msd) > 0
then count(c.msd)
else null
end f_missed_clients
from
(
select
f_name,
f_workflowjob_id,
case when f_status = 'disabled'
then 1
else null
end dis,
case when f_status = 'missed'
then 1
else null
end msd
from dpa.view_workflowjob_client
where f_starttime <= 1606146420 AND f_endtime >= 1605023220
) c
group by c.f_workflowjob_id
) clients
on clients.f_workflowjob_id = wfj.f_id
) wfjc
where (
(wfjc.f_starttime <= 1606146420 AND wfjc.f_endtime >= 1605023220 AND ( (wfjc.f_agent_id = '8512813a-5654-4b18-99cc-0e812076e719') ))
)

Converting a cursor/while loop into a set based approach

I am very new to SQL and I am trying to update a stored procedure that has a cursor in it. I had never seen a cursor prior to this one. The cursor's select statement has an inner join, but returns only a single column of IDs. The cursor calculates the number of deleted accounts for every ID, on a row by row basis.
At the end of the stored procedure, the number of deletion variables are inserted into a table
I was hoping someone that understands more about cursors/while loops would be able to suggest the best way to convert the code above into an efficient set based approach.
This is a set based way:
;WITH IDS AS
(
SELECT DISTINCT c.p_id
FROM dbo.deletion_h dh
INNER JOIN dbo.Child c
ON dh.C_id = c.c_id
WHERE CONVERT(CHAR(25),dh.delete_date,101) = #ReportDate
AND c.isT = 1
AND c.p_id NOT IN (SELECT p_id FROM dbo.Parent WHERE support = 'Y')
), Data AS
(
SELECT p_id,
COUNT(*) ActiveChild,
SUM(CASE WHEN isT = 1 AND [level] <> 'H' THEN 1 ELSE 0 END) activePk8,
SUM(CASE WHEN isT = 1 AND [level] = 'H' THEN 1 ELSE 0 END) activeHS
FROM dbo.child c
WHERE [login] <> 'f'
AND EXISTS( SELECT 1 FROM IDS
WHERE p_id = c.p_id)
GROUP BY p_id
)
SELECT SUM(CASE WHEN ActiveChild > 0 THEN 1 ELSE 0 END) NumParentDeletions,
SUM(CASE WHEN activechildPk8 > 0 THEN 1 ELSE 0 END) NumDeletionsPk8,
SUM(CASE WHEN activeHS > 0 THEN 1 ELSE 0 END) NumDeletionsHS
FROM Data
You can modify the last SELECT to make it insert those values into your table.

Select Records from First OR occurence within a multiple AND/OR T-SQL statement within a function

I have the following SQL (example):
SET #Return_Value = = (SELECT Top 1
(CASE WHEN .... THEN ColumValue1 ELSE ColumValue2 END)
FROM TableA WHERE (Lots of AND Statements)
AND
(
(bla1)
OR
(bla2)
OR
(bla3)
)
The bla1, etc are logic to retrieve colum values from TableA. How can I return the values from bla1 if they were found without executing bla2 or bla3 because those might overwrite what I'm looking for? In other words I only want to execute OR statements if the previous one didn't find data, all this within a function.
You can use a case expression as :
SET #Return_Value = (SELECT Top 1
(CASE WHEN .... THEN ColumValue1 ELSE ColumValue2 END)
FROM TableA WHERE (Lots of AND Statements)
AND
( 1 = case when condition1 then 1
case when condition2 then 1
case when condition3 then 1
end
);
you can use order by, like
select Top 1
CASE WHEN .... THEN ColumValue1 ELSE ColumValue2 END
FROM TableA
WHERE
(Lots of AND Statements) AND
(
(bla1) OR
(bla2) OR
(bla3)
)
order by
case
when (bla1) then 1
when (bla2) then 2
when (bla3) then 3
else 999
end
Or you can try to simplify it (but you have to check performance):
select Top 1
CASE WHEN .... THEN ColumValue1 ELSE ColumValue2 END
FROM TableA
outer apply (
select
case
when (bla1) then 1
when (bla2) then 2
when (bla3) then 3
end as T
) as C
WHERE
(Lots of AND Statements) and
C.T is not null
order by C.T
or, for example, you can use union, something like this:
with cte as (
select Top 1
CASE WHEN .... THEN ColumValue1 ELSE ColumValue2 END as data
FROM TableA
WHERE
(Lots of AND Statements)
), cte2 as (
select top 1 data, 1 as c from cte where (bla1)
union all
select top 1 data, 2 as c from cte where (bla2)
union all
select top 1 data, 3 as c from cte where (bla3)
)
select top 1 data
from cte2
order by c

Issues with counting records in secondary table based on complex criteria

I need to be able to count the number of records in a secondary table tblOptyRecordsHistorical which are related to the main table tblOptyRecordsCurrent.
The tables are exactly the same, the main contains the current 'daily snapshot', the secondary table contains previous daily snapshots.
I have a number of flags which use the following basic syntax:
(SELECT COUNT(OpportunityRecordID) AS Expr1
FROM dbo.tblOptyRecordsHistorical AS hist
WHERE (OpportunityGlobalCRMId = curr.OpportunityGlobalCRMId))
AS prevEntries,
This works fine. But one flag, I need to count the number of records in the historical table, but the logic is more complicated and depends on values from the main table:
SELECT OpportunityGlobalCRMId,
(SELECT SUM(CASE WHEN curr.PartnerGlobalCRMID IS NULL THEN CASE WHEN
hist.IgnoreOpportunity != 0 THEN 1 ELSE 0 END ELSE CASE
WHEN curr.CustomerAccountID IS NULL THEN CASE WHEN hist.IgnoreOpportunity = 1 AND
hist.PartnerGlobalCRMID = curr.PartnerGlobalCRMID THEN 1 ELSE 0 END ELSE CASE WHEN
hist.IgnoreOpportunity = 1 AND CONVERT(varchar, hist.CustomerAccountID) +
hist.PartnerGlobalCRMID = CONVERT(varchar, curr.CustomerAccountID) +
curr.PartnerGlobalCRMID AND hist.OpptyIncentiveCreatedDate =
curr.OpptyIncentiveCreatedDate THEN 1 ELSE 0 END END END) AS Expr1 FROM
dbo.tblOptyRecordsHistorical AS hist WHERE (OpportunityGlobalCRMId =
curr.OpportunityGlobalCRMId)) AS prevIgnored
FROM dbo.tblOptyRecordsCurrent AS curr
I've omitted the other flags and fields except for the initial OpportunityGlobalCRMID. This results in the following error: Multiple columns are specified in an aggregated expression containing an outer reference. If an expression is being aggregated contains an outer reference, then that outer reference must be the only column referenced in the expression.
SQL Server does not like mixing of inner (hist table) and outer (curr table) in a aggregate subquery expression. Some explanation is available here.
The proposed solutuon is to re-include the outer table in the sub-query, joining on it's key, in order to make all references inner. In your case, that would mean putting the tblOptyRecordsCurrent table inside the subquery, like this:
SELECT OpportunityGlobalCRMId,
(SELECT SUM(CASE
WHEN curr2.PartnerGlobalCRMID IS NULL
THEN CASE WHEN hist.IgnoreOpportunity != 0 THEN 1 ELSE 0 END
ELSE CASE
WHEN curr2.CustomerAccountID IS NULL
THEN CASE
WHEN hist.IgnoreOpportunity = 1 AND hist.PartnerGlobalCRMID = curr2.PartnerGlobalCRMID THEN 1 ELSE 0 END
ELSE CASE
WHEN hist.IgnoreOpportunity = 1
AND CONVERT(varchar, hist.CustomerAccountID) + hist.PartnerGlobalCRMID
= CONVERT(varchar, curr2.CustomerAccountID) + curr2.PartnerGlobalCRMID
AND hist.OpptyIncentiveCreatedDate = curr2.OpptyIncentiveCreatedDate
THEN 1
ELSE 0
END
END
END) AS Expr1
FROM dbo.tblOptyRecordsHistorical AS hist
inner join dbo.tblOptyRecordsCurrent AS curr2 on curr2.OpportunityGlobalCRMId = hist.OpportunityGlobalCRMId
WHERE curr2.OpportunityGlobalCRMId = curr.OpportunityGlobalCRMId) AS prevIgnored
FROM dbo.tblOptyRecordsCurrent AS curr
Haven't tested the code however.

Dynamically set the result of a TSQL query using CASE WHEN

SELECT MyTable.Name,
(
SELECT CASE WHEN ISNULL(SUM(TotalDays), 0) <= 0 THEN 0
ELSE SUM(TotalDays)
END AS Total
FROM Application AS Applications
WHERE (ID = MyTable.id)
) - MIN(Assignments) AS Excesses
FROM MyTable
The above TSQL statement is a subquery in a main query. When i run it, if TotalDays is NULL or <=0, then Total is set to 0 (zero).
What i would like to do here is to set the result of the whole query(Excesses) to 0. I want (Excesses) which is the result of Total - Min(Assignments) to be set to 0 if its NULL or <=0.
I want the CASE WHEN to apply to the whole query but am struggling to get it right.
SELECT
MyTable.Name,
CASE WHEN
0 < (SELECT SUM(TotalDays) FROM Application WHERE ID = MyTable.id) - MIN(Assignments)
THEN
(SELECT SUM(TotalDays) FROM Application WHERE ID = MyTable.id) - MIN(Assignments)
ELSE
0
END AS [Excesses]
FROM
MyTable
Note: MS SQL Server won't exexute the two correlated-sub-queries independantly, it will infact recognise that they are the same and re-use the results.
Alternative:
SELECT
MyTable.Name,
CASE WHEN
0 < SUM([application].TotalDays) - MIN([MyTable].Assignments)
THEN
SUM([application].TotalDays) - MIN([MyTable].Assignments)
ELSE
0 -- If either aggregate is NULL, 0 will still be returned
END AS [Excesses]
FROM
MyTable
LEFT JOIN
Application
ON [application].ID = [MyTable].id
SELECT MyTable.Name, CASE WHEN ISNULL(SUM(TotalDays), 0) <= 0 THEN 0 ELSE SUM(TotalDays) END AS Total
FROM Application AS Applications
JOIN MyTable
ON Applications.id = mytable.id
GROUP BY
MyTable.id, MyTable.name
HAVING CASE WHEN ISNULL(SUM(TotalDays), 0) <= 0 THEN 0 ELSE SUM(TotalDays) END > 0