简体   繁体   中英

How to find the exercise was interrupted person by sql please?

I have a table like:

person_id, pe_year, exercise_keep_year
1001, 2016, 12
1001, 2018, 14
1002, 2017, 5
1002, 2018, 0
1003, 2016, 10
1003, 2017, 0
1003, 2018, 1
1003, 2019, 2

If the person keep take exercise every year, then pe_year-exercise_keep_year will be all equal.(like person_id=1001, exercise_start_year = 2016-12 = 2018-14 = 2004)

If exercise was interrupted, pe_year-exercise_keep_year will be not all equal. (like person_id = 1003)

I want find all the person_id, which the exercise was interrupted. How to do that please?

Try this

select person_id from
(select person_id,count(distinct(pe_year-exercise_keep_year)) as cnt from tablename group by person_id) as a
where cnt>1
select person_id from t
group by person_id
having count( distinct pe_year - exercise_keep_year ) <> 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.

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