I want subquery result to be part of main query , is this possible in hive
select distinct table1.col1,table1.col2,
calcolumn=
(select count(table1.newcal)
from Table 1 table2
where table2.col1 = table1.col1
and table2.col2 = table1.col2)
from Table 1 table1
I see error as below
Error while compiling statement: FAILED: ParseException line 3:1
cannot recognize input near '(' 'select' 'count' in expression
specification
Use explicit join something like this:
select t1.col1,t1.col2,
count(distinct case when t2.col1 is not null then t1.newcal else null end) as calcolumn
from Table 1 t1
left join table2 t2 on t2.col1 = t1.col1 and t2.col2 = t1.col2
group by t1.col1,t1.col2
Related
SELECT T1.COL1
,CASE
WHEN T1.COL2 = 111
THEN 'A'
WHEN T1.COL2 = 222
THEN 'B'
ELSE 'C'
END AS DT
,T2.COL1
FROM TABLE1 T1
LEFT JOIN TABLE2 T2 ON T1.COL1 = DT;
error- invalid identifier DT
I want to use and verify the condition using case when alias in join condition which is giving error
NOTE - UPDATED CODE
SELECT T1.COL1
,CASE
WHEN T1.COL2 = 111
THEN 'A'
WHEN T1.COL2 = 222
THEN 'B'
ELSE 'C'
END AS DT
,T2.COL1
FROM TABLE1 T1
LEFT JOIN TABLE2 T2 ON T1.COL1 = CASE
WHEN T1.COL2 = 111
THEN 'A'
WHEN T1.COL2 = 222
THEN 'B'
ELSE 'C'
END;
THis one is working. Any other way other than this?
Unfortunately your new query still makes no sense. As noted previously, your join does not involve any column from TABLE2. Perhaps you obfuscated table names to the point you added this logic error by accident? Here is one way to avoid the huge effort to copy/paste the case expression code.
with cte as (
select *,
case COL2
when 111 then 'A'
when 222 then 'B'
else 'C' end as DT
from dbo.TABLE1
)
select ...
from cte left join dbo.TABLE2 as t2
on cte.Col1 = cte.DT
order by ... ;
If this case expression is commonly used, you could create a computed column and use it for your "join". That does not address the logic flaw but does address reusability.
If you place the assembly of the custom column in a table expression, then the new column gets officially named and can be used on any other expression.
For example:
select t1.*, T2.COL1
from ( -- this is a "table expression" named "t1"
SELECT T1.COL1
,CASE
WHEN T1.COL2 = 111
THEN 'A'
WHEN T1.COL2 = 222
THEN 'B'
ELSE 'C'
END AS DT
FROM TABLE1
) t1
LEFT JOIN TABLE2 T2 ON T2.COL1 = t1.DT;
I want to convert a oracle query to a plain SQL query. In oracle the join condition will be done this way:
Select col1,col2,col3
from TABLE1,TABLE2
WHERE TABLE1.COL IN TABLE2.COL2
When I'm converting the above code to a plain SQL query:
Select col1,col2,col3
from TABLE1 INNER JOIN
TABLE2
ON TABLE1.COL IN TABLE2.COL2
I'm getting Syntax error: Expected "(" or keyword UNNEST but got identifier "TABLE2"
you should use a = and not IN ..
IN clause require ()
Select col1,col2,col3
from TABLE1
INNER JOIN TABLE2 ON TABLE1.COL = TABLE2.COL2
If col2 is an array, you can use:
SELECT col1, col2, col3
FROM TABLE2 t2 CROSS JOIN
UNNEST(t2.col2) as col2_element JOIN
TABLE1 t1
ON t1.COL = col2_element;
Update table1 t1
set t1. col1='op'
where t1.col2 in (select t2.col2 from table2 t2 where t1.col3 = t2.col3) ;
The above query is working in Oracle but not in SQL server.
Throwing error: incorrect syntax near t1.
You can't set an alias in the UPDATE clause in SQL Server. The correct syntax would be to drop the alias or alias the object in the FROM:
--Without Alias
UPDATE table1
SET col1 = 'op'
WHERE EXISTS (SELECT 1
FROM Table2 AS T2
WHERE T2.col3 = table1.col3
AND T2.col2 = table1.col2);
--FROM and JOIN
UPDATE T1
SET col1 = 'op'
FROM Table1 AS T1
JOIN Table2 AS T2 ON T1.Col2 = T2.Col2
AND T1.Col3 = T2.Col3;
--With Aliases
UPDATE T1
SET col1 = 'op'
FROM Table1 AS T1
WHERE EXISTS (SELECT 1
FROM Table2 AS T2
WHERE T2.col3 = table1.col3
AND T2.col2 = table1.col2);
I am working on SQL 2008. I have two identical tables with same column names.
On Table2, i am missing some records. Some records got deleted in the Table2.
I have to compare Table1 and Table2 and retrieve only missing records from table1.
Use a LEFT JOIN and check for IS NULL like below. where t2.col2 is null will be TRUE only when there are records in table1 which are not present in table2. Which is what you are looking for. [This is a sample code and have no resemblance with your original query]
select t1.col1,t1.col2,t1.col3
from table1 t1
left join table2 t2 on t1.some_column = t2.some_column
where t2.col2 is null
You should use SQL Except. There is no Join involved.
Select * from dbo.TableA
Except
Select * from dbo.TableB
In set theory, the difference of sets A, B (A-B) is the set of elements that belong to A and do not belong to B.
With an " not exists", you have a solution :
select * from Table1 t1 where not exists (select 1 from Table2 t2
where t1.col1 = t2.col1
and t1.col2 = t2.col2
and t1.col3 = t2.col3
and ... // check here all columns ...
)
There is however a little problem in this solution in the case of null values, which can only be tested via a "IS NOT NULL" or "IS NULL", hence the complementary solution:
select * from Table1 t1 where not exists (select 1 from Table2 t2
where (t1.col1 = t2.col1 or (t1.col1 IS NULL and t2.col1 IS NULL))
and (t1.col2 = t2.col2 or (t1.col2 IS NULL and t2.col2 IS NULL))
and (t1.col3 = t2.col3 or (t1.col3 IS NULL and t2.col3 IS NULL))
and ... // check here all columns ...
)
i have sql like this:
select case when.....end as colA, table2.col3
from table1
join table2 on table2.colB = colA
in the case when statement, there are some other tables columns, is this doable? I have got error saying that colA is not valid.
Column aliases are not understood in the where or on clauses.
You can do this with a subquery, assuming the case only involves columns from table1:
select case when.....end as colA, t2.col3
from (select table1.*, (case when.....end) as colA
from table1
) t1 join
table2 t2
on t2.colB = t1.colA;
Otherwise, you could put the case statement in the on clause:
select case when.....end as colA, t2.col3
from (select table1.*, (case when.....end) as colA
from table1
) t1 join
table2 t2
on t2.colB = ( case when.....end );