comparison of several attributes in one SQL query - sql

I want tow write a Teradata SQL query.
I have two attributes: date and name.
I want to create do a comparison for those two only using ONE subquery. I want it to look something like:
date,name= SELECT date, name FROM ...
Is it possible? How does that syntax look like

Have you tried the following:
SELECT {columns}
FROM {TableA}
WHERE (date,name) IN (SELECT date, name FROM {TableB});

Related

SQLite, LIKE column name

What query could I use in sqlite to get the names of columns beginning with (for example) "thing" in a DB
Such as if they were formatted like this:
"thing_column1"
"thing_column2"
"thing_data"
etc.
You can use pragma_table_info() with the table's name:
SELECT name
FROM pragma_table_info('tablename')
WHERE name LIKE 'thing%'
You can use this query:
SELECT GROUP_CONCAT(name) AS columns
FROM pragma_table_info('tablename')
WHERE name LIKE 'thing%'
which returns only 1 column columns with a string value like 'thing_column1,thing_column2,thing_column3' and you can use it to construct a SELECT statement in your application.

T-SQL: Exclude Columns from a SELECT statement based on string

My overall goal is to create new tables by selecting columns in existing tables with certain patterns/tags in their column names. This is in SQL Server.
For example, I have a common need to get all contact information out of a company table, and into its own contact table.
I haven't been able to find a programmatic approach in SQL to express excluding columns from a SELECT statement based on string.
When looking to options like the COL_NAME function, those require an ID arg, which kills that option for me.
Wishing there was something built in that could work like the following:
SELECT *
FROM TABLE
WHERE COL_NAME() LIKE 'FLAG%'
Any ideas? Open to anything! Thanks!!
One trick could be to firstly using the following code to get the column names you desire:
select * from information_schema.columns
where table_name='tbl' and column_name like 'FLAG%'
Then concatenate them into a comma-delimited string and finally create a dynamic sql query using the created string as the column names.

Select name from system table and select from this table

I need dynamically obtain table name from system table and perform a select query on this table example:
SELECT "schema"+'.'+"table" FROM SVV_TABLE_INFO WHERE "table" LIKE '%blabla%'
it returns my_schema.the_main_blabla_table
And after I get this table name I need to perform :
SELECT * FROM my_schema.the_main_blabla_table LIMIT 100
Is it possible to in a single query?
If you are talking about select subquery after "from" i can say that you can do this.
You will get something like this:
SELECT * FROM
(
SELECT "schema"+'.'+"table" FROM SVV_TABLE_INFO WHERE "table" LIKE '%blabla%'
)
LIMIT 100
Unfortunately, i can't test it on yor data, but i very interested in result because i have never done something like this. If i get your question incorrect, tell me pls.
Amazon Redshift does not support the ability to take the output of a query and use it as part of another query.
Your application will need to query Redshift to obtain the relevant table name(s), then make another call to Redshift to query that table.

How does one select all columns but rename some of them in one statement?

Normally I would write a statement like:
SELECT * FROM my_table;
But I have two columns, (both of date type), called 'created' and 'edited'. If I do select *, then the date in each of these columns will appear as:
2017-11-04T18:30:00.000Z
I would rather the date appear in DD/MON/YYYY.
To do that, I currently modify my SQL statement to:
SELECT column_name1,column_name2,column_name3,to_char(created, 'DD-MON-YYYY') as created,column_name4.... FROM my_table;
The problem is that although I can format the date, I have the problem of having to specify each column name in the statement. Is there some way I can select all the columns (but rename one or more columns using the method above), without having to specify each column name ?
You can try something like this:
select to_char(t.created,'DD-MON-YYYY') as created,to_char(t.edited,'DD-MON-YYYY') as edited, t.* from my_table t;

Bigquery how to query multiple tables of the same structure?

I have datasets of the same structure and i know I can query them like this, they are named by date:
SELECT column
FROM [xx.ga_sessions_20141019] ,[xx.ga_sessions_20141020],[xx.ga_sessions_20141021]
WHERE column = 'condition';
However I actually want to query various months of this data... so instead of listing them all in the same way as above, is there syntax that you can use that looks like:
SELECT column
FROM [xx.ga_sessions_201410*] ,[xx.ga_sessions_201411*]
WHERE column = 'condition';
Take a look at the table wildcard functions section of the BigQuery query reference. TABLE_DATE_RANGE or TABLE_QUERY will work for you here. Something like:
SELECT column
FROM TABLE_DATE_RANGE(xx.ga_sessions_,
TIMESTAMP('2014-10-19'),
TIMESTAMP('2014-10-21'))
WHERE column = 'condition';