SQL REGEX search for table name - sql

this is my first entry, so please excuse me for any mistakes I might make writing this Post.
I have an oracle database schema full of Table Views. I want to use the "TEXT_VC" column in the "all_views" table to get a list of the source table names.
This is my statement so far:
SELECT view_name, text_vc, REGEXP_SUBSTR(TEXT_VC,'from (.+?) ') AS source_table
FROM all_views
WHERE owner = 'OWNER';
I still have a view problems with this regex.
First of all, I am getting "from table_name" as a result. I don't want to get "from" in my result set.
Secondly, the column TEXT_VC basically displays the SQL behind the view. So in some cases I don't get any results, because there are line breaks. I tried working with \n and \r but I am not able to generate the result I want.
Can anyone please help me?

select * from all_dependencies
Demo
create table mytable_1 (i int);
create table mytable_2 (i int);
create view myview
as
select count(*) as cnt
from mytable_1 t1
join mytable_2 t2
on t2.i = t1.i
;
select *
from all_dependencies
where owner = 'DMARKOVITZ'
and name = 'MYVIEW'
;
+------------+--------+------+------------------+-----------------+-----------------+----------------------+-----------------+
| OWNER | NAME | TYPE | REFERENCED_OWNER | REFERENCED_NAME | REFERENCED_TYPE | REFERENCED_LINK_NAME | DEPENDENCY_TYPE |
+------------+--------+------+------------------+-----------------+-----------------+----------------------+-----------------+
| DMARKOVITZ | MYVIEW | VIEW | DMARKOVITZ | MYTABLE_1 | TABLE | (null) | HARD |
| DMARKOVITZ | MYVIEW | VIEW | DMARKOVITZ | MYTABLE_2 | TABLE | (null) | HARD |
+------------+--------+------+------------------+-----------------+-----------------+----------------------+-----------------+

Related

How to get a value inside of a JSON that is inside a column in a table in Oracle sql?

Suppose that I have a table named agents_timesheet that having a structure like this:
ID | name | health_check_record | date | clock_in | clock_out
---------------------------------------------------------------------------------------------------------
1 | AAA | {"mental":{"stress":"no", "depression":"no"}, | 6-Dec-2021 | 08:25:07 |
| | "physical":{"other_symptoms":"headache", "flu":"no"}} | | |
---------------------------------------------------------------------------------------------------------
2 | BBB | {"mental":{"stress":"no", "depression":"no"}, | 6-Dec-2021 | 08:26:12 |
| | "physical":{"other_symptoms":"no", "flu":"yes"}} | | |
---------------------------------------------------------------------------------------------------------
3 | CCC | {"mental":{"stress":"no", "depression":"severe"}, | 6-Dec-2021 | 08:27:12 |
| | "physical":{"other_symptoms":"cancer", "flu":"yes"}} | | |
Now I need to get all agents having flu at the day. As for getting the flu from a single JSON in Oracle SQL, I can already get it by this SQL statement:
SELECT * FROM JSON_TABLE(
'{"mental":{"stress":"no", "depression":"no"}, "physical":{"fever":"no", "flu":"yes"}}', '$'
COLUMNS (fever VARCHAR(2) PATH '$.physical.flu')
);
As for getting the values from the column health_check_record, I can get it by utilizing the SELECT statement.
But How to get the values of flu in the JSON in the health_check_record of that table?
Additional question
Based on the table, how can I retrieve full list of other_symptoms, then it will get me this kind of output:
ID | name | other_symptoms
-------------------------------
1 | AAA | headache
2 | BBB | no
3 | CCC | cancer
You can use JSON_EXISTS() function.
SELECT *
FROM agents_timesheet
WHERE JSON_EXISTS(health_check_record, '$.physical.flu == "yes"');
There is also "plain old way" without JSON parsing only treting column like a standard VARCHAR one. This way will not work in 100% of cases, but if you have the data in the same way like you described it might be sufficient.
SELECT *
FROM agents_timesheet
WHERE health_check_record LIKE '%"flu":"yes"%';
How to get the values of flu in the JSON in the health_check_record of that table?
From Oracle 12, to get the values you can use JSON_TABLE with a correlated CROSS JOIN to the table:
SELECT a.id,
a.name,
j.*,
a."DATE",
a.clock_in,
a.clock_out
FROM agents_timesheet a
CROSS JOIN JSON_TABLE(
a.health_check_record,
'$'
COLUMNS (
mental_stress VARCHAR2(3) PATH '$.mental.stress',
mental_depression VARCHAR2(3) PATH '$.mental.depression',
physical_fever VARCHAR2(3) PATH '$.physical.fever',
physical_flu VARCHAR2(3) PATH '$.physical.flu'
)
) j
WHERE physical_flu = 'yes';
db<>fiddle here
You can use "dot notation" to access data from a JSON column. Like this:
select "DATE", id, name
from agents_timesheet t
where t.health_check_record.physical.flu = 'yes'
;
DATE ID NAME
----------- --- ----
06-DEC-2021 2 BBB
Note that this approach requires that you use an alias for the table name (so you can use it in accessing the JSON data).
For testing I used the data posted by MT0 on dbfiddle. I am not a big fan of double-quoted column names; use something else for "DATE", such as dt or date_.

