Oracle Select Query - Select columns based on column start text - sql

I have a table which has got a field which has many "Item numbers" (starting from 8%, 2%, etc...). These Item numbers have further components, which are also stored in the very same column (because each component is also an Item). Now the requirement is to if I encounter an Item number that has its component number starting from 8%, then I need to create a new column against the Main Item number (some thing which is called 800-Level Items) and store it there, and if the main Item Number has 2% component number then i should put it into yet another column something like (200-Level).
Field1 XLike YLike
---------------------
(Main Item) XXXX
(Main Item's component)YYYY
(Main Item's component)ZZZZ
(Main Item's component)XABC
(Main Item's component)YABC
Now depending upon the starting character of the Sting in Field1, I need to segregate the values in fields XLike and YLike in following manner:
Field1 | XLike | YLike
-------------------------
XXXX | XABC |
YYYY | | YABC
ZZZZ | |
XABC | |
YABC | |

for 10g you can try
select
wm_concat(case when field1 like '%X%' then field1 end) XLike,
wm_concat(case when field1 like '%Y%' then field1 end) YLike
from table1
11g has listagg function
select listagg(case when field1 like '%X%' then field1 end, ';') within group(order by field1) XLike,
listagg(case when field1 like '%Y%' then field1 end, ';') within group(order by field1) YLike
from table1

Related

How to check if two field match in SPLUNK

number1= AnyNumber from 1 to 100
number2= AnyNumber from 1 to 100,
This is how my data looks in Splunk
{[-]
field1: number1,
fiedl2: number2,
...
}
I want to check if these two fields match or doesn't,
my Splunk Query
| search filed1 != field2
| stats count by field1,field2
Try using where with match:
<spl>
| where !match(field1,field2)
| stats count by field1 field 2
After adding the single quotes around the field2. I was able to get the data
for the list of fields that are not matching!
| where field1 != 'field2' | stats count by field1, field2

Data field - search and write value in new data field (Oracle)

Sorry, I don't know how to describe that as a title.
With a query (example: Select SELECT PKEY, TRUNC (CREATEDFORMAT), STATISTICS FROM BUSINESS_DATA WHERE STATISTICS LIKE '% business_%'), I can display all data that contains the value "business_xxxxxx".
For example, the data field can have the following content: c01_ad; concierge_beendet; business_start; or also skill_my; pre_initial_markt; business_request; topIntMaster; concierge_start; c01_start;
Is it now possible in a temp-only output the corresponding value in another column?
So the output looks like this, for example?
PKEY | TRUNC(CREATEDFORMAT) | NEW_STATISTICS
1 | 13.06.2020 | business_start
2 | 14.06.2020 | business_request
That means removing everything that does not start with business_xxx? Is this possible in an SQL query? RegEx would not be the right one, I think.
I think you want:
select
pkey,
trunc(createdformat) createddate,
regexp_substr(statistics, 'business_\S*') new_statistics
from business_data
where statistics like '% business_%'
You can also use the following regexp_substr:
SQL> select regexp_substr(str,'business_[^;]+') as result
2 from
3 --sample data
4 (select 'skill_my; pre_initial_markt; business_request; topIntMaster; concierge_start; c01_start;' as str from dual
5 union all
6 select 'c01_ad; concierge_beendet; business_start;' from dual);
RESULT
--------------------------------------------------------------------------------
business_request
business_start
SQL>

SQL LIKE using the same row value

I'm wondering how can I use a row value as a variable for my like statement? For example
ID | PID | DESCRIPTION
1 | 4124 | Hi4124
2 | 2451 | Test
3 | 1467 | Hello
4 | 9642 | Me9642
I have a table above, I want to return IDs 1 and 4 since DESCRIPTION contains PID.
I'm thinking it would be SELECT * from TABLE WHERE DESCRIPTION LIKE '%PID%' but I can't get it.
You can use CONCAT() to assemble the matching pattern, as in:
select *
from t
where description like concat('%', PID, '%')
We could also try using CHARINDEX here:
SELECT ID, PID, DESCRIPTION
FROM yourTable
WHERE CHARINDEX(PID, DESCRIPTION) > 0;
Demo
Note that I assume in the demo that the PID column is actually text, and not a numeric column. If PID be numeric, we might have to first use a cast in order to use CHARINDEX (or any of the methods given in the other answers).
Use the CONCAT SQL function
SELECT *
FROM TABLE
WHERE DESCRIPTION LIKE CONCAT('%', PID, '%')

In proc sql when using SELECT * and GROUP BY, the result is not collapsed

When using the asterisk in combination with sum and group, the duplicates are not removed as I expect (and as it works in for example mysql):
col1 | country
-----------------
5 | sweden
20 | sweden
30 | denmark
select *, sum(col1) as s from table
group by country
the data returned is:
col1 | country | s
--------------------
5 | sweden | 25
20 | sweden | 25
30 | denmark | 30
instead of what I expected:
col1 | country | s
------------------------
5 | sweden | 25
30 | denmark | 30
If I don't use asterisk (*), the data returned is as I expect it to be.
SELECT country, sum(col1) as s from table
You are correct, SAS does not collapse WHEN you have variables in the statement that are not in the GROUP BY statement.
There will be a note to that effect in the log, about your data being merged.
If you want just the variables, you'll have to list them unfortunately, but since you have to list them in GROUP BY it's not extra work per se.
Different SQL implementations handle things differently, this is one way that SAS is different. It's handy when you do want to merge a summary stat back with the main data set though.
If you don't want this behaviour add the NOREMERGE option to your PROC SQL - but it throws an error, it still doesn't work the way you want.
See the documentation for the reference
Don't use SELECT *, ever. It's bad practice, risky, unsustainable... Read about it.
What flavor of SQL?
Your first query shouldn't work. You're basically saying...
select col1
, country
, sum(col1) as s
from table
group by country
...which will return an error:
Column 'table.col1' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
SELECT country, sum(col1) as s from table
...also should not work:
Column 'table.country' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Given your expected output, I suspect what you are looking for is...
select min(col1) as col1
, country
, sum(col1) as s
from table
group by country

How to write SQL queries with respect to the following conditions?

I have a database table in which a column tags contain values such as:
"AutoMNRP, MNRP"
"Macro, MNRP"
"AutoMNRP, Micro"
"Macro, Micro"
where "...." represents a string.
I want to write a SQL query such that it filters out all results having MNRP tag in it. How can I do this?
I tried a not like operator of SQL on it, but if I want to remove MNRP tag, it also filters out AutoMNRP tag.
At the last of query I need results featuring -
"AutoMNRP, Micro"
"Macro, Micro".
(Results when MNRP is filtered out.)
The right answer to this is to fix your design, you shouldn't store the data like this (comma separated), because your table should be like (and the duplicates should be removed and handled too)
+----------+
| Data |
+----------+
| AutoMNRP |
| MNRP |
| Macro |
| MNRP |
| AutoMNRP |
| Micro |
| Macro |
| Micro |
+----------+
But... here is a way it may fit you requirements
;WITH T(Str) AS
(
SELECT 'AutoMNRP, MNRP' UNION ALL
SELECT 'Macro, MNRP' UNION ALL
SELECT 'AutoMNRP, Micro' UNION ALL
SELECT 'Macro, Micro'
)
SELECT Str
FROM T
WHERE Str NOT LIKE '% MNRP,%'
AND
Str NOT LIKE '%, MNRP';
Returns:
+-----------------+
| Str |
+-----------------+
| AutoMNRP, Micro |
| Macro, Micro |
+-----------------+
Live Demo
You also (as Larnu point to) do as
;WITH T(Str) AS
(
SELECT 'AutoMNRP, MNRP' UNION ALL
SELECT 'Macro, MNRP' UNION ALL
SELECT 'AutoMNRP, Micro' UNION ALL
SELECT 'Macro, Micro'
)
SELECT Str
FROM T
WHERE CONCAT(', ', Str, ',') NOT LIKE '%, MNRP,';
In SQL Server 2016+ you can use the STRING_SPLIT function. So you can multiply a record by the number of separated values in the tags column so you can then apply a simple WHERE clause. Something like this:
WITH cte AS
(
SELECT Id, SingleTag
FROM table_name CROSS APPLY STRING_SPLIT(tags, ',')
)
SELECT * FROM cte WHERE SingleTag = 'MNRP'