How to use analytic count to conditionally count? - sql

Using this example, how can I tweak my sql to report whether a listing_id has passed all the tests?
with listing_row
as
(
select 1 as listing_id, 'TEST1' as listing_test, 'Y' as pass_yn from dual union all
select 1 as listing_id, 'TEST2' as listing_test, 'Y' as pass_yn from dual union all
select 1 as listing_id, 'TEST3' as listing_test, 'Y' as pass_yn from dual union all
select 2 as listing_id, 'TEST1' as listing_test, 'N' as pass_yn from dual union all
select 2 as listing_id, 'TEST2' as listing_test, 'Y' as pass_yn from dual union all
select 2 as listing_id, 'TEST3' as listing_test, 'N' as pass_yn from dual union all
select 3 as listing_id, 'TEST1' as listing_test, 'N' as pass_yn from dual union all
select 3 as listing_id, 'TEST2' as listing_test, 'N' as pass_yn from dual union all
select 3 as listing_id, 'TEST3' as listing_test, 'N' as pass_yn from dual)
select listing_id,
listing_test,pass_yn,
count(*) over (partition by listing_id, pass_yn) as all_y,
count(*) over (partition by listing_id, pass_yn) as all_n
from listing_row
Desired Results
LISTING_ID ALL_Y ALL_N
1 Y N
2 N N
3 N Y

I think the easiest way is to use min() and max():
select listing_id,
listing_test, pass_yn,
min(pass_yn) over (partition by listing_id) as all_y,
min(case when pass_yn = 'Y' then 'N' else 'Y' end) over (partition by listing_id) as all_n
from listing_row;
This uses a trick based on the fact the "Y" > "N". So, if you take the min() of the column and it has any "N" values, then the result will be "N".

This is another solution using sum():
SELECT listing_id,
CASE WHEN max(all_test_cnt)-MAX(num_of_test) = 0 THEN 'Y' ELSE 'N' END all_y,
CASE WHEN MAX(num_of_test) = 0 THEN 'Y' ELSE 'N' END all_n
FROM
(SELECT listing_id,
COUNT(DISTINCT listing_test) OVER (PARTITION BY NULL) all_test_cnt,
SUM(CASE WHEN pass_yn = 'Y' THEN 1 ELSE 0 END) OVER (PARTITION BY listing_id)num_of_test
FROM listing_row)
GROUP BY listing_id

Related

Reporting MIN(VAL) for Each Partitioned Value in Another Column

Oracle 11g
How can I return single row of min(email_sent) for each email_type per ID?
That is, I'd like to get the first invitation date and the first confirmation date into a single row.
with mailings as (
select 1 as recipient_id, 'INVITE' as email_type, to_date('JAN-01-2020','MON-DD-YYYY') as email_sent from dual union all
select 1 as recipient_id, 'INVITE' as email_type, to_date('JAN-02-2020','MON-DD-YYYY') as email_sent from dual union all
select 1 as recipient_id, 'INVITE' as email_type, to_date('JAN-03-2020','MON-DD-YYYY') as email_sent from dual union all
select 1 as recipient_id, 'CONFIRM'as email_type, to_date('JAN-10-2020','MON-DD-YYYY') as email_sent from dual union all
select 1 as recipient_id, 'CONFIRM'as email_type, to_date('JAN-11-2020','MON-DD-YYYY') as email_sent from dual
)
select *
from (
select recipient_id,
email_type,
min(email_sent) over (partition by recipient_id, email_type) as first_invite,
min(email_sent) over (partition by recipient_id, email_type) as first_confirmation,
rank() over (partition by recipient_id, email_type order by email_sent) as email_type
from mailings
)
where email_type=1;
Desired Result: Report the dates for the first invitation and the first confirmation.
Recipient_ID FIRST_INVITE FIRST_CONFIRMATION
1 JAN-01-2020 JAN-10-2020
Here you go, as per your wording, not your result (as you first sent, but you show last sent):
with mailings as (
select 1 as recipient_id, 'INVITE' as email_type, to_date('JAN-01-2020','MON-DD-YYYY') as email_sent from dual union all
select 1 , 'INVITE' , to_date('JAN-02-2020','MON-DD-YYYY') from dual union all
select 1 , 'INVITE' , to_date('JAN-03-2020','MON-DD-YYYY') from dual union all
select 1 , 'CONFIRM', to_date('JAN-10-2020','MON-DD-YYYY') from dual union all
select 1 , 'CONFIRM', to_date('JAN-11-2020','MON-DD-YYYY') from dual
)
select recipient_id,
min(case when email_type='INVITE' then email_sent else null end) sent,
min(case when email_type='CONFIRM' then email_sent else null end) confirmed
from mailings
group by recipient_id;
RECIPIENT_ID SENT CONFIRMED
1 01-JAN-20 10-JAN-20

