SELECT
xfqti_virtuemart_products_pt_pt.virtuemart_product_id,
xfqti_virtuemart_product_medias.virtuemart_media_id
INTO #tempTable
FROM xfqti_virtuemart_products_pt_pt
Gives syntax error, I'm about to pull my hair off
Being Virtuemart, I'm guessing this is a MySQL database. If so, the correct syntax for creating a temp table is:
CREATE TEMPORARY TABLE IF NOT EXISTS tempTableName AS
(
SELECT field1, field2
FROM yourtable;
)
That being said, your SELECT statement has two fields from two different tables, but only one of those tables is mentioned in the FROM clause of your statement. They should really both be in there and JOINed. Something like:
CREATE TEMPORARY TABLE IF NOT EXISTS tempTableName AS
(
SELECT
t1.virtuemart_product_id,
t2.virtuemart_media_id
FROM
xfqti_virtuemart_products_pt_pt as t1
INNER JOIN xfqti_virtuemart_product_medias as t2 ON
t1.product_id = t2.product_id
)
Or something.. I can't see your tables and it's been years since I used Virtuemart, so it's just a guess at the table relationship.
Insert Into and Selecthave these sintax
Insert into your_Table (col1,col2)
SELECT
xfqti_virtuemart_products_pt_pt.virtuemart_product_id,
xfqti_virtuemart_product_medias.virtuemart_media_id
FROM xfqti_virtuemart_products_pt_pt
for create table
Create your_Table as
SELECT
xfqti_virtuemart_products_pt_pt.virtuemart_product_id,
xfqti_virtuemart_product_medias.virtuemart_media_id
FROM xfqti_virtuemart_products_pt_pt
Related
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!
I want to pass values from output of select query to another query. Basically both queries will be part of a stored procedure. e.g.
select Id, RelId
from tables
There will be multiple rows returned by above query and I want to pass them to the following query
select name
from table2
where Id = #Id and MgId = #RelId
Please suggest
You cannot pass multiple values in SQL.
But maybe you can just join your 2 tables, that would be far more efficient.
Not knowing your table schemes I suggest something like this. You might have to adapt this to your actual table schemas off course
select name
from table2 t2
inner join tables t on t2.Id = t.Id
and t2.MgId = t.RelId
EDIT
As Gordon mentioned in his answer, this approach can show double rows in your result.
If you don't want that than here are 2 ways of getting rid of the doubles
select distinct name
from ...
or by grouping by adding this at the end of the statement
group by name
Though this will work, avoiding the doubles like in Gordon's answer is better
I would suggest using exists:
select t2.name
from table2 t2
where exists (select 1
from tables t
where t2.Id = t.Id and t2.MgId = t.RelId
);
The difference between exists and join is that this will not generate duplicates, if there are multiple matches between the tables.
Or...
SELECT *
INTO #Table1
FROM ...
SELECT *
INTO #Table2
FROM ...
SELECT *
FROM #Table1 T1
JOIN #Table2 T2
DROP TABLE #Table1, #Table2
I have a scenario, for example:
with tmp as (select name from table1)
select * from table2 b
where b.name=(select max(name) from tmp)
However, Hive can't recognize this syntax, so is there any legal syntax for this?
After search, I learnt it can use join to realize:
select table2.* from table2
join (select max(name) as name from tmp) t2
where table2.name = t2.name
but I don't want to use join, as the join will be very slow, I just want to regard it as a reference.
Like in MySQL, you are able to set the result as a reference:
set #max_date := select max(date) from some_table;
select * from some_other_table where date > #max_date;
While Hive can achieve the effect that storing query result in shell. Check: HiveQL: Using query results as variables
Can Hive support such feature in SQL mode?
In Hive you can achieve it as below:
select * from table2 b
where b.name=(select max(name) from table1)
Other way :
You can also create temporary table in hive which will help to replicate your Oracle query above.
CREATE TEMPORARY TABLE tmp AS SELECT name FROM table1;
SELECT * FROM table2 b WHERE b.name=(SELECT max(name) FROM tmp);
I'm trying to dynamically create a variable or table in SQL which will store distinct values as a result for another sql query.
declare sample_table table
( values varchar(100))
insert into #sample_table values (select t1.value from my_tablw as t1 group by t1.value);
Supposing the distinct values in column value can change from table query to another table query, I want to store the result of this query in a user defined variable/table which can be used later in another query.
Depending on your definition of can be used later you can use a local temp table or table variable.... you just need to change the syntax a bit to not use the values since you are inserting from the results of a query. I also used DISTINCT below which is clearer than the GROUP BY without an aggregate function.
declare sample_table table ([values] varchar(100))
insert into #sample_table
select distinct t1.value
from my_tablw as t1
--one way to use it
select *
from newTable
where columnVal in (select * from #sample_table)
--another way to use it
select at.*
from anotherTable at
inner join #sample_table t on
t.column = at.column
--and another way...
select f.*
from finalTable f
where exists (select * from #sample_table t where t.column = f.column)
If you need this to be used outside the scope of your current batch, you'll need to use a persisted table or global temporary table.
I have the same table in two oracle databases. One is a staging database that has it's record loaded into the primary. I want to go through the staging table and see if there are no changes to the primary record. If there aren't then delete it. How can I do this?
To get the ones where something has changed you can use:
SELECT * FROM "staging_table"
MINUS
SELECT * FROM "table";
So, assuming that the table has a primary key then you can do
DELETE FROM "table"
WHERE primary_key_column
NOT IN
( SELECT primary_key_column
FROM (
SELECT * FROM "staging_table"
MINUS
SELECT * FROM "table"
)
);
Something like this should work
delete from stagingtable
where id in
(select id
from stagingtable st join productiontable pt on st.id = pt.id
and st.nextfield = pt.nextfield
etc
)