Conditional data selection in oracle - oracle

I have a set of data by using query and in this data I need to select some data according to following condition
If SIGNSTAT = 4 then select all row which have SIGNSTAT = 4
Otherwise select latest create date row data.
Please help to found the data
My query is as
WITH CTE AS(SELECT C2C.NO AS CONTRACTNO,
C.BRANCH,
C.IDCLIENT,
--C.PAN,
C.NAMEONCARD,
C.CREATEDATE,
C.SIGNSTAT,
ROW_NUMBER ()
OVER (PARTITION BY C2C.NO ORDER BY C.CREATEDATE DESC)
AS ROW_NUM
FROM A4M.TCONTRACTCARDITEM C2C, A4M.TCARD C,A4M.TREFERENCECARDPRODUCT RCP
WHERE 1 = 1 AND C.BRANCH = C2C.BRANCH AND C.PAN = C2C.PAN AND C2C.NO = '700000075333'
AND C.BRANCH = RCP.BRANCH AND C.CARDPRODUCT = RCP.CODE
AND UPPER(RCP.NAME) LIKE '%SUPPL%'
)
SELECT * FROM CTE
--WHERE ROW_NUM = 1
Result set:
CONTRACTNO BRANCH IDCLIENT NAMEONCARD CREATEDATE SIGNSTAT ROW_NUM
700000075333 1 1215995 SAMIR CHANDRA DHAR 14-Jul-19 4 2
700000075333 1 1215995 SAMIR CHANDRA DHAR 20-Aug-19 3 1

Is this what you need?
WITH data AS(SELECT C2C.NO AS CONTRACTNO,
C.BRANCH,
C.IDCLIENT,
--C.PAN,
C.NAMEONCARD,
C.CREATEDATE,
C.SIGNSTAT,
ROW_NUMBER ()
OVER (PARTITION BY C2C.NO ORDER BY C.CREATEDATE DESC)
AS ROW_NUM
FROM A4M.TCONTRACTCARDITEM C2C, A4M.TCARD C,A4M.TREFERENCECARDPRODUCT RCP
WHERE 1 = 1 AND C.BRANCH = C2C.BRANCH AND C.PAN = C2C.PAN AND C2C.NO = '700000075333'
AND C.BRANCH = RCP.BRANCH AND C.CARDPRODUCT = RCP.CODE
AND UPPER(RCP.NAME) LIKE '%SUPPL%'
), cnt AS (SELECT COUNT(1) total
FROM data
WHERE SIGNSTAT=4)
SELECT *
FROM data
WHERE SIGNSTAT=4
UNION ALL
SELECT *
FROM data
JOIN cnt
ON cnt.total=0
WHERE data.ROW_NUM = 1

I got the desire result by following query
WITH data AS(SELECT C2C.NO AS CONTRACTNO,
C.BRANCH,
C.IDCLIENT,
C.PAN,
C.NAMEONCARD,
C.CREATEDATE,
C.SIGNSTAT,
ROW_NUMBER ()
OVER (PARTITION BY C2C.NO ORDER BY C.CREATEDATE DESC)
AS ROW_NUM
FROM A4M.TCONTRACTCARDITEM C2C, A4M.TCARD C,A4M.TREFERENCECARDPRODUCT RCP
WHERE 1 = 1 AND C.BRANCH = C2C.BRANCH AND C.PAN = C2C.PAN AND C2C.NO = '700000075333'
AND C.BRANCH = RCP.BRANCH AND C.CARDPRODUCT = RCP.CODE
AND UPPER(RCP.NAME) LIKE '%SUPPL%') SELECT CONTRACTNO,BRANCH,IDCLIENT FROM data WHERE SIGNSTAT=4 UNION ALL (SELECT CONTRACTNO,BRANCH,IDCLIENT FROM data WHERE data.ROW_NUM = 1 MINUS SELECT CONTRACTNO,BRANCH,IDCLIENT FROM data WHERE SIGNSTAT=4)

You can achieve this with minor change in your original query. Use CASE WHEN as following:
WITH CTE AS(SELECT C2C.NO AS CONTRACTNO,
C.BRANCH,
C.IDCLIENT,
--C.PAN,
C.NAMEONCARD,
C.CREATEDATE,
C.SIGNSTAT,
CASE WHEN C.SIGNSTAT = 4 THEN 1 ELSE ROW_NUMBER ()
OVER (PARTITION BY C2C.NO ORDER BY C.CREATEDATE DESC) END
AS ROW_NUM
FROM A4M.TCONTRACTCARDITEM C2C, A4M.TCARD C,A4M.TREFERENCECARDPRODUCT RCP
WHERE 1 = 1 AND C.BRANCH = C2C.BRANCH AND C.PAN = C2C.PAN AND C2C.NO = '700000075333'
AND C.BRANCH = RCP.BRANCH AND C.CARDPRODUCT = RCP.CODE
AND UPPER(RCP.NAME) LIKE '%SUPPL%'
)
SELECT * FROM CTE
WHERE ROW_NUM = 1
Cheers!!

