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
124 views
in Technique[技术] by (71.8m points)

sql - Incredible select

Good day! Need help selecting. I can hardly imagine how to implement it. There is a table clients (client_id, name) There is a table contact (client_id, phone, phone_type (1,2,3,4,5,12), contact_flg (1,0), date_add (timestamp)) A task: For all client_id, find only one phone value with the following priority:

  1. Phones, where contact_flg = 1 with the earliest date of addition
  2. If the client does not have contact_flg = 1, then the phones with contact_flg = 0 and phone_type = 5 with the earliest date of adding
  3. If the client does not have phones from p. 1-2, then phones with contact_flg = 0 and phone_type = 2 with the earliest date of adding
  4. If the client does not have phones from p. 1-3, then the phones with contact_flg = 0 and phone_type = any value with the earliest date of addition.
  5. If there are no phones for the client at all - output null The first point can be done using row num and sorting, I understand that, but how can the other four fit?
question from:https://stackoverflow.com/questions/65936955/incredible-select

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

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

...