Intersystems Cache: Querying a table that has a column with data type of another table - intersystems-cache

What is the notation I need to use to extract specific/all row and field information from a field that is of type table (basically an embedded table inside of a field)?

You can use -> notation.
For example your table name is "Sample.Person" and the "zipcode" is a column of that table that directs to "Sample.Zipcodes"
Example Queries:
SELECT *,zipcode->name,zipcode->number FROM Sample.Person
SELECT * FROM Sample.Person where zipcode->name = 'foo bar'

Related

postgresql 11 select value from jsonb data by path from other table field

I have jsonb field 'data' in my table. I can get value of subKey by select data#>'{key1,subKey}' from table.How to insert a path into select if the path is stored in another table as a string 'key1,subKey'?
You should really store it as an array, not as a string, since that is how it will be used. You can just split it to an array dynamically, but then what if you ever need the comma to appear literally in the path?
with t as (select '{"key1":{"subKey":"foo"}}'::jsonb as data),
k as (select 'key1,subKey' as k)
select data#>regexp_split_to_array(k,',') from t,k;

Like operation on Json object in postgresql

I have the JSON column in my table which is having array's of dictionary. The array has standard format.
[{'path': 'chat/xyz.pdf', 'file_name': 'xyz.pdf'},
{'path': 'chat/xyl.pdf', 'file_name': 'xyl.pdf'}]
The table name is chat and column name is attachments. I want to perform search on file names such that even if i type one letter is typed then that row should be retrieved. For example: if i search by string 'pd' then all values with file_name having 'pd' string should be retrieved.
I tried this and it did work.
select distinct attachments from chat, jsonb_array_elements_text(attachments)
where value::json->>'file_name' like '%xyz%';
I took reference from documentation.
You can use an EXISTS condition, then you don't need the DISTINCT:
select c.*
from chat c
where exists (select *
from jsonb_array_elements(c.attachments) as t(doc)
where t.doc ->> 'file_name' like '%xyz%');

Why is "select table_name from table_name" valid [duplicate]

This question already has an answer here:
What are differences between SQL queries?
(1 answer)
Closed 4 years ago.
This syntax is valid for PostgreSQL:
select T from table_name as T
T seems to become a CSV list of values from all columns in table_name. select T from table_name as T works, and, for that matter, select table_name from table_name. Where is this syntax documented, and what is the datatype of T?
This syntax is not in SQL Server, and (AFAIK) does not exist in any other SQL variant.
If you create a table, Postgres creates a type with the same name in the background. The table is then essentially a "list of that type".
Postgres also allows to reference a complete row as a single "record" - a value built from multiple columns. Those records can be created dynamically through a row constructor.
Each row in a the result of a SELECT statement is implicitly assigned a TYPE - if the row comes from a single table, it's the table's type. Otherwise it's an anonymous type.
When you use the table name in a place where a column would be allowed it references the full row as a single record. If the table is aliased in the select, the type of that record is still the table's type.
So the statement:
select T
from table_name as T;
returns a result with a single column which is a record (of the table's type) containing each column of the table as a field. The default output format of a record is a comma separated list of the values enclosed in parentheses.
Assuming table_name has three columns c1, c2 and c3 the following would essentially do the same thing:
select row(c1, c2, c3)
from table_name;
Note that a record reference can also be used in comparisons, e.g. finding rows that are different between two tables can be done in the following manner
select *
from table_one t1
full outer join table_two t2 on t1.id = t2.id
where t1 <> t2;

SQL database repetative value sum

i have a database table with repetitive values in every cell and also same values separated by commas in particular cell, if i need to count all the occurrences of particular name (ex : Facebook) what query i must use(amazon red shift)
A typical SQL query would be:
select count(1) from table t1 where t1.column_name = 'Facebook';
This is if the data cells contain just that word exactly.
Or you might have:
select count(1) from table t1 where t1.column_name like '%Facebook%';
This is if you want to count rows containing word 'Facebook' (among other strings).

Copy selected query fields name in Mysql Workbench

I am using mysql workbench (SQL Editor). I need copy the list of columns in each query as was existed in Mysql Query Browser.
For example
Select * From tb
I want have the list of fields like as:
id,title,keyno,......
You mean you want to be able to get one or more columns for a specified table?
1st way
Do SHOW COLUMNS FROM your_table_name and from there on depending on what you want have some basic filtering added by specifying you want only columns that data type is int, default value is null etc e.g. SHOW COLUMNS FROM your_table_name WHERE type='mediumint(8)' ANDnull='yes'
2nd way
This way is a bit more flexible and powerful as you can combine many tables and other properties kept in MySQL's INFORMATION_SCHEMA internal db that has records of all db columns, tables etc. Using the query below as it is and setting TABLE_NAME to the table you want to find the columns for
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='your_table_name';
To limit the number of matched columns down to a specific database add AND TABLE_SCHEMA='your_db_name' at the end of the query
Also, to have the column names appear not in multiple rows but in a single row as a comma separated list you can use GROUP_CONCAT(COLUMN_NAME,',') instead of only COLUMN_NAME
To select all columns in select statement, please go to SCHEMAS menu and right click ok table which you want to select column names, then select "Copy to Clipboard > Select All statement".
The solution accepted is fine, but it is limited to field names in tables. To handle arbitrary queries would be to standardize your select clause to be able to use regex to strip out only the column aliases. I format my select clause as "1 row per element" so
Select 1 + 1 as Col1, 1 + 2 Col2 From Table
becomes
Select 1 + 1 as Col1
, 1 + 2 Col2
From Table
Then I use simple regex on the "1 row per select element" version to replace "^.* " (excluding quotes) with nothing. The regex finds everything before the final space in the line, so it assumes your column aliases doesn't contain spaces (so replace spaces with underscore). Or if you don't like "1 row per element" then always use "as" keyword to give you a handle that regex can grasp.