Related

ORACLE - FLATTEN DATA

I am sure that this question has been asked before but possibly not in this way.
Here is the data:
I need the rows flattened and the end output:
Any ideas?
As you already said we exactly can use row_number here and then use the row_number to pivot it,
select *
from
(
select userid,make,model,
row_number() over (partition by userid order by make,model) rn
from table1
)
pivot
(
max(make) make,max(model) model
for rn in (1,2,3,4,5)
)
Or
Using conditional aggregation which many prefers over PIVOT
select userid
,max(case when rn = 1 then make end) make_1
,max(case when rn = 1 then model end) model_1
,max(case when rn = 2 then make end) make_2
,max(case when rn = 2 then model end) model_2
,max(case when rn = 3 then make end) make_3
,max(case when rn = 3 then model end) model_3
from
(
select userid,make,model,
row_number() over (partition by userid order by make,model) rn
from table1
)
group by userid;
In both cases you can say the disadvantage is hard coding the row numbers but this is how it works or else you may opt for dynamic SQL if necessary.

procedure failurecompilation

I have an existing procedure which creates a table adwstg.switchhold_notificatn_stg
Suddenly it is throwing error in creating the table. I didn't change anything the statement.
First I thought the error is with ''No'' . So find & replace '' with '
But it is throwing error at line:
nvl(ba300_z51.sent_650_01, 'No') sent_650_01,
error(27,29): PLS-00103: Encountered the symbol "NO" when expecting one of the following: * & = - + ; < / > at in is mod remainder not rem <> or != or ~= >= <= <> and or like like2 like4 likec between || multiset member submultiset***
Below is the query :
create table adwstg.switchhold_notificatn_stg nologging parallel (degree 8) compress as
select distinct
bad3700.createdon,
bad3700.uc_pod_ext,
bpc.cacont_acc,
bad3700.notificatn,
bad3700.nfcat_code,
nvl(ba300_z51.sent_650_01, ''No'') sent_650_01,
decode(ba300_z51.sent_650_01,''No'',''--'',''Yes'',to_char(ba300_z51.ucswmsgdat,''yyyymmdd''),''--'') date_sent_650_01,
nvl(to_char(ba300_z51.ucswmsgtim,''hh24:mi:ss''),''--'') time_sent_650_01,
nvl(ba300_z52.received_650_02, ''No'') received_650_02,
decode(ba300_z52.received_650_02,''No'',''--'',''Yes'',to_char(ba300_z52.ucswmsgdat,''yyyymmdd''),''--'') date_received_650_02,
nvl(to_char(ba300_z52.ucswmsgtim,''hh24:mi:ss''),''--'') time_received_650_02,
nvl(ba300_z20.received_814_20, ''No'') received_814_20,
decode(ba300_z20.received_814_20,''No'',''--'',''Yes'',to_char(ba300_z20.ucswmsgdat,''yyyymmdd''),''--'') date_received_814_20,
nvl(to_char(ba300_z20.ucswmsgtim,''hh24:mi:ss''),''--'') time_received_814_20,
case
when trim(bad3700.nfcat_code) = ''SH01'' and zet.ext_ui is not null then ''ADDED''
when trim(bad3700.nfcat_code) = ''SH01'' and zet.ext_ui is null then ''NOT PROCESSED''
when trim(bad3700.nfcat_code) in (''SH02'',''SH03'') and zet.ext_ui is null then ''REMOVED''
when trim(bad3700.nfcat_code) in (''SH02'',''SH03'') and zet.ext_ui is not null then ''NOT PROCESSED''
else ''NOT PROCESSED''
end work_order_check
from
(select distinct *
from
(select
trunc(createdon) createdon,
trim(notificatn) notificatn,
trim(uc_pod_ext) uc_pod_ext,
trim(not_type) not_type,
trim(nfcat_code) nfcat_code,
row_number () over (partition by trim(uc_pod_ext),trunc(createdon) order by trim(notificatn) desc) rnum
from birpt.bic_azfc_ds3700
where upper(trim(not_type)) = ''SH''
and trim(bic_zdiscstat) = ''E0010'')
where rnum = 1
) bad3700
left outer join
(
select distinct ucinstalla, uc_pod_ext, datefrom, dateto
from birpt.bi0_qucinstalla
where objvers = ''A''
) bqi
on (trim(bad3700.uc_pod_ext) = trim(bqi.uc_pod_ext)
and trunc(bad3700.createdon) between bqi.datefrom and bqi.dateto)
left outer join
(
select distinct cacont_acc, ucinstalla, ucmovein_d, ucmoveoutd
from birpt.bi0_puccontract
where objvers = ''A''
) bpc
on (trim(bqi.ucinstalla) = trim(bpc.ucinstalla)
and trunc(bad3700.createdon) between bpc.ucmovein_d and bpc.ucmoveoutd)
left outer join
(select distinct *
from
(select
trim(ucswtpodex) ucswtpodex,
trunc(ucswmsgdat) ucswmsgdat,
ucswmsgtim,
case --650_01 CHECK
when trim(uc_mdcat) = ''Z51'' then ''Yes''
else ''No''
end sent_650_01,
row_number () over (partition by trim(ucswtpodex), trunc(ucswmsgdat) order by trunc(ucswmsgdat) desc, ucswmsgtim desc) rnum
from birpt.bic_azudeds0300
where trim(uc_mdcat) = ''Z51'')
where rnum = 1) ba300_z51
on (trim(bad3700.uc_pod_ext) = trim(ba300_z51.ucswtpodex)
and trunc(bad3700.createdon)= trunc(ba300_z51.ucswmsgdat))
left outer join
(select distinct *
from
(select
trim(ucswtpodex) ucswtpodex,
trunc(ucswmsgdat) ucswmsgdat,
ucswmsgtim,
case --650_02 CHECK
when trim(uc_mdcat) = ''Z52'' then ''Yes''
else ''No''
end received_650_02,
row_number () over (partition by trim(ucswtpodex), trunc(ucswmsgdat) order by trunc(ucswmsgdat) desc, ucswmsgtim desc) rnum
from birpt.bic_azudeds0300
where trim(uc_mdcat) = ''Z52'')
where rnum = 1) ba300_z52
on (trim(bad3700.uc_pod_ext) = trim(ba300_z52.ucswtpodex)
and trunc(bad3700.createdon)= trunc(ba300_z52.ucswmsgdat))
left outer join
(select distinct *
from
(select
trim(ucswtpodex) ucswtpodex,
trunc(ucswmsgdat) ucswmsgdat,
ucswmsgtim,
case --814_20 CHECK
when trim(uc_mdcat) = ''Z20'' then ''Yes''
else ''No''
end received_814_20,
row_number () over (partition by trim(ucswtpodex), trunc(ucswmsgdat) order by trunc(ucswmsgdat) desc, ucswmsgtim desc) rnum
from birpt.bic_azudeds0300
where trim(uc_mdcat) = ''Z20'')
where rnum = 1) ba300_z20
on (trim(bad3700.uc_pod_ext) = trim(ba300_z20.ucswtpodex)
and trunc(bad3700.createdon)= trunc(ba300_z20.ucswmsgdat))
left outer join
(select distinct ext_ui
from isurpt.zcs_esiid_tamper
) zet
on (trim(bad3700.uc_pod_ext) = trim(zet.ext_ui));
If you remove all doubled single quotes (perform replace function in any text editor), your code will be valid (as far as syntax is concerned).
I don't have your tables to test it, and there are too many of them with too many columns to create a test case by myself.
The question is: why did you double them in the first place?

