I can join two tables, but i am trying to save them as a new table but it keeps saying my select syntax is wrong?
I am extremely new to SQL server
Create table New_table AS
SELECT *
from Old_Table inner join id on Old_table.id=Other_table.id
You need SELECT ... INTO:
SELECT Old_table.id
, Old_table.foo
, Other_Table.bar
, Other_Table.foo AS baz -- foo has to be renamed, can't have two columns with the same name
INTO New_table
FROM Old_Table
INNER
JOIN Other_Table
ON Old_table.id = Other_table.id
Related
If it is possible, how to create one table/temp table out of multiple temp tables using SQL? Because creating a view is unfortunately not possible with temp tables.
For example, I have 3/more temp tables:
WITH a_table AS (
SELECT *
FROM a
JOIN b
WHERE ...
)
, b_table AS (
SELECT *
FROM c
JOIN d
WHERE ...
)
, c_table AS (
SELECT *
FROM c
JOIN d
WHERE ...
)
SELECT * FROM a_table
UNION
SELECT * FROM b_table
UNION
SELECT * FROM c_table
How do I create just one table instead? Or have one view for all these unioned temp tables?
Tried to create a view but that is not possible with temp tables and the query that I have has like 50 temp tables.
I am trying to create a some logic using CTE and then instead of using DML statement after CTE, I am trying to create a temp table using CTE. This is possible in T-SQL. Is it possible in GBQ?
I know I can create temp table instead of CTE in the below example, but just want to know the possibility!
WITH xyz AS
(SELECT * FROM table1)
CREATE TEMP TABLE temp1 AS (
SELECT * FROM xyz INNER JOIN table2 on ...);
Use below instead
CREATE TEMP TABLE temp1 AS (
WITH xyz AS
(SELECT * FROM table1)
SELECT * FROM xyz INNER JOIN table2 on ...
);
So in 2022 I believe that no longer works without a script or session in GBQ:
You could write your query as follows:
WITH xyz AS (
SELECT
*
FROM table1
)
SELECT
*
FROM xyz
INNER JOIN table2
ON ...
and then click the More Button -> Query Settings as shown below:
After that you can set a destination for your results a a temporary table and here you can define the name of your table etc. in your case it's temp1:
This way you can just save the results of your query into a temporary table. Hope it helps!
This query filters some IDs from a master table, and returns all data abpout that IDs from two other tables as two record sets:
WITH filteredIds (fid) AS
(
SELECT id
FROM MasterTable
WHERE <somecondition>
)
SELECT *
FROM Table1
RIGHT JOIN filteredIds ON (Table1.id = filteredIds.fid);
WITH filteredIds (fid) AS
(
SELECT id
FROM MasterTable
WHERE <somecondition>
)
SELECT *
FROM Table2
RIGHT JOIN filteredIds ON (Table2.id = filteredIds.fid);
This works so far, but it would be great to have just one WITH clause, since the condition is always the same. Further more, the condition is often written manually, since this is used to collect some diagnostics data.
But this
WITH filteredIds (fid) AS
(
SELECT id
FROM MasterTable
WHERE <somecondition>
)
SELECT *
FROM Table1
RIGHT JOIN filteredIds ON (Table1.id = filteredIds.fid);
SELECT *
FROM Table2
RIGHT JOIN filteredIds ON (Table2.id = filteredIds.fid);
does not work, SQL Server claims it does not know object filteredIds in the last query.
Have I overseen something, or doesn't it work that way? I guess the alternative would be a temporary table.
Once a CTE has been consumed in a query, it cannot be reused in another query. There is no direct workaround as far as I know. The closest thing perhaps to want you want would be to create a bona fide temporary table:
CREATE TABLE #temp (fid int);
INSERT INTO #temp (fid)
SELECT id FROM MasterTable WHERE <somecondition>;
Then, you may reuse the temporary table directly:
SELECT * FROM Table1 a RIGHT JOIN #temp b ON a.id = b.fid;
SELECT * FROM Table2 a RIGHT JOIN #temp b ON a.id = b.fid;
You can't do what you want with CTE. CTE doesn't exist outside the query where it was defined.
I think that the closest thing to what you want is called a "view".
CREATE VIEW filteredIds
AS
SELECT id AS fid
FROM MasterTable
WHERE <somecondition>
;
And now you can use that view in many other queries.
SELECT *
FROM Table1
RIGHT JOIN filteredIds ON (Table1.id = filteredIds.fid);
SELECT *
FROM Table2
RIGHT JOIN filteredIds ON (Table2.id = filteredIds.fid);
Furthermore, in SQL Server it will work exactly like a CTE in a sense that the list of filtered IDs is not materialized and the engine will optimize the whole query that references the view. It will inline the view definition into the outer / main query as if you wrote everything in one big query. Just like it would do with CTE.
With a temporary table you explicitly write this temporary result to disk and read it back.
In some cases it may be better, in some not.
I need to take the distinct values from Table 2 while joining with Table 1 in Hive. Because the table 2 has duplicate records.
Considering below join condition is it possible to take only distinct key_col from table 2? i dont want to use select distinct * from ...
select * from Table_1 a left join Table_2 b on a.key_col = b.key_col
Note: This is in Hive
Use Left semi join. This will give you all the record in table1 which exist in table2(duplicate record) without duplicates.
select a.* from Table_1 a left semi join Table_2 b on a.key_col = b.key_col
I have a table called Table1.
The table contains columns like:
The sourceID (ID of the join)
The sourceTable (the table to be joined table, this is variable)
My query:
SELECT T.Category, J.Tariff
FROM Table1 as T
INNER JOIN T.SourceTable J ---- This needs to be changed
ON T.sourceID = J.id
Is this possible?
Basically the source table can be different for every row
I'd say that's generally a bad idea. Even if possible, it basically prevents the optimiser from doing much (which can cause the query to slow down a lot).
I suggest changing your database design to make what you're trying to do easier (think having a base table which the tables you want to join on have a 1-to-1 mapping to) rather than trying to get this to work.
You can use Union, and filter by the column with the table pointer. Something like this:
SELECT T.Category, J.Tariff
FROM Table1 as T
INNER JOIN T.SourceTable1 J ON T.sourceID = J.id
where T.sourceTable = '1'
union
SELECT T.Category, J.Tariff
FROM Table1 as T
INNER JOIN T.SourceTable2 J ON T.sourceID = J.id
where T.sourceTable = '2'
Table names cannot be dynamic in an SQL Query. You have to find a different way to do this. Use a Stored Procedure and/or EXEC().
In principle it's possible using left joins, but only if you have a fixed set of lookup tables (otherwise, you'll need to build your SQL statement dynamically):
SELECT T.Category, coalesce(source1.tariff, source2.tariff) as tariff
FROM Table1 as T
LEFT OUTER JOIN T.source1
ON T.sourceID = source1.id and t.sourcetable = 'source1'
LEFT OUTER JOIN T.source2
ON T.sourceID = source2.id and t.sourcetable = 'source2'
but as mentioned in the other answers, that's usually a sign that your database design is flawed.
Try something like this:
1) Create a VIEW with all source tables:
CREATE VIEW source_tables AS
(SELECT 'table_1' as table_name,
id,
value
FROM table_1
UNION ALL
SELECT 'table_2' as table_name,
id,
value
FROM table_2
UNION ALL
..........
SELECT 'table_n' as table_name,
id,
value
FROM table_n);
2) Use the VIEW to get the values from source table:
SELECT T.category, S.value
FROM base_table as T
JOIN source_tables S ON T.sourceID = S.id AND T.SourceTable = S.table_name