I need the count of values - sql

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;

Related

SQL Query returning Null for Value present

I have a sql table which has columns like ITEM_ID, FROM_QUANTITY, TO_QUANTITY, LIST_PRICE. Now every item should have a FROM_QUANTITY from 1 to 12 and a null in FROM_QUANTITY.
Now in order to check which QUANTITY is missing from ITEM in table , I have written a SQL query which will print None if the FROM_QUANTITY is not there.
The query is working as expected. Just that each item also has a null value in quantity and has some price associated with it. But my query is Showing null as list price for it.
Here is my query
with pl as (
select DISTINCT ITEM_ID, FROM_QUANTITY,TO_QUANTITY, coalesce(to_char(list_price), 'NONE') as list_price
from PRICELIST_LINE
where item_id IN ('XYZ') and
PRICELIST_HDR_KEY in (select Pricelist_Hdr_Key
from PRICELIST_HDR
where PRICELIST_NAME IN ('ABC') and
SELLER_ORGANIZATION_CODE IN ('100')
) and SYSDATE < END_DATE_ACTIVE
)
select i.item_id, u.from_quantity, pl.to_quantity,pl.list_price
from (select distinct item_id from pl) i cross join
(select '1' as from_quantity from dual union all
select '2' as from_quantity from dual union all
select '3' as from_quantity from dual union all
select '4' as from_quantity from dual union all
select '5' as from_quantity from dual union all
select '6' as from_quantity from dual union all
select '7' as from_quantity from dual union all
select '8' as from_quantity from dual union all
select '9' as from_quantity from dual union all
select '10' as from_quantity from dual union all
select '11' as from_quantity from dual union all
select '12' as from_quantity from dual union all
select '' as from_quantity from dual
) u left join
pl
on pl.item_id = i.item_id and pl.from_quantity = u.from_quantity;
Now the Output for this is like:
ITEM_ID FROM_QUANTITY TO_QUANTITY LIST_PRICE
------- ------------- ----------- ----------
ABC 1 2 100
ABC
Expected :
ITEM_ID FROM_QUANTITY TO_QUANTITY LIST_PRICE
------- ------------- ----------- ----------
ABC 1 2 100
ABC 200
Using a partition outer join in Oracle should help, e.g.:
WITH pl AS (SELECT DISTINCT item_id,
from_quantity,
to_quantity,
coalesce(to_char(list_price), 'NONE') AS list_price
FROM pricelist_line
WHERE item_id IN ('XYZ')
AND pricelist_hdr_key IN (SELECT pricelist_hdr_key
FROM pricelist_hdr
WHERE pricelist_name IN ('ABC')
AND seller_organization_code IN ('100'))
AND SYSDATE < end_date_active),
qtys AS (SELECT LEVEL from_quantity
FROM dual
CONNECT BY LEVEL <= 12
UNION ALL
SELECT NULL from_quantity
FROM dual)
SELECT pl.item_id,
q.from_quantity,
pl.to_quantity,
pl.list_price
FROM qtys q
LEFT OUTER JOIN PL PARTITION BY (pl.item_id) ON pl.from_quantity = q.from_quantity
OR (pl.from_quantity IS NULL AND q.from_quantity IS NULL);
N.B. untested.

converting comma separated value to multiple rows

I have a table like this:
ID NAME Dept_ID
1 a 2,3
2 b
3 c 1,2
Department is another table having dept_id and dept_name as columns. i want result like,
ID Name Dept_ID
1 a 2
1 a 3
2 b
3 c 1
3 c 2
any help please?
You can do it as:
--Dataset Preparation
with tab(ID, NAME,Dept_ID) as (Select 1, 'a', '2,3' from dual
UNION ALL
Select 2, 'b','' from dual
UNION ALL
Select 3, 'c' , '1,2' from dual)
--Actual Query
select distinct ID, NAME, regexp_substr(DEPT_ID,'[^,]+', 1, level)
from tab
connect by regexp_substr(DEPT_ID,'[^,]+', 1, level) is not null
order by 1;
Edit:
based on which column i need to join? in one table i have comma
separated ids and in other table i have just ids
with tab(ID, NAME,Dept_ID) as (Select 1, 'a', '2,3' from dual
UNION ALL
Select 2, 'b','' from dual
UNION ALL
Select 3, 'c' , '1,2' from dual) ,
--Table Dept
tbl_dept (dep_id,depname) as ( Select 1,'depa' from dual
UNION ALL
Select 2,'depb' from dual
UNION ALL
Select 3,'depc' from dual
) ,
--Seperating col values for join. Start your query from here using with clause since you already have the two tables.
tab_1 as (select distinct ID, NAME, regexp_substr(DEPT_ID,'[^,]+', 1, level) col3
from tab
connect by regexp_substr(DEPT_ID,'[^,]+', 1, level) is not null
order by 1)
--Joining table.
Select t.id,t.name,t.col3,dt.depname
from tab_1 t
left outer join tbl_dept dt
on t.col3 = dt.dep_id
order by 1
with tmp_tbl as(
select
1 ID,
'a' NAME,
'2,3' DEPT_ID
from dual
union all
select
2 ID,
'b' NAME,
'' DEPT_ID
from dual
union all
select
3 ID,
'c' NAME,
'1,2' DEPT_ID
from dual)
select
tmp_out.ID,
tmp_out.NAME,
trim(tmp_out.DEPT_ID_splited)
from(
select
tmp.ID,
tmp.NAME,
regexp_substr(tmp.DEPT_ID,'[^,]+', 1, level) DEPT_ID_splited
from
tmp_tbl tmp
connect by
regexp_substr(tmp.DEPT_ID,'[^,]+', 1, level) is not null) tmp_out
group by
tmp_out.ID,
tmp_out.NAME,
tmp_out.DEPT_ID_splited
order by
tmp_out.ID,
tmp_out.DEPT_ID_splited

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

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.

Find count value for a different group with reference with single column (Oracle)

I have the below scenario data. I need a count for column 'c1' with different set of data. total count should be based on unique no of data from column c1 and e1.
with t as
(
select 'cab1' as c1, 'ae1' as e1 from dual
union all
select 'cab1' , 'ae2' from dual
union all
select 'cab1' , 'ae3' from dual
union all
select 'cab1' , 'ae4' from dual
union all
select 'cab3' , 'ae1' from dual
union all
select 'cab3' , 'ae1' from dual
union all
select 'cab2' , 'ae' from dual
)
select
c1,e1, COUNT(*) OVER (partition by c1 order by c1,e1 ) as p1
from t;
my result should be
c1 e1 count
-----------------------
cab1 ae3 4
cab1 ae2 4
cab1 ae1 4
cab1 ae4 4
cab2 ae 1
cab3 ae1 1
Can anyone help on this.
SqlFiddleDemo
with t as
(
select 'cab1' as c1, 'ae1' as e1 from dual
union all
select 'cab1' , 'ae2' from dual
union all
select 'cab1' , 'ae3' from dual
union all
select 'cab1' , 'ae4' from dual
union all
select 'cab3' , 'ae1' from dual
union all
select 'cab3' , 'ae1' from dual
union all
select 'cab2' , 'ae' from dual
)
SELECT
c1,
e1,
COUNT(*) OVER (partition by c1) as p1
FROM t
GROUP BY c1, e1