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?
Related
programmers.
I have a question.
I know that Oracle's WITH is used when using subquery a lot, what is the difference from View?
With: The WITH clause is a kind of inline view or a temporary table. The advantage of using is that if there is multiple reference to a subquery it can be replaced temporary table, rather being written multiple times.
View: It is a virtual table based on the result of an SQL statement.
A View is a type of persistent sql object like a table or index used to produce a query result while a with statement is sql syntax similar in effect to a sub query.
With statements allow for the ability to break down a query into smaller efficient steps and ultimately are used to produce a query result.
Since the goal of with statements is to produce a query result, with statements can exist inside views.
I am working in MySql. Hive, to be particular.
There is no such limit, however, you can refer the documentation for kind of subqueries are supported.
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.
I recently learned about Recursive Common Table Expressions (CTEs) while looking for a way to build a certain view of some data. After taking a while to write out how the first iteration of my query would work, I turned it into a CTE to watch the whole thing play out. I was surprised to see that grouping didn't work, so I just replaced it with a "Select TOP 1, ORDER BY" equivalent. I was again surprised that "TOP" wasn't allowed, and came to find that all of these clauses aren't allowed in the recursive part of a CTE:
DISTINCT
GROUP BY
HAVING
TOP
LEFT
RIGHT
OUTER JOIN
So I suppose I have 2 questions:
In order to better understand my situation and SQL, why aren't these clauses allowed?
If I need to do some sort of recursion using some of these clauses, is my only alternative to write a recursive stored procedure?
Thanks.
Referring to :-
In order to better understand my situation and SQL, why aren't these clauses allowed?
Based on my understanding of CTE's, the whole idea behind creating a CTE is so that you can create a temporary result-set and use it in a named manner like a regular table in SELECT, INSERT, UPDATE, or DELETE statements.
Because a CTE is logically very much like a view, the SELECT statement in your CTE query must follow the same requirements as those used for creating a view. Refer **CTE query definitions* section in following link on MSDN
Also, because a CTE is basically a named resultset and it can be used like any table in SELECT, INSERT, UPDATE, or DELETE statements, you always have the option of using the various operators you mentioned when you use the CTE in any of those statements.
With regarding to
2.If I need to do some sort of recursion using some of these clauses, is my only alternative to write a recursive stored procedure?
Also, I am pretty sure that you can use at least some of the keywords that you have mentioned above in a view / CTE select statement. For Example: Refer to the use of GROUP BY in a CTE here in the Creating a simple common table expression example
Maybe, if you can provide a sample scenario of what you are trying to achieve, we can suggest some possible solution.
Having multiple indices for an SQL table, is there a way to know what index will be used automatically when using a specific query?
EDIT: I wanted the question to be general, but I mostly use MySQL, PostgreSQL and SQLite
Use the EXPLAIN PLAN statement to see what the dbms will do with your query.