case statement if table present - sql

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

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') ))
)

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

SQL Server 2008, how to count distinct values that change

I have entities that have 2 answers y/n.
I need to count the number of entities who change answers from 'n' to 'y' between stage1 and stage2.
entity || answer || stage
a || y || 1
a || n || 2
b || y || 1
b || y || 2
c || n || 1
c || n || 1
d || n || 1
d || y || 2
I tried this but this doesn't work (because it counts all entities who change answers)
select
entity, count(distinct answer)
from
myDB
where
stage between '1' and '2'
group by
entity, answer
but I don't understand why this doesn't work, the result comes out all O's
select
entity,
case
when stage = '1' and answer = 'n' and
stage = '2' and answer = 'y' then 1
else 0
end as 'result'
from
myDB
where
stage between '1' and '2'
group by
entity, stage, answer
select count(*)
from myDB s2
where
s2.stage ='2' and s2.answer='y'
and exists (select * from myDB s1
where s1.entity=s2.entity
and s1.stage ='1' and s1.answer='n'
)
select
count(*)
from
[myDb] as [s1]
inner join
[myDb] as [s2]
on
[s1].[entity] = [s2].[entity]
and [s1].[answer] = 'n'
and [s1].[stage] = 1
and [s2].[answer] = 'y'
and [s2].[stage] = 2;
But, it works with your provided data only.
If your have duplicated enities, it does not works, because at this case there is not impossible to identify uniqueness of entity. You need additional data then.
a y 1
a n 2
b y 1
b y 2
c n 1
c n 2
d n 1
d y 2
d n 1
d y 2
Lets make assumption, that same logical entity values are stored one after another. Then you can handle this by using this query:
declare #myDB TABLE
(
[rec_id] int identity(1, 1)
,[entity] varchar(10)
,[answer] varchar(10)
,[stage] int
);
insert into #myDB
(
[entity]
,[answer]
,[stage]
)
select
[entity]
,[answer]
,[stage]
from
[myDB];
select
[s1].[entity]
,count([s1].[entity])
from
#myDB as [s1]
inner join
#myDB as [s2]
on
[s1].[entity] = [s2].[entity]
and [s1].[answer] = 'n'
and [s1].[stage] = 1
and [s2].[answer] = 'y'
and [s2].[stage] = 2
and [s1].[rec_id] = [s2].[rec_id] - 1
group by
[s1].[entity];
select count(*)
from (select entity,stage,answer from myDB) t
pivot (max(answer) for stage in([1],[2])) p
where [1] = 'n' and [2] = 'y'
select count(*)
from (select 1 as x
from myDB
where stage in (1,2)
group by entity
having min(case when stage = 1 then answer end) = 'n'
and max(case when stage = 2 then answer end) = 'y'
) t

Joining 2 queries...error while pulling certain fields out

I am trying to join 2 queries & get certain columns out of the join. But I am getting an error. Can you please help me understand where I am going wrong -
SELECT X.*,Y.* FROM
(
(
SELECT
C1,C2,C3
COUNT(C4) AS CNT -- count
FROM [dbo].[Tb1]
WHERE C1 <> 0 AND -- amount not = zero
C2 = 'F' -- flag
GROUP BY C1,C2,C3
HAVING COUNT(C4) > 1
)X
INNER JOIN
(SELECT * FROM [dbo].[Tb1])Y
ON
X.C1 = Y.C1
AND X.C2 = Y.C2
AND X.C3=Y.C3
AND X.C4=Y.C4
)
The first query helps me get the duplicates & the second query will help me get the other fields out of the same table.
Thanks.
Solution #1:
SELECT X.*,Y.* FROM
--( <-- (1) comment this line
(
SELECT
C1,C2,C3, -- <-- (2) add , after C3
COUNT(C4) AS CNT -- count
FROM [dbo].[Tb1]
WHERE C1 <> 0 AND -- amount not = zero
C2 = 'F' -- flag
GROUP BY C1,C2,C3
HAVING COUNT(C4) > 1
)X
INNER JOIN
(SELECT * FROM [dbo].[Tb1])Y
ON
X.C1 = Y.C1
AND X.C2 = Y.C2
AND X.C3=Y.C3
AND X.CNT=Y.C4 <-- see anir's comment
--) <-- (3) comment this line
Or
Solution #2:
SELECT X.*, Y.*
FROM
(
SELECT
C1,C2,C3,
COUNT(C4) AS CNT -- count
FROM [dbo].[Tb1]
WHERE
C1 <> 0 AND -- amount not = zero
C2 = 'F' -- flag
GROUP BY C1,C2,C3
HAVING COUNT(C4) > 1
) X
INNER JOIN [dbo].[Tb1] Y
ON X.C1 = Y.C1
AND X.C2 = Y.C2
AND X.C3=Y.C3
AND X.CNT=Y.C4 <-- see anir's comment
Note #1: When CNT > 1 and x.C1 , y.C1 contains NULLs then X.C1 = Y.C1 <=> NULL = NULL which is evaluated to UNKNOWN if ANSI_NULLS is ON. This means that these rows will be eliminated from final resultset. The same applies to X.C2 = Y.C2 and X.C3=Y.C3.
SET ANSI_NULLS ON
SELECT CASE WHEN NULL = NULL THEN 1 ELSE 0 END AS T1
SET ANSI_NULLS OFF
SELECT CASE WHEN NULL = NULL THEN 1 ELSE 0 END AS T2
/*
T1
-----------
0
T2
-----------
1
*/
Note #2: "In a future version of SQL Server, ANSI_NULLS will always be ON and any applications that explicitly set the option to OFF will generate an error.".
Or
Solution #3:
SELECT y.*
FROM
(
SELECT x.*, COUNT(x.C4) OVER(PARTITION BY x.C1, x.C2, x.C3) AS CNT -- count
FROM [dbo].[Tb1] x
WHERE
x.C1 <> 0 AND -- amount not = zero
x.C2 = 'F' -- flag
-- AND x.C1 IS NOT NULL AND x.C2 IS NOT NULL AND x.C3 IS NOT NULL ?
) y
WHERE y.CNT > 1 AND y.CNT = y.C4

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