I have source table Test
contains id
column, target table testm
consist of id , col2
. Could please assist me to get rid of this error.
test (source)
id
---
10
10
20
20
20
30
30
40
Target
testm
id col2
-- ----
10 1
10 2
20 1
20 2
20 3
30 1
30 2
40 1
The query:
select id, (case id
when 10 then select count(id) from test where id =10
when 20 then select count(id) from test where id =20
when 30 then select count(id) from test where id =30
when 40 then select count(id) from test where id =40
else 0 END ) col2 from test
throws the error:
missing expression
From looking at the desired output I guess that you want to number each occurrence of an id by groups; if this is the case using the row_number analytical function should do what you want:
select id, row_number() over (partition by id order by id) as col2
from test
order by id;
See this sample SQL Fiddle
Given you sample source data this would be the output:
| ID | COL2 |
|----|------|
| 10 | 1 |
| 10 | 2 |
| 20 | 1 |
| 20 | 2 |
| 20 | 3 |
| 30 | 1 |
| 30 | 2 |
| 40 | 1 |
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.