marie20
marie20

Reputation: 783

Correlated Scalar SubQueries

I am using SparkSQL. I have the following table:

Department:

dep_id | emp_id | age
0 | 0 | 21
0 | 1 | 53
0 | 3 | 42
0 | 5 | 58
1 | 4 | 36
1 | 8 | 48
2 | 2 | 54
2 | 6 | 31
2 | 7 | 30
2 | 9 | 54

However, I am not being able to successfully execute the following query in SparkSQL:

SELECT   A.dep_id,
         A.emp_id,
         A.age,
         (SELECT  MAX(age) FROM department B WHERE A.dep_id = B.dep_id) max_age
FROM     department A
ORDER BY 1,2

Can anyone please help where I may be going wrong.

Thanks

Upvotes: 0

Views: 191

Answers (2)

GMB
GMB

Reputation: 222482

There is no need for a subquery, you can do this with window functions:

SELECT   dep_id,
         emp_id,
         age,
         MAX(age) OVER(PARTITION BY dep_id) max_age
FROM     department 
ORDER BY 1,2

Upvotes: 1

Yogesh Sharma
Yogesh Sharma

Reputation: 50163

You should declare table alias :

SELECT A.dep_id, A.emp_id, A.age,
      (SELECT MAX(b.age) FROM department B WHERE A.dep_id = B.dep_id) max_age
FROM department A;

Upvotes: 0

Related Questions