How to query a Super Column containing an array in AWS RedShift - amazon-redshift

I have a column(user_array) in my Redshift DB table "User" with the data type Super which contains an array. It looks something like this.
["{\"username\":\"John\"","\"usertype\":\"ABC\"}"]
How do I query this table to pull the username?
I tried the following methods:
select x.username from User u
left join u.user_array x on true;
or
select u.user_array.username from User u;
Both statements return null.
What is the correct way of querying? I could not find it in any of the documentation.

Related

PostgreSQL, allow to filter by not existing fields

I'm using a PostgreSQL with a Go driver. Sometimes I need to query not existing fields, just to check - maybe something exists in a DB. Before querying I can't tell whether that field exists. Example:
where size=10 or length=10
By default I get an error column "length" does not exist, however, the size column could exist and I could get some results.
Is it possible to handle such cases to return what is possible?
EDIT:
Yes, I could get all the existing columns first. But the initial queries can be rather complex and not created by me directly, I can only modify them.
That means the query can be simple like the previous example and can be much more complex like this:
WHERE size=10 OR (length=10 AND n='example') OR (c BETWEEN 1 and 5 AND p='Mars')
If missing columns are length and c - does that mean I have to parse the SQL, split it by OR (or other operators), check every part of the query, then remove any part with missing columns - and in the end to generate a new SQL query?
Any easier way?
I would try to check within information schema first
"select column_name from INFORMATION_SCHEMA.COLUMNS where table_name ='table_name';"
And then based on result do query
Why don't you get a list of columns that are in the table first? Like this
select column_name
from information_schema.columns
where table_name = 'table_name' and (column_name = 'size' or column_name = 'length');
The result will be the columns that exist.
There is no way to do what you want, except for constructing an SQL string from the list of available columns, which can be got by querying information_schema.columns.
SQL statements are parsed before they are executed, and there is no conditional compilation or no short-circuiting, so you get an error if a non-existing column is referenced.

Raw SQL SELECT in EF Core 3.1 on dynamic Table

My trivial problem is this: I need to select a single field from a table filtering by the Id field.
Something that in SQL I would simply write like this:
SELECT Field FROM {Table} WHERE Id = 1
The table name is variable.
How do I do this very trivial Query with EF Core 3.1 considering that the table name can vary?
I have already tried
context.Database.ExecuteSqlCommand($"SELECT TOP 1 Field FROM {table} WHERE Id={id}")
but I cannot read the result (it always returns -1)
I also tried this:
var test = context.Set<T>()
.FromSqlRaw($"SELECT Id FROM {table}")
.FirstOrDefault();
but give me an error because it wants all the fields in the select statement
Thanks for the support

How to write a select statement for a table in postgres that is inside a schema?

I have a postgres DB and inside of it there are many schemas.
Each one of those schemas contains tables. For example:
Schema Name: personal has tables actions_takes, page_views etc
How can i write a SQL query or ActiveRecord query to query the table inside the schema?
Something like:
select * from actions_takes where user_id = 123;
I can create a model for each table and query it that way, but i want to write a script that passed a user goes over all tables and get the data for that user.
in pgAdmin 4 web console should use double quotation marks like following select statement
SELECT "col1", "col2"
FROM "schemaName".profile;
Point to specific table within a given schema using a dot notation schema.table_name. In your case it translates to
select * from personal.actions_takes where user_id = 123;
For me this query worked : select * from schemaName."Table_Name"

Postgresql dblink

Trying to be lazy when looking at an example
SELECT realestate.address, realestate.parcel, s.sale_year, s.sale_amount,
FROM realestate INNER JOIN
dblink('dbname=somedb port=5432 host=someserver
user=someuser password=somepwd',
'SELECT parcel_id, sale_year,
sale_amount FROM parcel_sales')
AS s(parcel_id char(10),sale_year int, sale_amount int)
Is there a way of getting the AS section filled in from the table?
I'm copying data from tables of the same name and structure on different servers.
If I can get the structure to copy from the existing table, it will save me a lot of time
Thanks
Bruce
The answer is: No. See the doc:
Since dblink can be used with any query, it is declared to return record, rather than specifying any particular set of columns. This means that you must specify the expected set of columns in the calling query — otherwise PostgreSQL would not know what to expect.
http://www.postgresql.org/docs/9.1/static/contrib-dblink-function.html
Edit: by the way, for a table or a view, you can get the fields name and type in a first query:
select column_name
from information_schema.columns
where table_name = 'your_table_or_view';
You could then use it to fill the fields declaration.
Alexis

SQLite query in Objective C iPhone App

I am building this application which fetches data from SQLite database tables, when a string is being entered in to UISearchBar, all possible entries should be printed. Right now i am able to fetch all the entries from the table but i am not able to perform the specific search, can anyone help me with the query or give some example to refer?
SELECT * FROM mytable WHERE myfield like '%your searchwordhere%' by executing this query you can get the words which are having the word of your search keyword.
Try this:
SELECT fieldone, fieldtwo FROM mytable WHERE myfield like '%%SEARCH_TERM_GOES_HERE%%'