How to identify duplicate rows having value within data range in oracle - oracle

I want to identify rows where value in Name column is same and value in start/ end column lies within range of Start and End value of another row...
for eg. for me row with ID value 4 & 1 are duplicate because they have same value in Name column and value in start column 26 of ID 4 lies within start & End values of ID 1 (24-56)
ID Name Start End
1 Adam 24 56
2 Max 1 5
3 Neil 6 4
4 Adam 26 30

You can use EXISTS for this:
select *
from yourtable y
where exists (
select 1
from yourtable y2
where y.id <> y2.id
and y.name = y2.name
and (y2.startfield between y.startfield and y.endfield
or
y.startfield between y2.startfield and y2.endfield))
SQL Fiddle Demo
I wasn't completely sure from your question if the end range had to be included as well. If so, you'll need to add that to the where criteria:
select *
from yourtable y
where exists (
select 1
from yourtable y2
where y.id <> y2.id
and y.name = y2.name
and ((y2.startfield > y.startfield and y2.endfield < y.endfield)
or
(y.startfield > y2.startfield and y.endfield < y2.endfield)))

Related

Group by on Key columns and get the records having one valid value and zero value

we have a scenario where as per group by key columns i have to get only record having combination of 0 & any valid number for column "ID"
with the below example
TransId,CustomerNm,Date ,Number,Gender,ID
1 ,Surya ,2020-01-01,123456,M ,1234
1 ,Surya ,2020-01-01,123456,M ,0
2 ,Naren ,2020-01-20,123456,M ,3456
2 ,Naren ,2020-01-20,123456,M ,6789
When i try with the below query (key columns: TransId,CustomerNm,Date,Number,Gender)
select TransId,CustomerNm,Date,Number,Gender from INS.Transaction
group by TransId,CustomerNm,Date,Number,Gender having count(*) > 1
i will get both the records
1 ,Surya ,2020-01-01,123456,M
2 ,Naren ,2020-01-20,123456,M
but i am trying to get the records having ID =0 . Expecting output as
1 ,Surya ,2020-01-01,123456,M
Pls suggest if i can add any change in the above query
while doing group by add MIN(ID) in select query and then fetch id 0
select * from (
select TransId,CustomerNm,Date,Number,Gender,MIN(ID) ID from INS.Transaction
group by TransId,CustomerNm,Date,Number,Gender having count(*) > 1 ) where ID = 0
Just add another AND to the having clause to check if min(id) = 0 . I have renamed some columns.
select transid,customernm,transaction_date,some_number,gender
from transaction
group by transid,customernm,transaction_date,some_number,gender
having count(*) > 1 and min(id) = 0;
Note:- Do not use reserved words as column name like date and number

Referancing value from select column in where clause : Oracle