Please help me to optimize the below mentioned script as the same table(i.e. Incident_Audit_log) has utilized multiple times?

select A.*
from Incident_Audit_log a where incident_audit_log_id in
(select top 1 incident_audit_log_id from Incident_Audit_log b
where b.incident_id=a.incident_id and b.status_did=a.status_did
and b.tracking_code_did = (select tracking_code_did
from Incident_Audit_log where update_date = (select MAX(update_date)
from Incident_Audit_log where Status_did in (103, 1035)
and incident_id = b.incident_id)
and incident_id = b.incident_id)
order by update_date asc)
I am not sure what you want to achieve but I guess that you want to extract row with new newest update and status_did equal to 13 and 1035.
In that case this should work:
select *
from (
select ROW_NUMBER() OVER(ORDER BY update_date DESC) AS rn,
*
from Incident_Audit_log
where status_did in (103, 1035)
) as SubQueryAlias
where rn = 1
In case not , provide more info.

CROSS APPLY too slow for running total - TSQL

Please see my code below as it is running too slowly with the CROSS APPLY.
How can I remove the CROSS APPLY and add something else that will run faster?
Please note I am using SQL Server 2008 R2.
;WITH MyCTE AS
(
SELECT
R.NetWinCURRENCYValue AS NetWin
,dD.[Date] AS TheDay
FROM
dimPlayer AS P
JOIN
dbo.factRevenue AS R ON P.playerKey = R.playerKey
JOIN
dbo.vw_Date AS dD ON Dd.dateKey = R.dateKey
WHERE
P.CustomerID = 12345)
SELECT
A.TheDay AS [Date]
,ISNULL(A.NetWin, 0) AS NetWin
,rt.runningTotal AS CumulativeNetWin
FROM MyCTE AS A
CROSS APPLY (SELECT SUM(NetWin) AS runningTotal
FROM MyCTE WHERE TheDay <= A.TheDay) AS rt
ORDER BY A.TheDay
CREATE TABLE #temp (NetWin money, TheDay datetime)
insert into #temp
SELECT
R.NetWinCURRENCYValue AS NetWin
,dD.[Date] AS TheDay
FROM
dimPlayer AS P
JOIN
dbo.factRevenue AS R ON P.playerKey = R.playerKey
JOIN
dbo.vw_Date AS dD ON Dd.dateKey = R.dateKey
WHERE
P.CustomerID = 12345;
SELECT
A.TheDay AS [Date]
,ISNULL(A.NetWin, 0) AS NetWin
,SUM(B.NetWin) AS CumulativeNetWin
FROM #temp AS A
JOIN #temp AS B
ON A.TheDay >= B.TheDay
GROUP BY A.TheDay, ISNULL(A.NetWin, 0);
Here https://stackoverflow.com/a/13744550/613130 it's suggested to use recursive CTE.
;WITH MyCTE AS
(
SELECT
R.NetWinCURRENCYValue AS NetWin
,dD.[Date] AS TheDay
,ROW_NUMBER() OVER (ORDER BY dD.[Date]) AS RN
FROM dimPlayer AS P
JOIN dbo.factRevenue AS R ON P.playerKey = R.playerKey
JOIN dbo.vw_Date AS dD ON Dd.dateKey = R.dateKey
WHERE P.CustomerID = 12345
)
, MyCTERec AS
(
SELECT C.TheDay AS [Date]
,ISNULL(C.NetWin, 0) AS NetWin
,ISNULL(C.NetWin, 0) AS CumulativeNetWin
,C.RN
FROM MyCTE AS C
WHERE C.RN = 1
UNION ALL
SELECT C.TheDay AS [Date]
,ISNULL(C.NetWin, 0) AS NetWin
,P.CumulativeNetWin + ISNULL(C.NetWin, 0) AS CumulativeNetWin
,C.RN
FROM MyCTERec P
INNER JOIN MyCTE AS C ON C.RN = P.RN + 1
)
SELECT *
FROM MyCTERec
ORDER BY RN
OPTION (MAXRECURSION 0)
Note that this query will work if you have 1 record == 1 day! If you have multiple records in a day, the results will be different from the other query.
As I said here, if you want really fast calculation, put it into temporary table with sequential primary key and then calculate rolling total:
create table #Temp (
ID bigint identity(1, 1) primary key,
[Date] date,
NetWin decimal(29, 10)
)
insert into #Temp ([Date], NetWin)
select
dD.[Date],
sum(R.NetWinCURRENCYValue) as NetWin,
from dbo.dimPlayer as P
inner join dbo.factRevenue as R on P.playerKey = R.playerKey
inner join dbo.vw_Date as dD on Dd.dateKey = R.dateKey
where P.CustomerID = 12345
group by dD.[Date]
order by dD.[Date]
;with cte as (
select T.ID, T.[Date], T.NetWin, T.NetWin as CumulativeNetWin
from #Temp as T
where T.ID = 1
union all
select T.ID, T.[Date], T.NetWin, T.NetWin + C.CumulativeNetWin as CumulativeNetWin
from cte as C
inner join #Temp as T on T.ID = C.ID + 1
)
select C.[Date], C.NetWin, C.CumulativeNetWin
from cte as C
order by C.[Date]
I assume that you could have duplicates dates in the input, but don't want duplicates in the output, so I grouped data before puting it into the table.

