Convert SQL to REGEX to Data Studio - sql

I am new to working in Google Data Studio. I am trying to do a case when on some data but all I have is the SQL.
Here are the examples of what I am working with. I know this is not the best way to create this example. I normally use work with making queries so these examples are new to me.
CREATE TABLE #botdata(botstatus varchar(255))
insert into #botdata (botstatus)
values ('0|3900000:3900037:3900999:BOT-ANOMALY-HEADER|undefined|false')
insert into #botdata (botstatus)
values ('1|3900037|undefined|false')
insert into #botdata (botstatus)
values ('0|3900005:3900024|undefined|')
I am making the above into groups in SQL using a query but I need this same query to be formatted in Google Data Studio and I am not sure how
select case when botstatus like '1|%' OR (botstatus like '0|%' and botstatus like '%BOT-%') then 'BOT'
when botstatus like '0|%' then 'Regular Visit' else 'Regular Visit NO BOT STATUS' end as updatedbotstatus, botstatus
from #botdata
Having issues since the '|' and '-' are special charters in data studio.
Tried this but not getting the results I need.
CASE
WHEN REGEXP_MATCH( Bot Status(11) ,'0\\|*') and REGEXP_MATCH(Bot Status(11),'*BOT\\-*') then 'Bot'
ELSE 'Other'
END

Please test it out and lemme know if it needs any revision.
CASE WHEN REGEXP_CONTAINS(regex,'1\\|') OR (REGEXP_CONTAINS(regex,'0\\|') AND REGEXP_CONTAINS(regex,'BOT-')) THEN 'BOT' WHEN REGEXP_CONTAINS(regex,'0\\|') THEN 'REGULAR VISIT' END
-

Related

Getting syntax error in #PROMPT Oracle SQL Developer?

SAP Bo has generated a SQL query for a report which is not working in Oracle SQL Developer. I am getting 'missing expression' error.
Below is the code, please help me finding out the error here.
and also help me to understand the role of #PROMPT here.
SELECT
CASE
WHEN MATERIAL IN #PROMPT('Enter materials which may not be shipped to SIMS:','C',,Multi,Free,Persistent) OR material IN #PROMPT'Enter materials which may not be shipped to SIMS(2):','C',,Multi,Free,Persistent)
THEN 'Not SIMS'
WHEN
material IN #PROMPT('Enter materials which can be shipped to SIMS:','C',,Multi,Free,Persistent)
THEN 'SIMS'
END
FROM
Materials;
Expecting query should work as the same is working in SAP BO.
Try it like this:
SELECT
CASE
WHEN MATERIAL IN(&Enter_NON_ShippableMaterials_SIMS) OR MATERIAL IN(&Enter_NON_ShippableMaterials_SIMS_2) THEN 'Not SIMS'
WHEN MATERIAL IN(&Enter_SIMS_ShippableMaterials) THEN 'SIMS'
END "SIMS_OR_NOT"
FROM
You will be prompted to input data 3 times. If data are of type VarChar2 and you need to enter a list of data as input then do it within single quotes separated by comma - 'MAT_1', 'MAT_2', ....
Regards...

Newly created column not registering in Case statement - 'Object not found'

I am combining a group of columns to create the specs per each line item. I then will compare these specs to the specs from an excel sheet that have an ID with them. So in the end the ID will be associated with the correct items in the database. To do this, I create a new column called SpecKey and then write a Case statement to compare. Below is some simplified code to show what I am doing. Is there a reason why I cannot use SpecKey in the case statement? And is there a workaround or better way to do this?
When I swap SpecKey with an already existing column in the database, the code compiles. But using the newly created column, SpecKey, seems to be the problem. Here is the error I receive in DBeaver: 'SQL Error [42501]: UCAExc:::5.0.1 user lacks privilege or object not found: FULL.SPECKEY'
SQL Code:
SELECT Flat_Size + '|' + Finish_Size + '|' + STD_PK + '|' + STD_PK_QUANTITY as SpecKey,
CASE WHEN FULL.SpecKey IS '4x8|4x4|Each|1' THEN '12345ID'
ELSE NULL
END AS TemplateID
FROM `Table`

find diffrences between 2 tables sql and how can i get the changed value?

