How do I query across many tables in redshift without UNION ALL? - sql

I'm looking for a dumb way to write the same select query across all tables. For example in Google Bigquery I can query like this using wild cards
select COMPLICATED QUERY HERE from `myproject:mytable_2017_1_*`;
How can I do the equivalent in redshift?

The wildcard syntax is not available for Amazon Redshift. Each query must specifically reference the table(s) it wishes to use.
You could create a VIEW that does the UNION ALL for you, and then you could just query the view.

Related

Iterative union SQL query

I'm working with CA (Broadcom) UIM. I want the most efficient method of pulling distinct values from several views. I have views that start with "V_" for every QOS that exists in the S_QOS_DATA table. I specifically want to pull data for any view that starts with "V_QOS_XENDESKTOP."
The inefficient method that gave me quick results was the following:
select * from s_qos_data where qos like 'QOS_XENDESKTOP%';
Take that data and put it in Excel.
Use CONCAT to turn just the qos names into queries such as:
SELECT DISTINCT samplevalue, 'QOS_XENDESKTOP_SITE_CONTROLLER_STATE' AS qos
FROM V_QOS_XENDESKTOP_SITE_CONTROLLER_STATE union
Copy the formula cell down for all rows and remove Union from the last query as well
as add a semicolon.
This worked, I got the output, but there has to be a more elegant solution. Most of the answers I've found related to iterating through SQL uses numbers or doesn't seem quite what I'm looking for. Examples: Multiple select queries using while loop in a single table? Is it Possible? and Syntax of for-loop in SQL Server
The most efficient method to do what you want to do is to do something like what CA's scripts do (the ones you linked to). That is, use dynamic SQL: create a string containing the SQL you want from system tables, and execute it.
A more efficient method would be to write a different query based on the underlying tables, mimicking the criteria in the views you care about.
Unless your view definitions are changing frequently, though, I recommend against dynamic SQL. (I doubt they change frequently. You regenerate the views no more frequently than you get a new script, right? CA isn't adding tables willy nilly.) AFAICT, that's basically what you're doing already.
Get yourself a list of the view names, and write your query against a union of them, explicitly. Job done: easy to understand, not much work to modify, and you give the server its best opportunity to optimize.
I can imagine that it's frustrating and error-prone not to be able to put all that work into your own view, and query against it at your convenience. It's too bad most organizations don't let users write their own views and procedures (owned by their own accounts, not dbo). The best I can offer is to save what would be the view body to a file, and insert it into a WITH clause in your queries
WITH (... query ...) as V select ... from V

Is there an "WITH" clause in BigQuery

I am a new user of bigquery. I used to use Postgresql and the WITH clause...
I have written multiple queries. Results of these queries need to be joined in order to create a single table (all results have a common key column).
Is there an equivalent of the WITH clause? I am reluctant to use sub-queries as my code will be hard to maintain.
BigQuery does support WITH clause in Standard SQL dialect. Documentation here: https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#with-clause
WITH doesn't come with BigQuery
How about using VIEWs?

PeopleSoft Query Union to Existing Query

In PS Query, is it possible to create a union with an existing query? Basically I need to take a query, union it with a duplicate (with a minor change). Unfortunately, they query is quite complex, with tons of expressions.
I have full access to the database, so I'm not limited to the query tool (for building - this is something that a basic user will run from query viewer)
As a direct answer to your question, no, that isn't possible in ps qry.
What I would do is, since you have full access to the database, create the query you want to connect to as a view, and then use a union w/ that view instead of the query, ie.
(the query you're working on)
union
select * from name_of_new_vw

How to write riak query in riakc

I am a noob at riak, and have been trying to test the query aspect of riak using riakc in erlang. but I can not find any example of how to query the database that match the old SQL way, Only on how to get a single value out of a single field. I think I am missing something but all I really want is just a standard SQL query with a matching riakc code.
SELECT * FROM bucket;
SELECT * FROM bucket LIMIT 10, 100;
SELECT id, name FROM bucket;
SELECT * FROM bucket WHERE name="john" AND surname LIKE "Ste%";
SELECT * FROM bucket LEFT JOIN bucket2 ON bucket.id = bucket2.id2;
I assume that there is no direct correlation on how you write these, but was hoping there is a standard way, and is there somewhere that has a a simple to understand way of explaining this querys in riakc (or even just riak).
I have looked a mapreduce but found that it was confusing for just simple queries
Riak is a NoSQL database, more specifically a key-value database, and there is no query language like SQL available. When working with Riak you need to model and query your data in a completely different way compared to how you use a relational database in order to get the most from it. Trying to model and query your data in a relational manner, e.g. by extensive use of secondary indexes or by trying to use map/reduce as a real-time query language, generally results in very poor performance and scalability. A good and useful discussion about Riak development anti-patterns that can be found here.

Questions about query several tables through JPA

Recently, I need to use JPA in my project.
However, I encountered some difficulties. JPA seemes to be harder than I thought.
So, I post my problem here, hoping that, some guys may help me.
There are two tables operation and analysis, the sql is like the following:
select op_id from operation where flag = 1
union
select an_id from analysis where on_type = "processed";
op_id and an_id are two column names actually means the same.
Can anyone help me writing a JPA version of this?
I'm very grateful of that.
Thank you very much.
JPQL does not support unions.
You have four options:
You can create a View in your DB that would do the unioning, then map to that view
Write a native SQL that does the union (since you only need the ID, this is a valid option)
If the IDs are distinct, you can run two queries, and concatenate the result list (if not distinct, you could use Sets to filter duplications)
If you are using the latest (at least >=2.4) EclipseLink as a JPA provider, you can use UNION
See this question here.