SQL asks Max and Min, but second big, second small how to calculate?

Source: Internet
Author: User

Use subqueries, but at a slower pace.

SELECTbaoming.id, Baoming.addtime as 'Registration Time', CONCAT (members.realname,members.username) as 'Customer Name', Sem.name as' channel source ', Case  whenBmcs_fd_time is  not NULL  Then"Split order"ELSE"No Order"END  as' Is it a single ', Case  when(SELECT COUNT(ID) fromCome_go_phone_logsWHEREbaoming_id=Baoming.id andConnect_status= 1   andCall_mode= 2)> 0  ThenOnELSE"Not Connected"END  as' is connected ', (SELECT COUNT(ID) fromCome_go_phone_logsWHEREbaoming_id=Baoming.id andCall_mode= 2) as 'Number of contacts',  (SELECT MIN(Call_time) fromCome_go_phone_logsWHEREbaoming_id=Baoming.id andCall_mode= 2) as 'First Contact Time',  (SELECT MIN(Call_time) fromCome_go_phone_logsWHEREbaoming_id=Baoming.id andCall_mode= 2   andCall_time>' First Contact time ') as '2nd Contact Time',  (SELECT MIN(Call_time) fromCome_go_phone_logsWHEREbaoming_id=Baoming.id andCall_mode= 2   andCall_time>' 2nd contact time ') as '3rd Contact Time',  (SELECT MIN(Call_time) fromCome_go_phone_logsWHEREbaoming_id=Baoming.id andCall_mode= 2   andCall_time>' 3rd contact time ') as '4th Contact Time' from' baomings ' asbaoming Left JOINCome_go_phone_logs asPhone onphone.baoming_id=baoming.id Left JOIN' Members ' as Members onPhone.seats_cs=Members . ' ID ' Left JOIN' Sem_sources ' asSem onSem.j_name=Baoming.bm_laiyuanWHEREBaoming.addtime>= '2017-07-01 00:00:00'    andBaoming.is_fp= 1    andBaoming.zx_tuijian= 1    andBaoming.addtime<= '2017-07-11 23:59:59'   andBaoming.idinch(SELECTBAOMING_ID,COUNT(ID) asC fromCome_go_phone_logsWHEREConnect_status= 1   andCall_mode= 2  )   GROUP  byBaoming.id

SQL asks Max and Min, but second big, second small how to calculate?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.