i have this query
insert into changes (id_registro)
select d2.id_registro
from daily2 d2
where exists (
select 1
from daily d1
where
d1.id_registro = d2.id_registro
and (d2.origen, d2.sector, d2.entidad_um, d2.sexo, d2.entidad_nac, d2.entidad_res,
d2.municipio_res, d2.tipo_paciente,d2.fecha_ingreso, d2.fecha_sintomas,
d2.fecha_def, d2.intubado, d2.neumonia, d2.edad, d2.nacionalidad, d2.embarazo,
d2.habla_lengua_indig, d2.diabetes, d2.epoc, d2.asma, d2.inmusupr, d2.hipertension,
d2.otra_com, d2.cardiovascular, d2.obesidad,
d2.renal_cronica, d2.tabaquismo, d2.otro_caso, d2.resultado, d2.migrante,
d2.pais_nacionalidad, d2.pais_origen, d2.uci )
<>
(d1.origen, d1.sector, d1.entidad_um, d1.sexo, d1.entidad_nac, d1.entidad_res,
d1.municipio_res, d1.tipo_paciente, d1.fecha_ingreso, d1.fecha_sintomas,
d1.fecha_def, d1.intubado, d1.neumonia, d1.edad, d1.nacionalidad, d1.embarazo,
d1.habla_lengua_indig, d1.diabetes, d1.epoc, d1.asma, d1.inmusupr, d1.hipertension,
d1.otra_com, d1.cardiovascular, d1.obesidad,
d1.renal_cronica, d1.tabaquismo, d1.otro_caso, d1.resultado, d1.migrante,
d1.pais_nacionalidad, d1.pais_origen, d1.uci ))
it results in an insersion data that doesn't exist in another table, that's fine. but i want know exactly which field has changed to store it in a log table
You don't mention precisely what you expect to see in your output but basically to accomplish what you're after you'll need a long sequence of CASE clauses, one for each column
e.g. one approach might be to create a comma-separated list of the column names that have changed:
INSERT INTO changes (id_registro, column_diffs)
SELECT d2.id_registro,
CONCAT(
CASE WHEN d1.origen <> d2.origen THEN 'Origen,' ELSE '' END,
CASE WHEN d1.sector <> d2.sector THEN 'Sector,' ELSE '' END,
etc.
Within the THEN part of the CASE you can build whatever detail you want to show
e.g. a string showing before and after values of the columns CONCAT('Origen: Was==> ', d1.origen, ' Now==>', d2.origen). Presumably though you'll also need to record the times of these changes if there can be multiple updates to the same record throughout the day.
Essentially you'll need to decide what information you want to show in your logfile, but based on your example query you should have all the information you need.

Like Clause over an 'Element' - ORACLE APEX

I encounter some problems that i don't understand with APEX.... Well, let's be specific.
I ve got a select element retrieving a top 50 of most liked url (P11_URL). This is populate by a table view, TOp_Domains.
I create an element called "Context" that have to print all text containing the URL selected by the user from the element select. Those Texts come from another table, let's say "twitter_post".
I create a dynamic action (show only) with this sql/statement:
Select TXT, NB_RT, RANK
from myschema.twitter_post
where TXT like '%:P11_URL%'
group by TXT, NB_RT, RANK
.... and it doesn't work... I think APEX don't like like clause... But i don't know how to do. Let's keep in min an url could have been shared by multiple Tweets, that's why this element "context" is important for me.
I tried to bypass the problem by building a State (in french Statique) and a dynamic action that will refresh the state but it doesn't work neither... bouhououououou
TriX
Right click on the 'P11_URL' and create DA. Event :change, Item:P11_URL. As the true action of the DA, select 'Set Value'. Write your query in the sql stmt area. In the page items to submit, select 'P11_URL' . In the 'Affected Items': select 'Context'.
Query should be :
Select TXT, NB_RT, RANK
from myschema.twitter_post
where TXT like '%' || :P11_URL || '%'
group by TXT, NB_RT, RANK
So
Thanks to #Madona... Their example made me realised my mistake. I wrote the answer here for futher help if somebody encouter the same porblem.
A list select element get as arguments a display value (the one you want to be shown in your screen.... if you want so....^^ ) and a return value (in order, I think to linked dynamic actions). So to solved my problem i had to shape my sql statement as:
select hashtags d, hastags r
from my table
order by 1
[let s say that now in Apex it s an object called P1_HASHTAGS]
First step problem solving.
In fact, the ranking as second value, as i put into my sql statement was making some mitsakens into my 'Where like' clause search... well... Newbie am i!
Second step was to correctly formate the sql statement receiving the datas from my select lov (P1_HASHTAGS) into my interactive report. As shown here:
Select Id, hashtags
from my table
where txt like '%'||:P1_HASHTAGS||'%'
And it works!
Thank you Madona your example helped me figure my mistakes!

T-SQL capturing Linked server name as a result column

How can I capture the (linked) server (in this case Morpheus) name as a column of the result. I do not want to define the Server name in the query itself.
exec("
select
COMNO
,T$CPLS ""Catalog""
,T$CUNO ""Customer ID.""
,T$CPGS ""Price Group""
,T$ITEM ""Item Code""
,T$UPCD UPC
,T$DSCA ""Description""
,T$WGHT ""Weight""
,T$SHIP ""Shipping Indicator""
,nvl(T$STDT,to_char(sysdate,'YYYY-MM-DD')) ""From""
,nvl(case T$TDAT
when '4712-01-01' then ' '
when null then ' '
else t$tdat
end,' ') ""To""
,nvl(t$qanp,99999999) ""Qty.""
,T$PRIC ""List Price""
,T$DISC ""Discount""
,to_char(round(t$pric * (1-t$disc/100),2),99999.99) ""Net""
,Source ""Source""
from Table(edi.ftCompositCatalog(?,?,?)) --where trim(t$item)='105188-041'
order by Source,t$cpgs,t$item",'010','145','000164') at morpheus
If, when running your query, you already know what linked server you are pointing to, then just include that as a string literal in your result:
exec("
select
'morpheus' ""Server Name""
,T$CPLS ""CATALOG""
...
Even if the linked server name is being stored in a variable, you can do this easily since you're building your query string dynamically.
If, as you say, you don't want to define it as a string literal, here is a normal way to get the host (server) name in Oracle:
SELECT SYS_CONTEXT ('USERENV', 'SERVER_HOST') FROM DUAL;
If you want to embed this as a subquery or inline view in your query, I think it would work.
*Please note that some organizations & dba's do not want you to know anything about the backend environment for security reasons, but assuming you have no roadblock there, this should work.