Here I used With AS Clause.if i use SELECT query it is working fine but if i use insert query . it gives syntax error.
Can we use WITH ....INSERT in SAP HANA?
Code:
WITH t1 as
(
Select
col1,
col2,
col3
from table1),
t2 as
(
select
a.col4,
a.col5,
a.col1,
b.col3
from table2 a
left outer join t1
on a.col1 = b. col1)
insert into table3
select
c.col4,
c.col5,
c.col3
from t2;
In addition to Serban's correct answer, a general workaround for lack of CTE functionality is to create views instead.
In your case that could be:
create view t1 as
(select
col1,
col2,
col3
from
table1);
create view t2 as
(select
a.col4,
a.col5,
a.col1,
b.col3
from
table2 a
left outer join t1
on a.col1 = b. col1);
insert into table3
select
c.col4,
c.col5,
c.col3
from t2;
Based on my knowledge on HANA, CTEs (~ WITH-based queries) are currently not supported in INSERT clauses. This means that you should directly use sub-queries instead where possible.
IMO, the only scenario that is impossible to create without CTEs are recursive queries (which are not at all supported in HANA). As your query is not recursive, you can re-write and simplify it as follows:
INSERT INTO TABLE3
SELECT T2.COL4, T2.COL5, T1.COL3
FROM TABLE1 AS T1
LEFT OUTER JOIN TABLE2 AS T2
ON T1.COL1 = T2.COL1
Related
I have multiple tables which I need to merge into one after performing some operations into each one of them.
A first nesting was achieved thanks to a (working) "WITH" statement:
With
T1 as (Select col1, col2, col3,...
from *database*
where *condition*)
Select t2.col1, t2.col2, t2.col3, ...
From(
Select
d.col1, d.col2, d.col3,...
from *d*
where *conditions*
Group by d.col1, d.col2, d.col3,...) t2
Inner join T1
on t1.z = t2.x
Where t2.col1 = *condition*
and *conditions*
Group by t2.col1, t2.col2, t2.col3, ...
The problem arises when I try to expand on this and add more layers to the nest.
I have tried to do the following (changes to the previous code are marked in between "**"):
With
T1 as (Select col1, col2, col3,...
from *database*
where *condition*)**,**
**T2 as (**
Select t2.col1, t2.col2, t2.col3, ...
From(
Select
d.col1, d.col2, d.col3,...
from *d*
where *conditions*
Group by d.col1, d.col2, d.col3,...) t2
Inner join T1
on t1.z = t2.x
Where t2.col1 = *condition*
and *conditions*
Group by t2.col1, t2.col2, t2.col3, ...
**)**
**Select t3.col1 as qw, t3.col2 as qe, t3.col3 as qr,...**
**FROM(**
**Select**
**c.col1,**
**c.col2,**
**c.col3, ...**
**from *c***
**where *conditions) t3***
**Inner join t1**
**on t1.col3 = t3.qr**
**where t3.qe = *condition***
**group by t3.qw, t3.qe, t3.qr,...**
In return, I get the following error:
"t3.qr": invalid identifier"
Does anybody knows what the issue is and how can I fix it? I need to figure out how to nest multiple tables in some way, as, after these ones, more table will have to be added
When we write a query we have to write things in the right order.
firstly CTE `with cte_alias as (select ... from ...)
the SELECT: select column_1, column_2 with possible agregate or window functions: SUM, MAX, ROW_NUMBER() etc. There should only be one SELECT which is not in brackets
FROM tables (sub-queries) CTE's etc with JOIN if needed
WHERE conditions which must be boolean (either true or false)
GROUP BY
ORDER BY
only the select is obligatory in mySQL
SELECT Hello AS "speech"; is a valid mySQL query.
I was wondering about the efficiency of a couple of different queries. The task is to pull multiple tables together that must be equal from multiple columns different. I am curious about what is the best way to approach this from an efficiency standpoint.
I have already checked this out, but it doesn't say anything about multiple column where clauses
SQL WHERE.. IN clause multiple columns
and this solution shows doesn't comment on the efficiency or best practices of the solution, and doesn't include a solution where the final query is a join from the two tables
Two columns in subquery in where clause
select ID, col1, col2, col3
from table1 a
left join
(select ID, col1, col2, col3 from table2) b on a.col1 = b.col1
where a.col2 = b.col2
and a.col3 = b.col3
or
select ID, col1, col2, col3
from table1 a
left join
(select ID, col1, col2, col3
from table2) b on a.col1 = b.col1
and a.col2 = b.col2
and a.col3 = b.col3
You do not need to do a join on a sub-select. You were very close on the second sample query. Because of the join based on 3 columns, I would make sure that the second table also has a single index using all 3 columns for optimal performance. Ex: Index on ( col1, col2, col3 ), and not 3 individual indexes, one for each column.
Also, try not to use aliases like a, b, c, unless it really correlates to the name of your table like "Accounts a", "Business b", "Customers c". Use an alias on your table references such as the abbreviation more closely matches its source.
select
t1.ID,
t1.col1,
t1.col2,
t1.col3,
t2.WhatColumnFromSecondTable,
t2.AnotherColumnFromTable2,
t2.AnythingElse
from
table1 t1
left join table2 t2
on t1.col1 = t2.col1
and t1.col2 = t2.col2
and t1.col3 = t2.col3
Then, if you are only looking for specific things within, you would add a WHERE clause to further filter your data down.
Considering this query:
select distinct col1,col2
from table
where col1='x' and (col1,col2) not in (select col1,col2 from table1)
order by col1, col2
How can I achieve the above query in hive version 0.13 which does not support not in
I have tried to use minus, but apparently minus also is not supported
You can do this:
select distinct t.col1, t.col2
from table t left outer join table1 t1
on t.col1 = t1.col1 and t.col2 = t1.col2
where t.col1 = 'x' and
t1.col1 is null and t1.col2 is null
order by t.col1, t.col2;
Explanation: in a left outer join between table and table1, whenever columns for table1 are null, it means that the corresponding columns in table were not in table1.
Its already supported in the version 0.13, but with some limitations, see here. If you have older version your question is already answered here.
I have an sql command similar to below one.
select * from table1
where table1.col1 in (select columnA from table2 where table2.keyColumn=3)
or table1.col2 in (select columnA from table2 where table2.keyColumn=3)
Its performance is really bad so how can I change this command? (pls note that the two sql commands in the paranthesis are exactly same.)
Try
select distinct t1.* from table1 t1
inner join table2 t2 ON t1.col1 =t2.columnA OR t1.col2 = t2.columnA
This is your query:
select *
from table1
where table1.col1 in (select columnA from table2 and t2.keyColumn = 3) or
table1.col2 in (select columnA from table2 and t2.keyColumn = 3);
Probably the best approach is to build an index on table2(keyColumn, columnA).
It is also possible that in has poor performance characteristics. So, you can try rewriting this as an exists query:
select *
from table1 t1
where exists (select 1 from table2 t2 where t2.columnA = t1.col1 and t2.keyColumn = 3) or
exists (select 1 from table2 t2 where t2.columnA = t2.col1 and t2.keyColumn = 3);
In this case, the appropriate index is table2(columnA, keyColumn).
Assuming you're doing this in VFP, use SYS(3054) to see how the query is being optimized and what part is not.
Are the main query and subqueries fully Rushmore-optimisable?
Since the subqueries do not appear to be correlated (i.e. they don't refer to table1 then as long as everything is fully supported by indexes you should be fine.
I'm trying to determine if there is a better way to do this in SQL. My goal is to run one query which returns two values that are to be used for another query. See below
select *
from table2
where col1 =
( select col1
from table1
where id = 123 )
and col2 =
( select col2
from table1
where id = 123 );
Is there a way to simplify this code by either doing a where clause that checks both values against one nested query, or by running the first querying and somehow setting the values of col1 and col2 to variables that I can use in the second query?
You can do
select *
from table2
where (col1, col2) = (select col1, col2
from table1
where id = 123)
SELECT DISTINCT a.*
FROM table2 a
INNER JOIN table1 b
ON a.col1 = b.col1
AND a.col2 = b.col2
WHERE b.id = 123
you can simply use query as below
select t2.* from table2 t2,table1 t1 where t1.col1=t2.col1 and
t1.col2=t2.col2 and t1.id=123
Seems like you've got it backwards. Since you know exactly what you want from table1 (so, presumably, the query is smaller), you should start by getting the data from table1, then join the releveant rows from table2:
select table2.*
from table1
inner join table2
on table2.col1 = table1.col1
and table2.col2 = table1.col2
where table1.id = 123