简体   繁体   中英

Can we use correlated sub-query in the group clause?

I have a table t2 with field Col & a,b,c,d,e as records. I'm trying to get an output like:

r1      0   1
1       b   a
2       d   c
4       e

when i use the below query i get an error: Syntax error in the expresion (((Select Count(b.Col)+1 from t2 as b where a.col>b.col)+1)\\2

Transform
first(col) as col1
Select  ((Select Count(b.Col)+1 from t2 as b where a.col>b.col)+1)\2 as r1
From t2 as a
Group  by (((Select Count(b.Col)+1 from t2 as b where a.col>b.col)+1)\2)
Pivot
(Select Count(b.Col)+1 from t2 as b where a.col>b.col) MOD 2

I don't think so. Just use a subquery:

select r1
from (select a.*,
             (Select Count(b.Col)+1 from t2 as b where a.col>b.col)+1)\2 as r1
      from t2 as a
     ) as a1
group by r1;

Or, because you are only selecting distinct values, use select distinct rather than group by in the original query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM