Postgres: missing FROM-clause entry for table - SQL - 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') ))
)

Related

skip null or 0 value while using row_number sql server

i am trying to get row_number from the data, but i want to skip the null or 0 value
DECLARE #ProcessId INT = 6013006
DECLARE #CommHeaderId int
SELECT #CommHeaderId = a.id
FROM EprocUltimate.dbo.commercial_header a
WHERE a.process_id = #ProcessId
SELECT b.rank_quotation, c.rank_nego,
CASE ISNULL(a.quotation_usd, 0) WHEN 0 THEN a.quotation_idr ELSE a.quotation_usd END As Quotation,
CASE ISNULL(a.nego_usd, 0) WHEN 0 THEN a.nego_idr ELSE a.nego_usd END As Nego
FROM EprocUltimate.dbo.commercial_vendor a
INNER JOIN
(
SELECT a.id, ROW_NUMBER() OVER(ORDER BY CASE quotation_usd WHEN 0 THEN quotation_idr ELSE quotation_usd END ASC) AS rank_quotation
FROM EprocUltimate.dbo.commercial_vendor a
WHERE a.commercial_header_id = #CommHeaderId AND a.commercial_admin_bid_evaluation_result = 'Pass'
) b ON a.id = b.id
INNER JOIN
(
SELECT a.id, ROW_NUMBER() OVER(ORDER BY CASE ISNULL(nego_usd, 0) WHEN 0 THEN nego_idr ELSE nego_usd END ASC) AS rank_nego
FROM EprocUltimate.dbo.commercial_vendor a
WHERE a.commercial_header_id = #CommHeaderId AND a.commercial_admin_bid_evaluation_result = 'Pass'
) c ON a.id = c.id
WHERE a.commercial_header_id = #CommHeaderId AND a.commercial_admin_bid_evaluation_result = 'Pass'
ORDER BY b.rank_quotation
heres the result
rank_quotation rank_nego, value1, value2
1 3 775460000.00 770000000.00
2 1 781036525.00 NULL
3 2 786250000.00 NULL
from this query what i want is to get row_numbering the rank_nego with having value first
so the result that i want to achive is
rank_quotation rank_nego, value1, value2
1 1 775460000.00 770000000.00
2 2 781036525.00 NULL
3 3 786250000.00 NULL
You can use a little workaround to have your NULL values as last records in your subquery with the ROW_NUMBER() OVER (ORDER BY ...) as rank_nego
(
SELECT a.id, ROW_NUMBER() OVER(ORDER BY
-- This statement will first sort records having nego_usd and nego_idr as NULL/0 at the end of the list
CASE WHEN ISNULL(nego_usd, 0) = 0 AND ISNULL(nego_idr, 0) = 0 THEN 1 ELSE 0 END ASC,
-- and then sort with the real values
CASE ISNULL(nego_usd, 0) WHEN 0 THEN nego_idr ELSE nego_usd END ASC
) AS rank_nego
FROM EprocUltimate.dbo.commercial_vendor a
WHERE a.commercial_header_id = #CommHeaderId AND a.commercial_admin_bid_evaluation_result = 'Pass'
) c ON a.id = c.id

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

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.

case statement if table present

