ParseExection error in Hive - hive

I am getting an error when executing a select statement in Hive.
Error: org.apache.hive.service.cli.HiveSQLException: Error while compiling statement: FAILED: ParseException line 1:33 cannot recognize input near '' '' ''
Query Sample: ;with cte1 as ( select * from table1)
, cte2 as ( select * from table2)
, cte3 as (select * from table3)
select * from cte2
join cte3 on cte2.col1 = cte3.col1
join cte1 on cte1.col1 = cte2.col1;

you are missing the alias reference, add the keywound "as" after your alias like
with cte1 as ( select * from table1) , cte2 as ( select * from table2) , cte3 as ( select * from table3) select * from cte2 join cte3 on cte2.col1 = cte3.col1 join cte1 on cte1.col1 = cte2.col1
Tested in hive 1.1

Related

How to fix Incorrect syntax error in CTE?

When I do this query, it gives me the error "incorrect syntax near t1".
The query works if it's not within the CTE. How can you do this in CTE?
WITH CTE
AS (
SELECT DISTINCT t1.id
FROM ticket t1
WHERE...
UNION
SELECT t1.id
FROM ticket t1
,ticket t2
WHERE t1.id = t2.matching_ticket_id
AND t2.id NOT IN (
SELECT ticket_id
FROM aa
,pa
WHERE aa.id = pa.account_id
)
) t1 --> incorrect syntax near t1
JOIN (
SELECT DISTINCT t1.id
FROM ticket t1
WHERE t1.id NOT IN (
SELECT ticket_id
FROM aa
,pa
WHERE aa.id = pa.account_id
)
) t2 ON t2.id = t1.id --> incorrect syntax near t2
UNION
(
SELECT DISTINCT t1.id
FROM cd_ticket t1
WHERE...
UNION
SELECT t1.id
FROM cd_ticket t1
,cd_ticket t2
WHERE t1.id = t2.matching_ticket_id
AND t2.id NOT IN (
SELECT ticket_id
FROM cd_aa
,cd_pa
WHERE aa.id = pa.account_id
)
) t1 --> incorrect syntax near t1
JOIN (
SELECT DISTINCT t1.id
FROM cd_ticket t1
WHERE t1.id NOT IN (
SELECT ticket_id
FROM cd_aa
,cd_pa
WHERE aa.id = pa.account_id
)
) t2 ON t2.id = t1.id --> incorrect syntax near t2
You are confusing "table expressions" with "common table expressions", so you are mixing the syntax of both of them, into something that is not a legal SQL statement.
See the difference, and make up your mind on which one to use:
Table Expression (aka "derived table", "inline view", etc.):
select *
from (
select ... -- my complex select #1 here
) a
join (
select ... -- my complex select #2 here
) b on a.column_a = b.column_b
Table expressions appear in the FROM clause and they are named just after the closing parenthesis. The SELECT statement is written at the beginning.
Common Table Expression (CTE):
with
a as (
select ... -- my complex select #1 here
),
b as (
select ... -- my complex select #2 here
)
select * from a join b on a.column_a = b.column_b
As you see Common Table Expressions are declared first, the aliases are defined before the open parenthesis, and they are separated by commas. The main SELECT is written at the end. One advantage of CTEs is that you can use the common table expressions many times, in subsequent CTEs, and in the final/main SELECT.
It looks like you are trying to JOIN directly to the CTE; that is not how they work.
If you want join to a CTE:
WITH CTE AS ( somequery )
SELECT stuff
FROM aTable AS a INNER JOIN CTE AS c ON a.somefield = CTE.somefield
If you want the CTE to represent two joined subqueries, they need to be within the CTE:
WITH CTE AS (
SELECT stuff
FROM (
subquery1
) AS q1
INNER JOIN (
subquery2
) AS q2 ON ....)
SELECT stuff
FROM CTE
You declare a CTE but then go straight to a union which requires a preceding SELECT.
Your second query has a unncessary trailing bracket with a t1 alias but I assume you want that as a derived table so It will need an opening bracket before the SELECT
You probably want something like this
WITH CTE AS
(
select distinct t1.id from ticket t1 where ...
)
SELECT id FROM CTE
union
SELECT * FROM
(
select t1.id
from ticket t1,
ticket t2
where t1.id = t2.matching_ticket_id and
t2.id not in ( select ticket_id from aa, pa where aa.id = pa.account_id )
) t1
join
etc......
Also, as a side, you should steer away from the old JOIN syntax:
SELECT ticket_id
FROM aa, pa
WHERE aa.id = pa.account_id
and re-write as
SELECT ticket_id
FROM aa
JOIN oa
ON aa.id=pa.account_id

