Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

postgresql - SQL Sorting table based on two interchangeable fields

I want to sort a table having 3 columns (time, source , recipient) by the order by which communication is being made. If the source and recipient are conversing together then it will list them by the time. The goal is to see the communication happening between similar people ordered by time.An example is as:

time|source|recipient

1   paul    amy
2   amy     paul
3   amy     paul
5   paul    jane
8   amy     paul
9   jane    paul
10  paul    amy
11  paul    jane

the end result would be like

1   paul    amy
2   amy     paul
3   amy     paul
8   amy     paul
10  paul    amy
5   paul    jane
9   jane    paul
11  paul    jane
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Your question is a bit vague. My educated guess is you want this:

SELECT *
FROM   tbl
ORDER  BY (GREATEST(source, recipient), LEAST(source, recipient), "time";

The manual about GREATEST and LEAST.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...