My tables are as below
MS_ISM_ISSUE
ISSUE_ID ISSUE_DUE_DATE ISSUE_SOURCE_TYPE
I1 25-11-2018 1
I2 25-12-2018 1
I3 27-03-2019 2
MS_ISM_SOURCE_SETUP
SOURCE_ID MODULE_NAME
1 IT-Compliance
2 Risk Assessment
I have written following query.
with rs as
(select
count(ISSUE_ID) as ISSUE_COUNT, src.MODULE_NAME,
case
when ISSUE_DUE_DATE<sysdate then 'Overdue'
when ISSUE_DUE_DATE between sysdate and sysdate + 90 then 'Within 3 months'
when ISSUE_DUE_DATE>sysdate+90 then 'Beyond 90 days'
end as date_range
from MS_ISM_ISSUE issue, MS_ISM_SOURCE_SETUP src
where issue.Issue_source_type = src.source_id
group by src.MODULE_NAME, case
when ISSUE_DUE_DATE<sysdate then 'Overdue'
when ISSUE_DUE_DATE between sysdate and sysdate + 90 then 'Within 3 months'
when ISSUE_DUE_DATE>sysdate+90 then 'Beyond 90 days'
end)
select ISSUE_COUNT,MODULE_NAME, DATE_RANGE,
(select count(ISSUE_COUNT) from rs where rs.MODULE_NAME=MODULE_NAME) as total from rs;
The output of the code is as below.
ISSUE_COUNT MODULE_NAME DATE_RANGE Total
1 IT-Compliance Overdue 3
1 IT-Compliance Within 3 months 3
1 Risk Assessment Beyond 90 days 3
The result is correct till 3rd column. In 4th column what I want is, total of Issue count for given module name. Hence in above case Total column will have value as 2 for first and second row (since there are 2 Issues for IT-Compliance) and value 1 for the third row (since one issue is present for Risk Assessment).
Essentially, I want to achieve is to replace current row's MODULE_NAME in last where clause. How do I achieve this using query?
OK, this condition
where rs.MODULE_NAME=MODULE_NAME
is essentially the same as if you wrote
where MODULE_NAME = MODULE_NAME
which is simply always true (if there are no nulls in module_name).
Try using different table alias for inner query and outer query, e.g.
select count(ISSUE_COUNT) from rs rs2 where rs2.MODULE_NAME=rs.MODULE_NAME
You can also try to use analytic function here, something like
select ISSUE_COUNT,
MODULE_NAME,
DATE_RANGE,
COUNT(ISSUE_COUNT) OVER (PARTITION BY RS.MODULE_NAME) AS TOTAL
from rs
instead of your subquery

SQL ORACLE MERGE Table

I want to update my table MA_PARTICIPANT using MERGE UPDATE CLAUSE. I want to update two columns:
MA_PARTICIPANT.CUSTOMER_RK,MA_PARTICIPANT.DEACT_FLG. CUSTOMER_RK will be updated with my second table TABLE_CHANGE which consists two columns:-
CUSTOMER_RK,NEW_CUSTOMER_RK. MA_PARTICIPANT.DEACT_FLG in table MA_PARTICIPANT may be 'Y' or 'N' and if it 'N' then i should change it to 'Y',but if it already 'Y' then i should not update this column. Merging tables will be on MA_PARTICIPANT.part_id = TABLE_CHANGE.part_id, but i dont know how to use merge with condition on MA_PARTICIPANT.DEACT_FLG.
Finally, each CUSTOMER_RK which i update should has flg = 'Y'
simple example of MA_PARTICIPANT:
PART_ID CUSTOMER_RK DEACT_FLG
1 10 Y
2 10 N
3 20 Y
simple example of TABLE_TO_CHANGE:
PART_ID CUSTOMER_RK NEW_CUSTOMER_RK
1 10 100
2 10 100
3 20 200
MA_PARTICIPANT after update:
PART_ID CUSTOMER_RK DEACT_FLG
1 100 Y
2 100 Y
3 200 Y
I suppose that that it will be:
merge INTO MA_PARTICIPANT P USING
(SELECT * from TABLE_TO_CHANGE) TT ON (TT.PART_ID = P.PART_ID)
WHEN matched THEN
UPDATE
SET
IF P.DEACTIVATED_FLG <> 'Y' THEN
P.CUSTOMER_RK = TT.NEW_CUSTOMER_RK,
P.DEACTIVATED_FLG = 'Y'
ELSE
P.CUSTOMER_RK = TT.NEW_CUSTOMER_RK
This should update all matching rows, setting DEACT_FLG to Y and CUSTOMER_RK to NEW_CUSTOMER_RK:
MERGE INTO MA_PARTICIPANT P
USING TABLE_TO_CHANGE TT
ON (TT.PART_ID = P.PART_ID)
WHEN matched THEN
UPDATE
SET CUSTOMER_RK = NEW_CUSTOMER_RK,
DEACT_FLG = 'Y'

How to select two max value from different records that has same ID for every records in table