Unexpected results from SELECT FROM WHERE X NOT IN Y

So...
select COUNT(*) cnt from docDocument d
inner join tblAttachment a on d.AttachmentID = a.ID
where
a.ContainerID = 1
Returns 6673
select COUNT(*) from tblAttachment
where
ContainerID = 1
Returns 10372
select COUNT(*) cnt from docDocument d
right join tblAttachment a on d.AttachmentID = a.ID
where
a.ContainerID = 1
AND
d.ID IS NULL
Returns 3699 which makes sense as 10372 - 6673 = 3699
SELECT COUNT(*) FROM
(
select ID from tblAttachment a
where
a.ContainerID = 1
Except
(
SELECT AttachmentId from docDocument
)
) tst
Unsurprisingly returns 3699... However...
select COUNT(*) from tblAttachment a
where
a.ContainerID = 1 AND
a.ID NOT IN
(
SELECT d.AttachmentId from docDocument d
)
I was expecting this to return 3699 but surprisingly it's returning 0.
Can anyone explain these results?
If the subquery returns a null value, the NOT IN is no longer true, and no rows are returned.
Either aviod null values to be returned:
select COUNT(*) from tblAttachment a
where
a.ContainerID = 1 AND
a.ID NOT IN
(
SELECT d.AttachmentId from docDocument d WHERE d.AttachmentId IS NOT NULL
)
Or switch to the "null safe" NOT EXISTS:
select COUNT(*) from tblAttachment a
where
a.ContainerID = 1 AND
NOT EXISTS
(
SELECT * from docDocument d WHERE d.AttachmentId = a.ID
)
Your not in query return nulls value. So its displaying zero records.
This is the below scenario you are getting right now. Let's try to understand that and make the changes.
select *
into #tab1
from (select 'a' Name union all
select 'b'union all
select 'c'union all
select 'd'union all
select 'e') AS A
select *
into #tab2
from (select 'd' Name union all
select 'e' union all
select NULL) AS A
Not Exists
select Name
from #tab1
where not exists (select *
from #tab2
where #tab1.Name = #tab2.Name )
Left Join
select t1.Name
from #tab1 t1
left join #tab2 t2 on t1.Name = t2.Name
where t2.Name is null
Not in
select *
from #tab1
where Name not in (select Name from #tab2)
Note: whenever a Not in query returns NULL, the outer query returns blank data.
You must be having NULL values in docDocument table...That's why you are not getting anything.
Please try like this USE NOT EXISTS
select COUNT(*) from tblAttachment a
where
a.ContainerID = 1 AND
NOT EXISTS
(
SELECT d.AttachmentId from docDocument d
WHERE a.ID = d.ID
)
You can use EXCEPT operator.
select * into #tab1 from (
select 'a' Name union all
select 'b'union all
select 'c'union all
select 'd'union all
select 'e'
)AS A
select * into #tab2 from
(
select 'd' Name union all
select 'e' union all
select NULL
)AS A
SOLUTION
select Name from #tab1
EXCEPT
select * from #tab2
OUTPUT
Name
----
a
b
c
(3 rows affected)

SQL Server why Incorrect syntax near the keyword 'select'

select *
from
(select *
from
(select t1.stdid
from t1) as table1
cross join
select *
from
(select t2.Subid
from t2) as table2
) as table3
Try the query below. You included an addition select * from that you should have left out.
select *
from (
select *
from (
select t1.stdid
from t1
) as table1
cross join
-- select * from : This line is extraneous and is causing your error.
(
select t2.Subid
from t2
) as table2
) as table3
Alternatively, you will get the same result if your query was written as:
select *
from (
select t1.stdid
from t1
) as table1
cross join
(
select t2.Subid
from t2
) as table2
Please don't use * in the query. Just select the column which you want.Please see below:
select ID
from
(select t1.id as ID
from
(select t1.id
from t1) as t1
cross join
(select t2.id
from t2) as t2
) as table3

SELECT Row Values WHERE MAX() is Column Value In GROUP BY Query

How can I select like this? Can I create a User defined Aggregate Function
SELECT Max(A),(SELECT TOP 1 FROM TheGroup Where B=Max(A)) FROM MyTable
where MyTable as Shown Below
A B C
--------------
1 2 S
3 4 S
4 5 T
6 7 T
I want a Query Like this
SELECT MAX(A),(B Where A=Max(A)),C FROM MYTable GROUP BY C
I'm Expecting the result as below
MAX(A) Condition C
-----------------------
3 4 S
6 7 T
SELECT A,B,C FROM
(SELECT *, ROW_NUMBER() OVER (PARTITION BY C ORDER BY A DESC) RN FROM MyTable)
WHERE RN = 1
(this query will always return only one row per C value)
OR
WITH CTE_Group AS
(
SELECT C, MAX(A) AS MaxA
FROM MyTable
GROUP BY C
)
SELECT g.MaxA, t.B, g.C
FROM MyTable t
INNER JOIN CTE_Group g ON t.A = g.MaxA AND t.C = g.C
(if there are multiple rows that have same Max(A) value - this query will return all of them)
Try Following Query :
SELECT TABLE1.A , TABLE2.B , TABLE1.C
FROM
(
SELECT MAX(A) AS A,C
FROM MYTable
GROUP BY C
) AS TABLE1
INNER JOIN
(
SELECT *
FROM MYTable
) AS TABLE2 ON TABLE1.A = TABLE2.A
SQLFIDDLE
you can do it by simple join query . join query always run faster then In query . Join query run only one time at the time of execution of the query . we can archive same result by using IN query .
SELECT t1.*
FROM YourTable t1
Left Outer Join YourTable t2 on t1.C=t2.C AND t1.A < t2.A
WHERE t2.A is null
how about this:
SELECT *
FROM MyTable
WHERE A IN (SELECT MAX(A) FROM MyTable GROUP BY C)
SELECT Max(A)
FROM MyTable
Where B=(SELECT Max(A) FROM MyTable)
update:
SELECT *
FROM MyTable
Where B=(SELECT Max(A) FROM MyTable)
update 2:
SELECT DISTINCT A, B
FROM MyTable
Where A=(SELECT Max(A) FROM MyTable GROUP BY C)
update 3:
ok, I think I understand what you're looking for now.. How about this:
SELECT *
FROM MyTable
Where A in (SELECT Max(A) FROM MyTable GROUP BY C)
WITH
cte AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY cola desc) AS Rno,
*
FROM
tbl
)
SELECT top 1
cola,colb
FROM
cte
order by Rno
Then try it:
WITH
cte AS
(
SELECT
ROW_NUMBER() OVER (PARTITION BY col3 ORDER BY col1 desc) AS Rno,
*
FROM
tbl
)
SELECT
col1,col2,col3
FROM
cte
WHERE Rno=1