Query to exclude row based on another row's filter

I'm using Oracle 10g.
Question: How can I write query to return just ID only if ALL the codes for that ID end in 6? I don't want ID=1 because not all its codes end in 6.
TABLE_A
ID Code
===============
1 100
1 106
2 206
3 316
3 326
4 444
Desired Result:
ID
==
2
3
You simply want each ID where the count of rows for that id is the same as the count of rows where the third digit is six.
SELECT ID
FROM TABLE_A
GROUP BY ID
HAVING COUNT(*) = COUNT(CASE WHEN SUBSTR(code,3,1) = '6' THEN 1 END)
Try this:
SELECT DISTINCT b.id
FROM (
SELECT id,
COUNT(1) cnt
FROM table_a
GROUP BY id
) a,
(
SELECT id,
COUNT(1) cnt
FROM table_a
WHERE CODE LIKE '%6'
GROUP BY id
)b
WHERE a.id = b.id
AND a.cnt = b.cnt
Alternative using ANALYTIC functions:
SELECT DISTINCT id
FROM
(
SELECT id,
COUNT(1) OVER(PARTITION BY id) cnt,
SUM(CASE WHEN code LIKE '%6' THEN 1 ELSE 0 END) OVER(PARTITION BY id) sm
FROM table_a
)
WHERE cnt = sm

Resources