extract substring using pl sql - sql

I have a table T1 with below data
Sno Ns_NAME Mode stat
1 AF_rtf_Nd_1 Manual 2
2 AF_rtf_Nd_2 Manual 3
3 AF_rtf_Nd_2i Manual 2
4 AF_rtf_Nd_3 Auto 2
5 AF_rtf_Nd_3i Auto 3
I need to perform below,
check if it is manual, fetch from Ns_NAME upto last "_" and check for duplicates. In this case there is 1 duplicate. Obtain average stat [(2+3)/2] of those two rows and pump into another table T2.
Output:
T2
AF_rtf_Nd Manual 2.5
I tried using substr function and used etract . But it is not fetching the correct result.

In order to look at 'Manual' records only, use a WHERE clause. Then aggregate over the substring. You get the substring with INSTR plus SUBSTR or with REGEX_REPLACE. Then only keep duplicates by using HAVING COUNT(*) > 1.
insert into t2
select
min(sno),
regexp_replace(ns_name, '_[^_]*$', ''),
'Manual',
avg(stat)
from t1
where mode = 'Manual'
group by regexp_replace(ns_name, '_[^_]*$', '')
having count(*) > 1;
The equivalent to the REGEXP_REPLACE with INSTR and SUBSTR is
substr(ns_name, 1, instr(ns_name, '_', -1) - 1)
(Only difference is when there is no '_' in the string at all.)

Related

Get 2nd from string in SQL

I have a requirement in SQL where I am getting a string and need to get the top 2nd row.
I am using SQL version 2014
I have a string which I am to spitting using the character 'M'
string is as follows: 3908K88513K1992K898593M
if I pass input: 88513 I need to get the output as 898593
Another example is
string: 24572K12345K10981K19809K
if the input is 19809 output should be: 12345
I have tried below code but it is failing at 2nd input example string:
SELECT TOP 1 T.*
FROM (
SELECT TOP 2 *
FROM splitfunction((
SELECT number
FROM name
WHERE id = 100709
), 'K')
) AS T
ORDER BY 1 DESC;
As Jarlh mentioned in the comments, instead of using TOP, use OFFSET and FETCH:
SELECT ss.* --This should be your columns
FROM dbo.contact c
CROSS APPLY dbo.dba_splitstring(c.structureno,'M') ss
WHERE c.recno = 100719
ORDER BY {????} --Your ORDER BY was missing in your query
OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY;
I've also reformulated your query, as all those subqueries weren't necessary.

Need to Transform a Rows of Data into a single Row

I have a set of Data in MS Access
Number Owner
1 Heelo
1 Hi
1 There
2 What
2 Up
This needs to be transferrid into
Number Owner1 Owner2 Owner3 Owner4
1 Heelo Hi There -
2 What Up - -
Any idea on how to go on with this?
The crux in this case is we don't have a third column from where we can pivot the data.
You could add a third column with a sequence of numbers:
SELECT Number, (select count(*)
from YourTable as s
where s.number = t.number) as sequence, owner
from YourTable as t
then apply this solution to the results: SQL to transpose row pairs to columns in MS ACCESS database

SQL - Select the longest substrings

I have the data like that.
AB
ABC
ABCD
ABCDE
EF
EFG
IJ
IJK
IJKL
and I just want to get ABCDE,EFG,IJKL. how can i do that oracle sql?
the size of the char are min 2 but doesn't have a fixed length, can be from 2 to 100.
In the event that you mean "longest string for each sequence of strings", the answer is a little different -- you are not guaranteed that all have a length of 4. Instead, you want to find the strings where adding a letter isn't another string.
select t.str
from table t
where not exists (select 1
from table t2
where substr(t2.str, 1, length(t.str)) = t.str and
length(t2.str) = length(t.str) + 1
);
Do note that performance of this query will not be great if you have even a moderate number of rows.
Select all rows where the string is not a substring of any other row. It's not clear if this is what you want though.
select t.str
from table t
where not exists (
select 1
from table t2
where instr(t1.str, t2.str) > 0
);

SQL Select where id is in `column`

I have a column that has multiple numbers separated by a comma. Example for a row:
`numbers`:
1,2,6,66,4,9
I want to make a query that will select the row only if the number 6 (for example) is in the column numbers.
I cant use LIKE because if there is 66 it'll work too.
You can use like. Concatenate the field separators at the beginning and end of the list and then use like. Here is the SQL Server sytnax:
where ','+numbers+',' like '%,'+'6'+',%'
SQL Server uses + for string concatenation. Other databases use || or the concat() function.
You should change your database to rather have a new table that joins numbers with the row of your current table. So if your row looks like this:
id numbers
1 1,2,6,66,4,9
You would have a new table that joins those values like so
row_id number
1 1
1 2
1 6
1 66
1 4
1 9
Then you can search for the number 6 in the number column and get the row_id

Oracle SQL query count group by timestamp substring

Given a table that has a column of string "timestamps" (yyyyMMddHHmmssSSS format), I want to substring the first 8 characters, and get a count of how many rows have that substring, grouping the results.
Sample data...
TIMESTAMP
20100802123456123
20100803123456123
20100803123456123
20100803123456123
20100804123456123
20100805123456123
20100805123456123
20100805123456123
20100805123456123
20100806123456123
20100807123456123
20100807123456123
...and expected results...
SUBSTRING, COUNT
20100802, 1
20100803, 3
20100804, 1
20100805, 4
20100806, 1
20100807, 2
I know this should be easy, but I'm not having any luck at the moment.
I don't have a database to test with, but it seems like you are looking for
select
substr(timestamp, 1, 8),
count(*)
from
my_table
group by
substr(timestamp, 1, 8);