retrieve row from multiple row of table in oracle

I want to retrieve data from three table
for example
Table_1 : NAME_A
PD_ID
A
B
C
Table_2 : NAME_B Primary_key PD_ID,EV_N
PD_ID EV_N EV_DEC
A 1 one
A 2 Two
B 1 one
B 2 Two
B 3 THREE
C 1 one
C 2 Two
Table_3 : NAME_C Primary key PD_ID
PD_ID, FFT_NAME, FFT_DESC
A XY XY_DESC
B ZY ZY_DESC
B XY XY_DESC
C ZY ZY_DESC
C XY XY_DESC
C PY PY_DESC
Required Output
PD_ID EV_N EV_DEC FFT_NAME FFT_DESC
A 1 ONE XY XY_DESC
A 2 TWO
B 1 ONE ZY ZY_DESC
B 2 TWO XY XY_DESC
B 3 THREE
C 1 ONE ZY ZY_DESC
C 2 Two XY XY_DESC
PY PY_DESC
The idea is to range records in both tables and then use this range numbers in a full outer join:
with
t1 as (
select 'A' pd_id from dual union all
select 'B' pd_id from dual union all
select 'C' pd_id from dual
),
t2 as (
select 'A' pd_id, 1 EV_N, 'one' EV_DEC from dual union all
select 'A' pd_id, 2 EV_N, 'two' EV_DEC from dual union all
select 'B' pd_id, 1 EV_N, 'one' EV_DEC from dual union all
select 'B' pd_id, 2 EV_N, 'two' EV_DEC from dual union all
select 'B' pd_id, 3 EV_N, 'three' EV_DEC from dual union all
select 'C' pd_id, 1 EV_N, 'one' EV_DEC from dual union all
select 'C' pd_id, 2 EV_N, 'two' EV_DEC from dual
),
t3 as (
select 'A' pd_id, 'XY' FFT_NAME, 'XY_DESC' FFT_DESC from dual union all
select 'B' pd_id, 'ZY' FFT_NAME, 'ZY_DESC' FFT_DESC from dual union all
select 'B' pd_id, 'XY' FFT_NAME, 'XY_DESC' FFT_DESC from dual union all
select 'C' pd_id, 'ZY' FFT_NAME, 'ZY_DESC' FFT_DESC from dual union all
select 'C' pd_id, 'XY' FFT_NAME, 'XY_DESC' FFT_DESC from dual union all
select 'C' pd_id, 'PY' FFT_NAME, 'PY_DESC' FFT_DESC from dual
)
select coalesce(t22.pd_id,t33.pd_id) pd_id,
t22.ev_dec,
t33.FFT_NAME,
t33.FFT_DESC
from (
select pd_id, ev_n, ev_dec, row_number() over (partition by pd_id order by ev_n, ev_dec) rn
from t2
) t22
full join (
select pd_id, FFT_NAME, FFT_DESC, row_number() over (partition by pd_id order by FFT_NAME, FFT_DESC) rn
from t3
) t33
on t22.pd_id = t33.pd_id
and t22.rn = t33.rn
This won't produce the exact output you specify but it will produce a consistent and predictable output:
select t1.PD_ID
, t2.EV_N
, t2.EV_DEC
, t2.FFT_NAME
, t2.FFT_DESC
from name_a t1
cross join ( select coalesce(b.p_id, c.p_id) as p_id
, b.ev_n
, upper(b.ev_dec) as ev_dec
, c.fft_name
, c.fft_desc
from ( select * from name_b ) b
full outer join
( select c.*
, row_number() over (partition by c.p_id
order by c.fft_name) as rn
from name_c c) c
on c.p_id = b._pid
and c.rn = b.ev_n) t2
where t1.p_id = t2.p_id
order by t1.p_id
, t2.ev_n nulls last