HIVE SQL: Select rows whose values contain string in a column

I want to select rows whose values contain a string in a column.
For example, I want to select all rows whose values contain a string '123' in the column 'app'.
table:
app id
123helper xdas
323helper fafd
2123helper dsaa
3123helper fafd
md5321 asdx
md5123 dsad
result:
app id
123helper xdas
2123helper dsaa
3123helper fafd
md5123 dsad
I am not familiar with SQL query.
Could anyone help me? .
Thanks in advances.
In a number of ways:
like:
select * from table
where app like '%123%'
rlike:
...
where app rlike '123'
instr:
...
where instr(app, '123')>0
locate:
...
where locate('123', app)>0
Invent your own way.
Read manual: String Functions and Operators.
Try the following using like
select
*
from yourTable
where app like '%123%'
Output:
| app | id |
| ---------- | ---- |
| 123helper | xdas |
| 2123helper | dsaa |
| 3123helper | fafd |
| md5123 | dsad |
Please use below query,
select app, id from table where app like '%123%';
Below are few additional information,
like '123%' --> Starts with 123
like '%123' --> Ends with 123
like '%123%'--> Contains 123 anywhere in the string

How to display all columns and its data type in a table via SQL query

I am trying to print the column names from a table called 'meta' and I need also its data types.
I tried this query
SELECT meta FROM INFORMATION_SCHEMA.TABLES;
but it throws an error saying no information schema available. Could you please help me, I am a beginner in SQL.
Edit:
select tables.name from tables join schemas on
tables.schema_id=schemas.id where schemas.name=’sprl_db’ ;
This query gives me all the tables in database 'sprl_db'
You can use the monetdb catalog:
select c.name, c.type, c.type_digits, c.type_scale
from sys.columns c
inner join sys.tables t on t.id = c.table_id and t.name = 'meta';
as you are using monetDB you can get that by using sys.columns
sys.columns
it will return all information related to table columns
you can also check Schema, table and columns documentation for monetDB
in sql server we get that like this exec sp_columns TableName
If I understand correctly you need to see the columns and the types of a table you (or some other user) defined called meta?
There are at least two ways to do this:
First (as #GMB mentioned in their answer) you can query the SQL catalog: https://www.monetdb.org/Documentation/SQLcatalog/TablesColumns
SELECT * FROM sys.tables WHERE NAME='meta';
+------+------+-----------+-------+------+--------+---------------+--------+-----------+
| id | name | schema_id | query | type | system | commit_action | access | temporary |
+======+======+===========+=======+======+========+===============+========+===========+
| 9098 | meta | 2000 | null | 0 | false | 0 | 0 | 0 |
+------+------+-----------+-------+------+--------+---------------+--------+-----------+
1 tuple
So this gets all the relevant information about the table meta. We are mostly interested in the value of the column id because this uniquely identifies the table.
(Please note that this id will probably be different in your system)
After we have this information we can query the columns table with this table id:
SELECT * FROM sys.columns WHERE table_id=9098;
+------+------+------+-------------+------------+----------+---------+-------+--------+---------+
| id | name | type | type_digits | type_scale | table_id | default | null | number | storage |
+======+======+======+=============+============+==========+=========+=======+========+=========+
| 9096 | i | int | 32 | 0 | 9098 | null | true | 0 | null |
| 9097 | j | clob | 0 | 0 | 9098 | null | true | 1 | null |
+------+------+------+-------------+------------+----------+---------+-------+--------+---------+
2 tuples
Since you are only interested in the names and types of the columns, you can modify this query as follows:
SELECT name, type FROM sys.columns WHERE table_id=9098;
+------+------+
| name | type |
+======+======+
| i | int |
| j | clob |
+------+------+
2 tuples
You can combine the two queries above with a join:
SELECT col.name, col.type FROM sys.tables as tab JOIN sys.columns as col ON tab.id=col.table_id WHERE tab.name='meta';
+------+------+
| name | type |
+======+======+
| i | int |
| j | clob |
+------+------+
2 tuples
The second, and preferred way to get this information if you are using the mclient utility of MonetDB, is by using the describe meta-command of mclient. When used without arguments it presents a list of tables that have been defined in the current database and when it is given the name of the table it prints its SQL definition:
sql>\d
TABLE sys.data
TABLE sys.meta
sql>\d sys.meta
CREATE TABLE "sys"."meta" (
"i" INTEGER,
"j" CHARACTER LARGE OBJECT
);
You can use the \? meta-command to see a list of all meta-commands in mclient:
sql>\?
\? - show this message
\<file - read input from file
\>file - save response in file, or stdout if no file is given
\|cmd - pipe result to process, or stop when no command is given
\history - show the readline history
\help - synopsis of the SQL syntax
\D table - dumps the table, or the complete database if none given.
\d[Stvsfn]+ [obj] - list database objects, or describe if obj given
\A - enable auto commit
\a - disable auto commit
\e - echo the query in sql formatting mode
\t - set the timer {none,clock,performance} (none is default)
\f - format using renderer {csv,tab,raw,sql,xml,trash,rowcount,expanded,sam}
\w# - set maximal page width (-1=unlimited, 0=terminal width, >0=limit to num)
\r# - set maximum rows per page (-1=raw)
\L file - save client-server interaction
\X - trace mclient code
\q - terminate session and quit mclient
For MySQL:
SELECT column_name,
data_type
FROM information_schema.columns
WHERE table_schema = ’ yourdatabasename ’
AND table_name = ’ yourtablename ’;
Output:
+-------------+-----------+
| COLUMN_NAME | DATA_TYPE |
+-------------+-----------+
| Id | int |
| Address | varchar |
| Money | decimal |
+-------------+-----------+

Return an array(Repeated Field) from a query in BigQuery

I am new to BigQuery and SQL. I have a table with following details
Schema
ID : String : Nullable
BCats : String : Repeated
ID can be repeated
Preview
ID BCats
|-----------------------|
| ABCD | BCat25 |
| | BCat24 |
| | BCat23 |
|_______________________|
| PQRS | BCat8 |
| | BCat9 |
|_______________________|
| ABCD | BCat23 |
| | BCat25 |
| | BCat24 |
|_______________________|
| MNOP | BCat12 |
| | BCat13 |
|_______________________|
| PQRS | BCat8 |
| | BCat9 |
|-----------------------|
I am trying to group the table based on ID using the following query
Query
SELECT BCats,ID
FROM (SELECT (GROUP_CONCAT(BCats)) as BCats,ID
FROM(
SELECT
UNIQUE(BCats) as BCats,ID FROM
my_table
GROUP BY
ID
)
GROUP BY
ID
)
OUTPUT from the query in JSON Format is
Output from Query
{"BCats":"BCat25,BCat24,BCat23","ID":"ABCD"}
{"BCats":"BCat8,BCat9","ID":"PQRS"}
{"BCats":"BCat12,BCat13","ID":"MNOP"}
My question is how can I output Array from the Query, like this
Expecting Output
{"BCats" : ["BCat25","BCat24","BCat23"],"ID":"ABCD"}
Currently I am getting BCats as a String.
I need to output this data into new table with BCats as Repeated.
Please Help.
Preview :
Try below. Note: in Web UI you need not only set Destination Table but also set/check-on Allow Large Results checkbox and uncheck Flatten Results checkbox
SELECT NEST(UNIQUE(BCats)) AS BCats, ID
FROM my_table
GROUP BY ID
You should instead use standard SQL. If you are familiar with legacy SQL, there is a migration guide that talks about the differences between the two dialects. After enabling standard SQL (uncheck "Use Legacy SQL" under "Show Options" in the UI) you can run e.g.:
WITH my_table AS (
SELECT 'ABCD' AS ID, ['BCat25', 'BCat24', 'BCat23'] AS BCats UNION ALL
SELECT 'PQRS', ['BCat8', 'BCat9'] UNION ALL
SELECT 'ABCD', ['BCat23', 'BCat25', 'BCat24'] UNION ALL
SELECT 'MNOP', ['BCat12', 'BCat13'] UNION ALL
SELECT 'PQRS', ['BCat8', 'BCat9']
)
SELECT
ID,
ARRAY_AGG(DISTINCT BCat) AS BCats
FROM my_table, UNNEST(BCats) AS BCat
GROUP BY ID;

SQL query to format table data for DataSource in GridView

I am looking for a SQL Server query that could transfer source SQL table data:
TextID | Text | LanguageID
-------|-------|-------------------------------------
app.aa | Hi | 6a13ea09-46ea-4c93-9b6a-e26bdc6ff4d8
app.cc | Hund | 0c894bb7-4937-4903-906a-d1b1dd64935c
app.aa | Hallo | 0c894bb7-4937-4903-906a-d1b1dd64935c
app.cc | Dog | 6a13ea09-46ea-4c93-9b6a-e26bdc6ff4d8
app.bb | Star | 6a13ea09-46ea-4c93-9b6a-e26bdc6ff4d8
...
into table like this one:
TextID | Original | Translated
-------|----------|-----------
app.aa | Hi | Hallo
app.bb | Star | -
app.cc | Dog | Hund
...
so that I can use it as a DataSource for GridView in ASP .NET. Thank you in advance for your help.
Whenever you need to combine data from two different rows into one, you need to join. For example:
select src.TextID "TextID", src.Text "Original", tr.Text "Translated"
from source_table src
left join source_table tr
on src.TextID = tr.TextID
and src.LangID = 'xxx' -- xxx is the source language id
and tr.LangID = 'yyy' -- yyy is the target language id
The left join ensures that untranslated words are included with a null translated value. To make a table for your DataSource, you'll need to wrap create table (or maybe create view) around the select:
create table translations as
select ...