I am having problem in finding table from different database (table sometimes get dropped due to procedure) if the table is present then condition else direct ans as 1
select (case when exists (select * from SysSet.INFORMATION_SCHEMA .TABLES where TABLE_NAME = 'CHQPASS') then (
case when left(tranm.docu_no,1) <>'3' or tranm.docu_dt <'01/01/2015' then 1
when (select top 1 ACHD_KEY from TRAND k where k.TRN_NO = TRANM.TRN_NO and k.DR_CR ='D' ) = 'A000100010002' THEN 1
WHEN (select COUNT(*) from SYSSET..chqpass D where D.COMP_DIR ='PSAGR' and D.DOCU_DT = TRANM.DOCU_DT and D.AMT = TRAND.TOT_AMT ) <> 0
THEN 1
else cast(isnull (trand.RECONCILE,0)as int)
end)
else 1
end )as pend
if table is not present it show the error
Msg 208, Level 16, State 1, Line 2 Invalid object name
'SYSSET..chqpass'.
table not present how can i stop it to see that table
Replace following line
WHEN (select COUNT(*) from SYSSET..chqpass D where D.COMP_DIR ='xyz' and D.DOCU_DT >'01/01/2015' and D.AMT = 2556 ) <> 0
with this line
WHEN EXEC('(select COUNT(*) from SYSSET..chqpass D where D.COMP_DIR =''xyz'' and D.DOCU_DT >''01/01/2015'' and D.AMT = 2556 )') <> 0
It should work this way...
Try this try replacing the sys.tables with INformationschema so you can specify which schema the table is present in.
SELECT
(
CASE WHEN EXISTS(SELECT * FROM SYSSET.INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'CHQPASS' AND TABLE_SCHEMA = 'dbo')
THEN (
CASE WHEN LEFT(tranm.docu_no,1) <>'3' or tranm.docu_dt < '01/01/2015' THEN 1
WHEN (SELECT TOP 1 ACHD_KEY FROM TRAND k WHERE k.TRN_NO = TRANM.TRN_NO and k.DR_CR ='D' ) = 'A000100010002' THEN 1
WHEN (select COUNT(*) from SYSSET..chqpass D WHERE D.COMP_DIR ='PSAGR' and D.DOCU_DT = TRANM.DOCU_DT and D.AMT = TRAND.TOT_AMT ) <> 0 THEN 1
ELSE cast(isnull (trand.RECONCILE,0)as int)
END
)
ELSE 1
END
) as pend

counting records on the same table with different values possibly none sql server 2008

I have a inventory table with a condition i.e. new, used, other, and i am query a small set of this data, and there is a possibility that all the record set contains only 1 or all the conditions. I tried using a case statement, but if one of the conditions isn't found nothing for that condition returned, and I need it to return 0
This is what I've tried so far:
select(
case
when new_used = 'N' then 'new'
when new_used = 'U' then 'used'
when new_used = 'O' then 'other'
end
)as conditions,
count(*) as count
from myDB
where something = something
group by(
case
when New_Used = 'N' then 'new'
when New_Used = 'U' then 'used'
when New_Used = 'O' then 'other'
end
)
This returns the data like:
conditions | count
------------------
new 10
used 45
I am trying to get the data to return like the following:
conditions | count
------------------
new | 10
used | 45
other | 0
Thanks in advance
;WITH constants(letter,word) AS
(
SELECT l,w FROM (VALUES('N','new'),('U','used'),('O','other')) AS x(l,w)
)
SELECT
conditions = c.word,
[count] = COUNT(x.new_used)
FROM constants AS c
LEFT OUTER JOIN dbo.myDB AS x
ON c.letter = x.new_used
AND something = something
GROUP BY c.word;
try this -
DECLARE #t TABLE (new_used CHAR(1))
INSERT INTO #t (new_used)
SELECT t = 'N'
UNION ALL
SELECT 'N'
UNION ALL
SELECT 'U'
SELECT conditions, ISNULL(r.cnt, 0) AS [count]
FROM (
VALUES('U', 'used'), ('N', 'new'), ('O', 'other')
) t(c, conditions)
LEFT JOIN (
SELECT new_used, COUNT(1) AS cnt
FROM #t
--WHERE something = something
GROUP BY new_used
) r ON r.new_used = t.c
in output -
new 2
used 1
other 0
You can do it as a cross-tab:
select
sum(case when new_used = 'N' then 1 else 0 end) as N,
sum(case when new_used = 'U' then 1 else 0 end) as U,
sum(case when new_used = 'O' then 1 else 0 end) as Other
from myDB
where something = something

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