Join Table1 with Table2 using Table1 column values in Table2's conditionals

I have the following select statement...
SELECT ROW_NUMBER() OVER(order by cola) as [id], cola
FROM Table1
That makes up my table of all values I'm wanting to insert as #var, right now it works but I have to specify #var each time...
SELECT #var AS [Cola], (
SELECT COUNT(*)
FROM vwTableA AS Z
WHERE Cola = COALESCE(#var,Cola)
AND NOT EXISTS (
SELECT *
FROM TableB
WHERE Colb = Z.Colb
)
) AS [NewCol1],
(
SELECT COUNT(*)
FROM vwTableB AS Y
INNER JOIN TableC AS C
ON Y.Colc = C.Colc
WHERE Y.Cola = #var
) AS [NewCol2],
(
SELECT COUNT(*)
FROM vwTableC AS X
INNER JOIN TableD AS D
ON X.Colc = D.Colc
WHERE X.Cola = #var
) AS [NewCol3]
So I'm wanting to run this second select through all the values of "cola" from the first Select/Table I showed, instead of having to specify the #var and it only return one row each time. How can I do this?
If you use a Common Table Expression CTE you can use it to join to your other statement
with var as
( SELECT ROW_NUMBER() OVER(order by cola) as [id], cola
FROM Table1)
SELECT var.id AS [Cola], (
SELECT COUNT(*)
FROM vwTableA AS Z
WHERE Cola = COALESCE(var.id,Cola)
AND NOT EXISTS (
SELECT *
FROM TableB
WHERE Colb = Z.Colb
)
) AS [NewCol1],
(
SELECT COUNT(*)
FROM vwTableB AS Y
INNER JOIN TableC AS C
ON Y.Colc = C.Colc
WHERE Y.Cola = var.id
) AS [NewCol2],
(
SELECT COUNT(*)
FROM vwTableC AS X
INNER JOIN TableD AS D
ON X.Colc = D.Colc
WHERE X.Cola = var.id
) AS [NewCol3]
FROM var