I need the count of values

I have data in my table in oracle like below
A_CODE B_M P_id
------ ---- ------
123 A 1
123 A 2
123 B 5
678 B 3
678 C 3
678 B 4
123 BC 2
The value "BC" is B and C. The data is not normalized so we need to count it as B and C. I need the counts to be displayed as below per A_CODE
A_CODE B_M COUNT
------- ---- -------
123 A 2
123 B 2
123 C 1
678 B 2
678 C 1
How can i do this in Oracle?
You should use CONNECT BY and CONNECT_BY_ROOT.
I hope this helps:
SELECT A_CODE, B_M, COUNT (*)
FROM ( SELECT A_CODE, SUBSTR (CONNECT_BY_ROOT (B_M), LEVEL, 1) B_M
FROM your_table
CONNECT BY LEVEL <= LENGTH (B_M))
WHERE B_M IS NOT NULL
GROUP BY A_CODE, B_M
ORDER BY A_CODE;
Please try below:
SELECT A_CODE, B_M, COUNT(*) "COUNT" FROM
(SELECT A_CODE, B_M
FROM
(SELECT A_CODE,
SUBSTR(B_M,x.LVL,1) B_M
FROM my_table t,
(SELECT LEVEL LVL FROM dual
CONNECT BY LEVEL <=
(SELECT MAX(LENGTH(B_M)) FROM my_table)
) x
WHERE t.B_M is not null
)
WHERE B_M IS NOT NULL
UNION ALL
SELECT A_CODE, B_M FROM my_table WHERE B_M IS NULL
)
GROUP BY A_CODE,
B_M
ORDER BY A_CODE, B_M;
One option is to join this table to a data set that provides you with the normalised structure you need.
with cte_normaliser as (
select 'A' B_M, 'A' 'B_M_norm from dual union all
select 'B' B_M, 'B' 'B_M_norm from dual union all
select 'C' B_M, 'C' 'B_M_norm from dual union all
select 'BC' B_M, 'B' 'B_M_norm from dual union all
select 'BC' B_M, 'C' 'B_M_norm from dual)
select my_table.A_CODE,
n.B_M_norm,
count(*)
from my_table join
cte_normaliser n on n.B_M = my_table.B_M
group by my_table.A_CODE,
n.B_M_norm;
Using a fixed data set like that might not be feasible if you have a large number of variable code combination, though, and that data set might need to be built dynamically.
Besides having my_table, you create a table with all possible values. That is:
P_VAL
-----
A
B
C
Then, you should be able to obtain the count of occurrences with a join. Something like:
with my_table
as ( select '123' a_code, 'A' b_m, '1' p_id from dual
union select '123' a_code, 'A' b_m, '2' p_id from dual
union select '123' a_code, 'B' b_m, '5' p_id from dual
union select '678' a_code, 'B' b_m, '3' p_id from dual
union select '678' a_code, 'C' b_m, '3' p_id from dual
union select '678' a_code, 'B' b_m, '4' p_id from dual
union select '123' a_code, 'BC' b_m, '2' p_id from dual)
,possible_values
as ( select 'A' p_val from dual
union select 'B' p_val from dual
union select 'C' p_val from dual)
select a_code
,p_val b_m
,count('X') count
from my_table
join possible_values
on instr(b_m,p_val) > 0
group by a_code,p_val
order by a_code,p_val;

How to count consecutive duplicates in a table?

I have below question:
Want to find the consecutive duplicates
SLNO NAME PG
1 A1 NO
2 A2 YES
3 A3 NO
4 A4 YES
6 A5 YES
7 A6 YES
8 A7 YES
9 A8 YES
10 A9 YES
11 A10 NO
12 A11 YES
13 A12 NO
14 A14 NO
We will consider the value of PG column and I need the output as 6 which is the count of maximum consecutive duplicates.
It can be done with Tabibitosan method. Run this, to understand it:
with a as(
select 1 slno, 'A' pg from dual union all
select 2 slno, 'A' pg from dual union all
select 3 slno, 'B' pg from dual union all
select 4 slno, 'A' pg from dual union all
select 5 slno, 'A' pg from dual union all
select 6 slno, 'A' pg from dual
)
select slno, pg, newgrp, sum(newgrp) over (order by slno) grp
from(
select slno,
pg,
case when pg <> nvl(lag(pg) over (order by slno),1) then 1 else 0 end newgrp
from a
);
Newgrp means a new group is found.
Result:
SLNO PG NEWGRP GRP
1 A 1 1
2 A 0 1
3 B 1 2
4 A 1 3
5 A 0 3
6 A 0 3
Now, just use a group by with count, to find the group with maximum number of occurrences:
with a as(
select 1 slno, 'A' pg from dual union all
select 2 slno, 'A' pg from dual union all
select 3 slno, 'B' pg from dual union all
select 4 slno, 'A' pg from dual union all
select 5 slno, 'A' pg from dual union all
select 6 slno, 'A' pg from dual
),
b as(
select slno, pg, newgrp, sum(newgrp) over (order by slno) grp
from(
select slno, pg, case when pg <> nvl(lag(pg) over (order by slno),1) then 1 else 0 end newgrp
from a
)
)
select max(cnt)
from (
select grp, count(*) cnt
from b
group by grp
);
with test as (
select 1 slno,'A1' name ,'NO' pg from dual union all
select 2,'A2','YES' from dual union all
select 3,'A3','NO' from dual union all
select 4,'A4','YES' from dual union all
select 6,'A5','YES' from dual union all
select 7,'A6','YES' from dual union all
select 8,'A7','YES' from dual union all
select 9,'A8','YES' from dual union all
select 10,'A9','YES' from dual union all
select 11,'A10','NO' from dual union all
select 12,'A11','YES' from dual union all
select 13,'A12','NO' from dual union all
select 14,'A14','NO' from dual),
consecutive as (select row_number() over(order by slno) rr, x.*
from test x)
select x.* from Consecutive x
left join Consecutive y on x.rr = y.rr+1 and x.pg = y.pg
where y.rr is not null
order by x.slno
And you can control output with condition in where.
where y.rr is not null query returns duplicates
where y.rr is null query returns "distinct" values.
Just for completeness, here's the actual Tabibitosan method:
with sample_data as (select 1 slno, 'A1' name, 'NO' pg from dual union all
select 2 slno, 'A2' name, 'YES' pg from dual union all
select 3 slno, 'A3' name, 'NO' pg from dual union all
select 4 slno, 'A4' name, 'YES' pg from dual union all
select 6 slno, 'A5' name, 'YES' pg from dual union all
select 7 slno, 'A6' name, 'YES' pg from dual union all
select 8 slno, 'A7' name, 'YES' pg from dual union all
select 9 slno, 'A8' name, 'YES' pg from dual union all
select 10 slno, 'A9' name, 'YES' pg from dual union all
select 11 slno, 'A10' name, 'NO' pg from dual union all
select 12 slno, 'A11' name, 'YES' pg from dual union all
select 13 slno, 'A12' name, 'NO' pg from dual union all
select 14 slno, 'A14' name, 'NO' pg from dual)
-- end of mimicking a table called "sample_data" containing your data; see SQL below:
select max(cnt) max_pg_in_queue
from (select count(*) cnt
from (select slno,
name,
pg,
row_number() over (order by slno)
- row_number() over (partition by pg
order by slno) grp
from sample_data)
where pg = 'YES'
group by grp);
MAX_PG_IN_QUEUE
---------------
6
SELECT MAX(consecutives) -- Block 1
FROM (
SELECT t1.pg, t1.slno, COUNT(*) AS consecutives -- Block 2
FROM test t1 INNER JOIN test t2 ON t1.pg = t2.pg
WHERE t1.slno <= t2.slno
AND NOT EXISTS (
SELECT * -- Block 3
FROM test t3
WHERE t3.slno > t1.slno
AND t3.slno < t2.slno
AND t3.pg != t1.pg
)
GROUP BY t1.pg, t1.slno
);
The query calculates the result in following way:
Extract all couples of records that don't have a record with different value of PG in between (blocks 2 and 3)
Group them by PG value and starting SLNO value -> this counts the consecutive values for any [PG, (starting) SLNO] couple (block 2);
Extract Maximum value from query 2 (block 1)
Note that the query may be simplified if the slno field in table contains consecutive values, but this seems not your case (in your example record with SLNO = 5 is missing)
Only requiring a single aggregation query and no joins (the rest of the calculation can be done with ROW_NUMBER, LAG and LAST_VALUE):
SELECT MAX( num_before_in_queue ) AS max_sequential_in_queue
FROM (
SELECT rn - LAST_VALUE( has_changed ) IGNORE NULL OVER ( ORDER BY ROWNUM ) + 1
AS num_before_in_queue
FROM (
SELECT pg,
ROW_NUMBER() OVER ( ORDER BY slno ) AS rn,
CASE pg WHEN LAG( pg ) OVER ( ORDER BY slno )
THEN NULL
ELSE ROW_NUMBER() OVER ( ORDER BY sl_no )
END AS change
FROM table_name
)
WHERE pg = 'Y'
);
Try to use row_number()
select
SLNO,
Name,
PG,
row_number() over (partition by PG order by PG) as 'Consecutive'
from
<table>
order by
SLNO,
NAME,
PG
This is should work with minor tweaking.
--EDIT--
Sorry, partiton by PG.
The partitioning tells the row_number when to start a new sequence.

How to Pivot Without Aggregation, Is that possible?

I want to rearrange the table with rows and columns in the following order. Is this possible and how?
This is the outcome i expect. How can I do this.
If your have multiple value for each row then what you expect without aggregation function?
And if you have a single value per each row, so it's no matter that use sum,min,max or avg aggregation function.
like this(in oracle):
select row_id, 'C1', 'C2', 'C3'
from table
pivot(max("value") for column_id in('C1', 'C2','C3'));
executable version with sample:
select row_id, 'C1', 'C2', 'C3'
from (select 1 table_id, 'C1' Column_id, 1 Row_id, 20000 "value" from dual union all
select 1 table_id, 'C2' Column_id, 1 Row_id, 30000 "value" from dual union all
select 1 table_id, 'C3' Column_id, 1 Row_id, 25000 "value" from dual union all
select 1 table_id, 'C1' Column_id, 2 Row_id, 80200 "value" from dual union all
select 1 table_id, 'C2' Column_id, 2 Row_id, 50000 "value" from dual union all
select 1 table_id, 'C3' Column_id, 2 Row_id, 95000 "value" from dual)
pivot(max("value") for column_id in('C1', 'C2','C3'))
Select Row_ID,
Min(Case DBColumnName When 'c1' Then Value End) c1,
Min(Case DBColumnName When 'c2' Then Value End) c2,
Min(Case DBColumnName When 'c3' Then Value End) c3
From table
Group By Row_ID
Edit: I have written this without an editor & have not run the SQL. I hope, you get the idea.