I have an interactive report, which using Pivot, but I was trying to custom sort the report by row column of pivot "Age Group", but I couldn't.
I need this report in such this sorting:
How I can do that?
Thanks
Looks like you'd want to sort by the first digit(s), and then by age_group itself (as a string). Something like this (sample data in lines #1 - 10; query you might be interested in begins at line #11):
SQL> with test (age_group) as
2 (select 'wahine 45-64' from dual union all
3 select 'wahine 25-44' from dual union all
4 select 'rangtahi 15-24' from dual union all
5 select 'pepis 0_4' from dual union all
6 select 'pakeke 45-64' from dual union all
7 select 'pakeke 25-44' from dual union all
8 select 'N/A' from dual union all
9 select 'kuia 65+' from dual
10 )
11 select age_group
12 from test
13 order by to_number(regexp_substr(age_group, '\d+')), age_group;
AGE_GROUP
--------------
pepis 0_4
rangtahi 15-24
pakeke 25-44
wahine 25-44
pakeke 45-64
wahine 45-64
kuia 65+
N/A
8 rows selected.
SQL>
Related
finaltableA has two columns WORDS, WCOUNTS. The program should insert every word of intitaltableA into finaltableA exactly once. In the column WCOUNTS the program should put how often the word occurs.
intitaltableA
COVID
is
a
disease
COVID
can
be
treated
with
antibodies
COVID
is
a
serious
disease
there
is
a
vaccination
available
for
COVID
finaltableA should look like this -
WORDS
WCOUNTS
a
3
antibodies
1
available
1
be
1
can
1
COVID
4
disease
2
for
1
is
3
serious
1
there
1
treated
1
vaccination
1
with
1
I should use a cursor to insert and update the table. I am new to PL/SQL.
It is quite obvious that you can do this without any cursor in PLSQL, so I guess that means that you are trying to solve a homework problem or a training question. Anyway, here you can see two simple options to get the result you want, with and without PLSQL.
Option 1 - Without PLSQL
with x ( words )
as
(
select 'COVID' from dual union all
select 'is' from dual union all
select 'a' from dual union all
select 'disease' from dual union all
select 'COVID' from dual union all
select 'can' from dual union all
select 'be' from dual union all
select 'treated' from dual union all
select 'with' from dual union all
select 'antibodies' from dual union all
select 'COVID' from dual union all
select 'is' from dual union all
select 'a' from dual union all
select 'serious' from dual union all
select 'disease' from dual union all
select 'there' from dual union all
select 'is' from dual union all
select 'a' from dual union all
select 'vaccination' from dual union all
select 'available' from dual union all
select 'for' from dual union all
select 'COVID' from dual
)
select words, count(words) as counter
from x
group by words
order by 2 desc;
If you execute it, it gives you the output you want
SQL> with x ( words )
2 as
3 (
4 select 'COVID' from dual union all
5 select 'is' from dual union all
6 select 'a' from dual union all
7 select 'disease' from dual union all
8 select 'COVID' from dual union all
9 select 'can' from dual union all
10 select 'be' from dual union all
11 select 'treated' from dual union all
12 select 'with' from dual union all
13 select 'antibodies' from dual union all
14 select 'COVID' from dual union all
15 select 'is' from dual union all
16 select 'a' from dual union all
17 select 'serious' from dual union all
18 select 'disease' from dual union all
19 select 'there' from dual union all
20 select 'is' from dual union all
21 select 'a' from dual union all
22 select 'vaccination' from dual union all
23 select 'available' from dual union all
24 select 'for' from dual union all
25 select 'COVID' from dual
26 )
27 select words, count(words) as counter
28 from x
29 group by words
30* order by 2 desc
SQL> /
WORDS COUNTER
----------- ----------
COVID 4
is 3
a 3
disease 2
there 1
available 1
for 1
serious 1
antibodies 1
can 1
vaccination 1
WORDS COUNTER
----------- ----------
treated 1
be 1
with 1
14 rows selected.
Option 2 - Using PLSQL and a CURSOR
In the below example, I'm going to use dbms_output to show the results. Keep in mind that here there are dozens of ways to do the same, I am just using the simplest one I can think of.
SQL> create table t ( words varchar2(40) ) ;
Table created.
SQL> insert into t
2 with x ( words )
3 as
4 (
5 select 'COVID' from dual union all
6 select 'is' from dual union all
7 select 'a' from dual union all
8 select 'disease' from dual union all
9 select 'COVID' from dual union all
10 select 'can' from dual union all
11 select 'be' from dual union all
12 select 'treated' from dual union all
13 select 'with' from dual union all
14 select 'antibodies' from dual union all
15 select 'COVID' from dual union all
16 select 'is' from dual union all
17 select 'a' from dual union all
18 select 'serious' from dual union all
19 select 'disease' from dual union all
20 select 'there' from dual union all
21 select 'is' from dual union all
22 select 'a' from dual union all
23 select 'vaccination' from dual union all
24 select 'available' from dual union all
25 select 'for' from dual union all
26 select 'COVID' from dual
27* ) select words from x
SQL> /
22 rows created.
SQL> commit ;
Commit complete.
SQL> declare
2 begin
3 for i in ( select words, count(words) as counter from t group by words order by 2 desc )
4 loop
5 dbms_output.put_line( 'Words: '||i.words||' - Wcount is: '||i.counter||' ');
6 end loop;
7* end;
SQL> /
Words: COVID - Wcount is: 4
Words: is - Wcount is: 3
Words: a - Wcount is: 3
Words: disease - Wcount is: 2
Words: antibodies - Wcount is: 1
Words: for - Wcount is: 1
Words: be - Wcount is: 1
Words: vaccination - Wcount is: 1
Words: can - Wcount is: 1
Words: available - Wcount is: 1
Words: treated - Wcount is: 1
Words: there - Wcount is: 1
Words: with - Wcount is: 1
Words: serious - Wcount is: 1
PL/SQL procedure successfully completed.
I have an Oracle stored procedure that takes input parameters as two date ranges.
e.g.
sp_periodic_data(p_from_date DATE, p_to_date DATE) // let's take p_from_date = 01-Jan-2021 and p_to_date = '03-31-2021'
I need to pick the latest record for each month from the table and add its corresponding value for the time period.
Table Value :
ID
Date
value
1
1-jan-2021
10
1
10-jan-2021
20
2
15-jan-2021
15
2
16-jan-2021
20
2
02-feb-2021
10
2
06-feb-2021
15
1
17-feb-2021
10
1
5-mar-2021
15
1
17-mar-2021
10
2
10-mar-2021
10
Expected output: Need to add the latest record (latest date) for each ID for every month between Jan to March
40 --> for ID 1 (20+10+10)
45 --> for ID 2 (20+15+10)
for a start :
SQL for Beginners
Aggregate Functions
Analytical SQL in Oracle Database 12c
example:
with
list_dates(id,dates,value) as
(
select 1,'1-jan-2021',10 from dual union all
select 1,'10-jan-2021',20 from dual union all
select 2,'15-jan-2021',15 from dual union all
select 2,'16-jan-2021',20 from dual union all
select 2,'02-feb-2021',10 from dual union all
select 2,'06-feb-2021',15 from dual union all
select 1,'17-feb-2021',10 from dual union all
select 1,'5-mar-2021',15 from dual union all
select 1,'17-mar-2021',10 from dual union all
select 2,'10-mar-2021',10 from dual
)
,step1 as (
select
id, trunc(to_date(dates,'dd-mon-yyyy'),'mm') mm,max(value) keep(dense_rank last order by to_date(dates,'dd-mon-yyyy')) value
from list_dates
group by id ,trunc(to_date(dates,'dd-mon-yyyy'),'mm')
)
select id,sum(value) val from step1
group by id;
I wrote query
select s_id from emp
where s_inv=12
i got results in this manner
1
2
3
4
5
but i want it in this format
1 2 3 4 5
If you need your result in a single column, you can use LISTAGG:
with emp(s_id, s_inv) as
(
select 1, 12 from dual union all
select 2, 12 from dual union all
select 3, 12 from dual union all
select 4, 12 from dual union all
select 5, 12 from dual
)
select listagg(s_id, ' ') within group (order by s_id)
from emp
where s_inv = 12
If you need to build many columns on the same row, you should first define how many columns will your result have
In oracle I have a table teststring and column name is STRINGVALUE and values in the column are :
A1CC
A2BB
C1DD
C2CC
ABA28
1B333
AB345
1A222
2NDDD
I have to select only those value which has first 2 values alphanumeric like A1CC,A2BB,1B33,2NDDD etc
SELECT stringvalue FROM teststring
WHERE regexp_like(substr(stringvalue,1,2),'[A-Z][0-9]|[0-9][A-Z]');
or
SELECT stringvalue FROM teststring
WHERE regexp_like(substr(stringvalue,1,2),'[[:alpha:]][[:digit:]]|[[:digit:]][[:alpha:]]');
select only those value which has first 2 values alphanumeric
Your example output doesn't satisfy the rule you mentioned. According to your rule, this is the test case :
SQL> WITH DATA AS(
2 SELECT 'A1CC' STR FROM DUAL UNION ALL
3 SELECT 'A2BB' STR FROM DUAL UNION ALL
4 SELECT 'C1DD' str from dual union all
5 SELECT 'C2CC' STR FROM DUAL UNION ALL
6 SELECT 'ABA28' str from dual union all
7 SELECT '1B333' STR FROM DUAL UNION ALL
8 SELECT 'AB345' STR FROM DUAL UNION ALL
9 SELECT '1A222' STR FROM DUAL UNION ALL
10 SELECT '2NDDD' FROM DUAL)
11 SELECT STR
12 FROM DATA
13 WHERE (REGEXP_LIKE(SUBSTR(STR,1,1),'[[:alpha:]]')
14 AND REGEXP_LIKE(SUBSTR(STR,2,1),'[[:digit:]]'))
15 OR (REGEXP_LIKE(SUBSTR(STR,1,1),'[[:digit:]]')
16 AND REGEXP_LIKE(SUBSTR(STR,2,1),'[[:alpha:]]'))
17 /
STR
-----
A1CC
A2BB
C1DD
C2CC
1B333
1A222
2NDDD
7 rows selected.
SQL>
Thanks Lalit, when i was using regular expression i was missing the keyword digit thats why i was unable to get the right result set.
in my requirement i have more than thousand value in that column so simply i have called the calum name from the table instead of using the dual union and its working fine..
the new query where any number of record available in the column below format would be feasible.
WITH DATA AS(SELECT STRINGVALUE as STR FROM TESTSTRING )
SELECT STR
FROM DATA
WHERE (REGEXP_LIKE(SUBSTR(STR,1,1),'[[:alpha:]]')
AND REGEXP_LIKE(SUBSTR(STR,2,1),'[[:digit:]]'))
OR (REGEXP_LIKE(SUBSTR(STR,1,1),'[[:digit:]]')
AND REGEXP_LIKE(SUBSTR(STR,2,1),'[[:alpha:]]'))
Use this Query
with TAB(TXT) as(
select 'A1CC' from dual union all
select 'A2BB' from dual union all
select 'C1DD' from dual union all
select 'C2CC' from dual union all
select 'ABA28' from dual union all
select '1B333' from dual union all
select 'AB345' from dual union all
select '1A222' from dual union all
select '2NDDD' from dual)
----------------
--- End of Data
----------------
select txt
from tab
where (regexp_like(TXT, '^((\w\d)|(\d\w)){1}\w+'));
To get output like:
| TXT |
|-------|
| A1CC |
| A2BB |
| C1DD |
| C2CC |
| 1B333 |
| 1A222 |
| 2NDDD |
Data in the file_name field of the generation table should be an assigned number, then _01, _02, or _03, etc. and then .pdf (example 82617_01.pdf).
Somewhere, the program is putting a state name and sometimes a date/time stamp, between the assigned number and the 01, 02, etc. (82617_ALABAMA_01.pdf or 19998_MAINE_07-31-2010_11-05-59_AM.pdf or 5485325_OREGON_01.pdf for example).
We would like to develop a SQL statement to find the bad file names and fix them. In theory it seems rather simple to find file names that include a varchar2 data type and remove it, but putting the statement together is beyond me.
Any help or suggestions appreciated.
Something like:
UPDATE GENERATION
SET FILE_NAME (?)
WHERE FILE_NAME (?...LIKE '%STRING%');?
You can find the problem rows like this:
select *
from Files
where length(FILE_NAME) - length(replace(FILE_NAME, '_', '')) > 1
You can fix them like this:
update Files
set FILE_NAME = SUBSTR(FILE_NAME, 1, instr(FILE_NAME, '_') -1) ||
SUBSTR(FILE_NAME, instr(FILE_NAME, '_', 1, 2))
where length(FILE_NAME) - length(replace(FILE_NAME, '_', '')) > 1
SQL Fiddle Example
You can also use Regexp_replace function:
SQL> with t1(col) as(
2 select '82617_mm_01.pdf' from dual union all
3 select '456546_khkjh_89kjh_67_01.pdf' from dual union all
4 select '19998_MAINE_07-31-2010_11-05-59_AM.pdf' from dual union all
5 select '5485325_OREGON_01.pdf' from dual
6 )
7 select col
8 , regexp_replace(col, '^([0-9]+)_(.*)_(\d{2}\.pdf)$', '\1_\3') res
9 from t1;
COL RES
-------------------------------------- -----------------------------------------
82617_mm_01.pdf 82617_01.pdf
456546_khkjh_89kjh_67_01.pdf 456546_01.pdf
19998_MAINE_07-31-2010_11-05-59_AM.pdf 19998_MAINE_07-31-2010_11-05-59_AM.pdf
5485325_OREGON_01.pdf 5485325_01.pdf
To display good or bad data regexp_like function will come in handy:
SQL> with t1(col) as(
2 select '826170_01.pdf' from dual union all
3 select '456546_01.pdf' from dual union all
4 select '19998_MAINE_07-31-2010_11-05-59_AM.pdf' from dual union all
5 select '5485325_OREGON_01.pdf' from dual
6 )
7 select col bad_data
8 from t1
9 where not regexp_like(col, '^[0-9]+_\d{2}\.pdf$');
BAD_DATA
--------------------------------------
19998_MAINE_07-31-2010_11-05-59_AM.pdf
5485325_OREGON_01.pdf
SQL> with t1(col) as(
2 select '826170_01.pdf' from dual union all
3 select '456546_01.pdf' from dual union all
4 select '19998_MAINE_07-31-2010_11-05-59_AM.pdf' from dual union all
5 select '5485325_OREGON_01.pdf' from dual
6 )
7 select col good_data
8 from t1
9 where regexp_like(col, '^[0-9]+_\d{2}\.pdf$');
GOOD_DATA
--------------------------------------
826170_01.pdf
456546_01.pdf
To that end your update statement might look like this:
update your_table
set col = regexp_replace(col, '^([0-9]+)_(.*)_(\d{2}\.pdf)$', '\1_\3');
--where clause if needed