i have problem with this case, i have log table that has many same ID with diferent condition. i want to select two max condition from this. i've tried but it just show one record only, not every record in table.
Here's my records table:
order_id seq status____________________
1256 2 4
1256 1 2
1257 0 2
1257 3 1
Here my code:
WITH t AS(
SELECT x.order_id
,MAX(y.seq) AS seq2
,MAX(y.extern_order_status) AS status
FROM t_order_demand x
JOIN t_order_log y
ON x.order_id = y.order_id
where x.order_id like '%12%'
GROUP BY x.order_id)
SELECT *
FROM t
WHERE (t.seq2 || t.status) IN (SELECT MAX(tt.seq2 || tt.status) FROM t tt);
this query works, but sometime it gave wrong value or just show some records, not every records.
i want the result is like this:
order_id seq2 status____________________
1256 2 4
1257 3 2
I think you just want an aggregation:
select d.order_id, max(l.seq2) as seq2, max(l.status) as status
from t_order_demand d join
t_order_log l
on d.order_id = l.order_id
where d.order_id like '%12%'
group by d.order_id;
I'm not sure what your final where clause is supposed to do, but it appears to do unnecessary filtering, compared to what you want.

Oracle Case clause can not evaluate an "AND" with tow or more expressions

Oracle behaves really extrange with the next query:
I am trying to evaluate these three records, one of them should show the column digitado = 1 because it accomplishes all the conditions, which are, NUM_DOCUMENTO_ENCABEZADO iS NOT NULL and ORIGEN_PLANILLA = 2
NUM_DOCUMENTO NUM_DOCUMENTO_ENCABEZADO ORIGEN_PLANILLA
8220568059 8220568059 2
8220681644 2
940723593097 1
select x.num_documento,
x.origen_planilla,
x.num_documento_encabezado,
case
when x.num_documento_encabezado > '0' and x.origen_planilla = 2 then
1
else
0
end digitado
from (
select h.num_documento,
h.num_documento_encabezado,
h.origen_planilla
from (
select a.num_documento,
c.num_documento num_documento_encabezado,
case when NVl(UPPER(a.txt_observacion),'X') like '%SGP%' THEN 1 ELSE 2 END origen_planilla
from epsis.ryc_recaudo a,
epsis.ryc_recaudo_unificado b,
epsis.ryc_documento_encabezado c
where a.fec_pago >= to_date('28082013','ddmmyyyy') ---aca se coloca el dia del ultimo proceso,
and a.fec_pago < to_date('25092013','ddmmyyyy')-- el cecaudo viene un dia atrasados
and b.num_documento(+) = a.num_documento
and c.num_documento(+) = b.num_documento --80595
and a.num_documento in ( '940723593097', '8220681644','8220568059')
) h,
epsis.ryc_divide_documento f,
epsis.ryc_documento_encabezado g
where f.num_documento(+) = h.num_documento
and g.num_documento(+) =f.num_division
group by h.num_documento,
h.num_documento_encabezado,
h.origen_planilla
) x
This is the result:
NUM_DOCUMENTO ORIGEN_PLANILLA NUM_DOCUMENTO_ENCABEZADO DIGITADO
8220568059 2 8220568059 0
8220681644 2 0
940723593097 1 0
The column DIGITADO should be "1" for the first record.
Oracle can not evaluate this "CASE" properly:
case
when x.num_documento_encabezado > '0' and x.origen_planilla = 2 then
1
else
0
end digitado
I have tried diferent things, for example if I change the previous code for this:
case
when length(x.num_documento_encabezado||x.origen_planilla) > 1 then
1
else
0
end digitado
This is the result:
NUM_DOCUMENTO ORIGEN_PLANILLA NUM_DOCUMENTO_ENCABEZADO DIGITADO
8220568059 2 8220568059 1
8220681644 2 0
940723593097 1 0
It works for every record, but that is not the point, the point is that oracle is not able to evaluate the "AND" expression, and the ortiginal query is much longer than the example displayed.
Now, another extrange this is that, when I execute the query only for the record that is ok, I mean this
and a.num_documento in ('8220568059')
the "AND" expression in the case sentence works properly with the original "CASE".
Result:
NUM_DOCUMENTO ORIGEN_PLANILLA NUM_DOCUMENTO_ENCABEZADO DIGITADO
8220568059 2 8220568059 1
Another thing is that, and here is where i believe the problem is, when no outer join in the second subquery, then the query runs ok, but I need that outer join, I am talking about this:
where f.num_documento(+) = h.num_documento
and g.num_documento(+) =f.num_division
I really don't want to rewrite the full query, does anyone know why this happen?
Create and insert statements, these ones reproduce the issue
create table tmp_origin
(
doc varchar2(30),
val number,
obs varchar2(30)
);
create table tmp_uni
(
doc varchar2(30),
doc_origin varchar2(30)
);
create table tmp_div
(
doc varchar2(30),
doc_div varchar2(30)
);
insert into tmp_origin values ('8220568059',100000, 'NORMAL');
insert into tmp_origin values ('8220681644',200000, 'NORMAL');
insert into tmp_origin values ('940723593097',300000, 'SGP');
commit;
insert into tmp_uni values ('8220568059','8220568059');
commit;
This is the query adapted to the above lines, I have also added some others cases, so you can compare and identify that the first one is not working
select x.num_documento,
x.origen_planilla,
x.num_documento_encabezado,
case
when x.num_documento_encabezado is not null and x.origen_planilla = 2 then
1
else
0
end digitado,
case
when length(x.num_documento_encabezado||x.origen_planilla) > 1 then
1
else
0
end digitado2,
case
when x.origen_planilla = 2 then
case
when x.num_documento_encabezado is not null then
1
else
0
end
else
0
end digitado3
from (
select h.num_documento,
h.num_documento_encabezado,
h.origen_planilla
from (
select a.doc num_documento,
b.doc num_documento_encabezado,
case when NVl(UPPER(a.obs),'X') like '%SGP%' THEN 1 ELSE 2 END origen_planilla
from tmp_origin a,
tmp_uni b
where a.doc in ( '940723593097', '8220681644','8220568059')
and b.doc(+) = a.doc
) h,
tmp_div f
where f.doc(+) = h.num_documento
group by h.num_documento,
h.num_documento_encabezado,
h.origen_planilla
) x
You should almost never use the comparison operators with VARCHAR2, it is almost never useful (except if you are writing a sorting algorithm). In your case especially, it doesn't do what you expect.
When you compare VARCHAR2s, the result will depend upon character ordering (for instance '2' is "greater" than '10' because 2 comes after 1 in the character table).
Consider:
SQL> select * from dual where '8220568059' > '0';
DUMMY
-----
X
SQL> select * from dual where ' 8220568059' > '0';
DUMMY
-----
Always use the right datatype for the right task. There is almost always only one datatype that will work correctly. You should always use NUMBER and explicit datatype conversion when working with numbers:
SQL> select * from dual where to_number('8220568059') > 0;
DUMMY
-----
X
Also if you just want to know if a value is NULL, please use the IS NOT NULL operator:
SQL> WITH DATA AS (
2 SELECT '8220568059' num_documento_encabezado,
3 2 origen_planilla FROM dual UNION ALL
4 SELECT '', 2 FROM dual UNION ALL
5 SELECT '', 1 FROM dual)
6 SELECT x.origen_planilla,
7 x.num_documento_encabezado,
8 CASE
9 WHEN x.num_documento_encabezado IS NOT NULL
10 AND x.origen_planilla = 2 THEN
11 1
12 ELSE
13 0
14 END digitado
15 FROM DATA x;
ORIGEN_PLANILLA NUM_DOCUMENTO_ENCABEZADO DIGITADO
--------------- ------------------------ ----------
2 8220568059 1
2 0
1 0

Resources