How do i save an alias name of one table in SQL (bigquery) to use in later queries?. there must be a way to avoid having to write the entire table name for each query. in this example ,id like to save the nameof the table as Citibike_station. how can i transfer this alias name to use in a different query?
FROM bigquery-public-data.new_york_citibike.citibike_stations` AS citibike_stations
not sure exactly what to do here. I've tired researching it online but i haven't found a satisfactory answer
ex:
"select column from table t"
From now on, you can reference "t" on your query and it will be interpreted as the table you are looking for.
"AS" should still be used for columns
For table alias visibility, you can refer to here.
FROM clause aliases are not visible to subqueries in the same FROM clause.
But if you want to use a short alias instead of fully qualified table name,
you might consider CTE (common table expression, or somtimes called as named subquery) or a temp table like below. (I don't prefer though)
WITH citibike_stations AS (
SELECT * FROM `bigquery-public-data.new_york_citibike.citibike_stations`
),
subquery_1 AS (
SELECT * FROM citibike_stations
),
subquery_2 AS (
SELECT * FROM (
SELECT 1 AS dummy
) CROSS JOIN citibike_stations
)
SELECT * FROM subquery_2 LIMIT 10;
For multiple select statements,
CREATE TEMP TABLE citibike_stations AS
SELECT * FROM `bigquery-public-data.new_york_citibike.citibike_stations`;
-- now you can use a shorthand table name instead of FQTN.
SELECT * FROM citibike_stations LIMIT 1;
SELECT * FROM (
SELECT 1 AS dummy
) CROSS JOIN citibike_stations LIMIT 1;
Related
I have 2 tables. Let's say Table A and Table B. Table A has a column called "name". Table B also has a column "name". I want to find out the count(distinct name). Name should take values from both the columns.
For ex-
Table A
name
A
B
C
Table B
name
A
B
D
Output should be 4.
The best concept is, first combine the data in the way you want using a subquery, and then dedupe or do the 2nd step.
For example,
WITH COMBINED AS (
SELECT
name
FROM
TableA
UNION ALL
SELECT
name
FROM
TableB
)
SELECT
DISTINCT name
FROM
COMBINED
In your situation, the 2nd step can be accomplished by changing UNION ALL to a UNION. This will dedupe the values automatically. You won't even need a subquery or a 2nd step. But I wanted to teach you the concept because it comes up often.
SELECT name FROM TableA
UNION
SELECT name FROM TableB
Then UNION in the CTE will reove all Duplicates
so a COUNT(*) will suffoce
WITH CTE AS (
SELECT name FROM TableA
UNION
SELECT name FROM TableB
)
SELECT COUNT(*) FROM CTE
I hope this query should do it:
SELECT SUM(names) AS total_names
FROM (
SELECT COUNT(DISTINCT(name)) as names FROM TableA
UNION
SELECT COUNT(DISTINCT(name)) as names FROM TableB
) t;
Note: Tested with sql server
Yet another option:
select hll_count.merge(hll_sketch) names
from (
select hll_count.init(name) hll_sketch from tableA
union all
select hll_count.init(name) from tableB
)
HLL++ functions are approximate aggregate functions. Approximate aggregation typically requires less memory than exact aggregation functions, like COUNT(DISTINCT), but also introduces statistical error. This makes HLL++ functions appropriate for large data streams for which linear memory usage is impractical, as well as for data that is already approximate.
See more about benefits of using HyperLogLog++ functions
I want to assign a value to my variable inside a CTE and use it in my next CTE.
Is it possible?
declare #TypeQty as int
;
With CTE1 as
(#TypeQty = (select Count(ID) as MyQty from MyTbl))
,CTE2 as
(select * from MyTbl2 where Qty= #TypeQty)
Select * from CTE2
I know it possible with some way else, but I want to know is it possible with CTE or not?
In SQL Server you can't use this syntax as you wrote in the question.
You can use the calculated value without the variable, just by referencing the CTE itself. Something like this:
WITH
CTE1
as
(
select Count(ID) as MyQty
from MyTbl
)
,CTE2
as
(
select *
from
MyTbl2
INNER JOIN CTE1 ON MyTbl2.Qty = CTE1.MyQty
)
Select *
from CTE2
;
This is explicitly not allowed in SQL Server. A query either returns a result set or (exclusively) assigns variables, but not both.
I do wonder why you would use either CTEs or variables for this example. A simple subquery seems simple enough:
Select t2.*
from MyTbl2 t2
where t2.qty = (select count(*) from mytable t);
CTEs in SQL Server do not offer performance benefits. And I don't see any "clarity-of-code" benefits to using them in this case.
No, it's impossible to populate variables inside a cte query definition.
The cte query definition must return a result set, and it's impossible to do both in T-SQL - a select statement can either return a result set or populate variables - but not both at the same time.
Therefor, it's impossible to populate variable inside a common table expression query.
You can use SUBQUERY Instead of using Recursive CTE
SELECT * FROM MYTBL2 M
INNER JOIN(
SELECT COUNT(ID) AS MYQTY FROM MYTBL) S ON M.QTY=S.MYQTY
From this helpful response on another question on stackoverflow.com (How do I count unique items in field in Access query?), the answer contained:
SELECT Count(*) AS N
FROM
(SELECT DISTINCT Name FROM table1) AS T;
Why is the distinct selection aliased "As T" (i.e. not why the letter T, but why do this?)
If you had the case of:
SELECT Count(Long_Table_Name_Here.[Field1]) AS N
FROM
(SELECT DISTINCT Field1 FROM Long_Table_Name_Here) AS T;
How could you also alias the long table name?
Because MS Access requires that every "object" referred to in the FROM clause has a name. Tables have a default name -- which is their name. (Actually, you can use either multi-part name or just the table name.)
As for your second query:
SELECT Count(Long_Table_Name_Here.[Field1]) AS N
FROM (SELECT DISTINCT Field1 FROM Long_Table_Name_Here) AS T;
This will result in a syntax error because Long_Table_Name_Here is not defined. You need to use the table alias defined in the FROM clause:
SELECT Count(T.[Field1]) AS N
FROM (SELECT DISTINCT Field1 FROM Long_Table_Name_Here) AS T;
Good day everyone! Firstly, I'm sorry for my poor english. Well, I've got a question that you can read in the title of this message.
SQL Server returns this message(Error 253) when I'm trying to select necessary data.
Translate "Recursive element from CTE (which name is 'recurse' - my
note) has multiple reference in CTE.
How can I solve this problem?
Can you advice me how to join two tables (with 2 columns(for example : a and b) which are the result of previous recursive select (I'm writing about the same select, but about another iteration of if)
with recurse (who_acts,on_whom_influence)
as
(
-------------------------------------------FIRST SELECT
select distinct interface_1.robot_name as who_acts,interface_2.robot_name as on_whom_influence
from INTERFACE as interface_1,INTERFACE as interface_2
where (interface_1.number in ( select INPUT_INTERFACE.source
from INPUT_INTERFACE
)
and interface_2.number in (
select INPUT_INTERFACE.number
from INPUT_INTERFACE
where (INPUT_INTERFACE.source=interface_1.number )
)
)
-------------------------------------------RECURSIVE PART
union all
select rec1.who_acts,rec1.on_whom_influence
from recurse as rec1
inner join
(select rec2.who_acts,rec2.on_whom_influence
from recurse as rec2) as P on (1=1)
)
select * from recurse
The problem is in recurse CTE.The connecting condition is not simple, but it have no
influence on this problem.
Can you type some working code in comments
Here's a dummy table
create table tbl1 ( a int, b int );
insert tbl1 select 1,2;
insert tbl1 select 11,12;
insert tbl1 select 2,3;
insert tbl1 select 4,5;
And a similar query to yours
with cte as (
select a,b from tbl1
union all
select x.a,x.b from cte x join cte y on x.a=y.a+1
)
select * from cte;
The error:
Recursive member of a common table expression 'cte' has multiple recursive references.: with cte as ( select a,b from tbl1 union all select x.a,x.b from cte x join cte y on x.a=y.a+1 ) select * from cte
Basically, the error is exactly what it says. You cannot have a recursive CTE appear more than ONCE in a recursive section. Above, you see CTE aliased as both x and y. There are various reasons for this limitation, such as the fact that CTEs are recursed depth-first and not generation-by-generation.
What you should think about is why you would need it more than once. Your recursive portion doesn't make sense.
select rec1.who_acts,rec1.on_whom_influence
from recurse as rec1
inner join
( select rec2.who_acts,rec2.on_whom_influence
from recurse as rec2) as P on (1=1)
On the surface, the following are true if recurse were a real table (non-CTE):
The number of rows generated is count(recurse as [rec1]) x count(recurse as [rec2]).
The rows in recurse (rec1) are each replicated per row in recurse, hence #1
Columns from rec2 are never used. rec2 serves only to multiply
If this were permitted to run, the recursive portion of the query would keep quadratically increasing its number of rows and never finish.
I would like to do something like
(SELECT ... FROM ...) AS my_select
WHERE id IN (SELECT MAX(id) FROM my_select GROUP BY name)
Is it possible to somehow do the "AS my_select" part (i.e. assign an alias to a SELECT statement)?
(Note: This is a theoretical question. I realize that I can do it without assign an alias to a SELECT statement, but I would like to know whether I can do it with that.)
Not sure exactly what you try to denote with that syntax, but in almost all RDBMS-es you can use a subquery in the FROM clause (sometimes called an "inline-view"):
SELECT..
FROM (
SELECT ...
FROM ...
) my_select
WHERE ...
In advanced "enterprise" RDBMS-es (like oracle, SQL Server, postgresql) you can use common table expressions which allows you to refer to a query by name and reuse it even multiple times:
-- Define the CTE expression name and column list.
WITH Sales_CTE (SalesPersonID, SalesOrderID, SalesYear)
AS
-- Define the CTE query.
(
SELECT SalesPersonID, SalesOrderID, YEAR(OrderDate) AS SalesYear
FROM Sales.SalesOrderHeader
WHERE SalesPersonID IS NOT NULL
)
-- Define the outer query referencing the CTE name.
SELECT SalesPersonID, COUNT(SalesOrderID) AS TotalSales, SalesYear
FROM Sales_CTE
GROUP BY SalesYear, SalesPersonID
ORDER BY SalesPersonID, SalesYear;
(example from http://msdn.microsoft.com/en-us/library/ms190766(v=sql.105).aspx)
You can do this using the WITH clause of the SELECT statement:
;
WITH my_select As (SELECT ... FROM ...)
SELECT * FROM foo
WHERE id IN (SELECT MAX(id) FROM my_select GROUP BY name)
That's the ANSI/ISO SQL Syntax. I know that SQL Server, Oracle and DB2 support it. Not sure about the others...
Yes, but you can select only one column in your subselect
SELECT (SELECT id FROM bla) AS my_select FROM bla2
You could store this into a temporary table.
So instead of doing the CTE/sub query you would use a temp table.
Good article on these here http://codingsight.com/introduction-to-temporary-tables-in-sql-server/