Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

sql - How can I extract values where there are repeated values of fields?

From the following table,

userId passageId score
1      1         2
1      2         3
1      1         4
2      1         3
2      3         3
2      3         4

is it possible to extract the scores as shown below when the first two values of passageId are identical for each value of userId.

userId passageId score_1 score_2 
1      1         2          4
2      3         3          4
question from:https://stackoverflow.com/questions/65905792/how-can-i-extract-values-where-there-are-repeated-values-of-fields

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

DBFIDDLE

SELECT 
   userId,
   passageId,
   min(score) as score_1,
   max(score) as score_2
FROM mytable
GROUP BY    
   userId,
   passageId
HAVING COUNT(*)>=2;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...