zoukankan      html  css  js  c++  java
  • 查询出多条记录,取最大或最小的某条件的记录,取唯一记录

    案例1:
    SELECT * FROM(
    SELECT t.*, max(t.visit_date) over(partition by t.cust_id) visit_date1 FROM(
    select a.cust_id, a.assess_result, max(a.visit_date) visit_date
    from yx_cust_guide a
    inner join yx_cust_guide b on a.cust_id = b.cust_id
    where a.visit_date < b.visit_date and a.status in('03','04','09')
    AND a.CUST_ID='1450260000207'

    group by a.cust_id, a.assess_result
    ) t
    ) t1 WHERE t1.visit_date = t1.visit_date1

    ORDER BY a.CUST_ID

    案例2:

    WITH tbl(ID, CRT_DATE, CRT_DPT, CRT_EMP, RECV_DPT, RECV_EMP, INFO_TYPE, DETAIL_TYPE, SUMMARY, STATUS, OPT) AS
    (
    --select id, crt_date, crt_dpt, crt_emp, recv_dpt, recv_emp,
    --info_type, detail_type, summary, status, opt
    --from (
    select i.id, replace(substr(i.crt_time,1,10),'-','') crt_date,
    o1.organ_name crt_dpt, o2.organ_name crt_emp,
    o3.organ_name recv_dpt, o4.organ_name recv_emp,
    i.info_type, d.detail_name detail_type, i.summary, i.status,
    '01' opt
    from yx_info_interact i
    left join pub_organ o1 on i.crt_dpt_id = o1.id
    left join pub_organ o2 on i.crt_emp_id = o2.id
    left join pub_organ o3 on i.rece_dpt_id = o3.id
    left join pub_organ o4 on i.rece_emp_id = o4.id
    left join yx_info_type_detail d on i.type_detail = d.id
    where 1=1
    union
    select i.id, replace(substr(i.crt_time,1,10),'-','') crt_date,
    o1.organ_name crt_dpt, o2.organ_name crt_emp,
    o3.organ_name recv_dpt, o4.organ_name recv_emp,
    i.info_type, d.detail_name detail_type, i.summary, i.status,
    '02' opt
    from yx_info_interact i
    left join pub_organ o1 on i.crt_dpt_id = o1.id
    left join pub_organ o2 on i.crt_emp_id = o2.id
    left join pub_organ o3 on i.rece_dpt_id = o3.id
    left join pub_organ o4 on i.rece_emp_id = o4.id
    left join yx_info_type_detail d on i.type_detail = d.id
    where 1=1
    union
    select i.id, replace(substr(i.crt_time,1,10),'-','') crt_date,
    o1.organ_name crt_dpt, o2.organ_name crt_emp,
    o3.organ_name recv_dpt, o4.organ_name recv_emp,
    i.info_type, d.detail_name detail_type, i.summary, i.status,
    '03' opt
    from yx_info_interact i
    left join pub_organ o1 on i.crt_dpt_id = o1.id
    left join pub_organ o2 on i.crt_emp_id = o2.id
    left join pub_organ o3 on i.rece_dpt_id = o3.id
    left join pub_organ o4 on i.rece_emp_id = o4.id
    left join yx_info_type_detail d on i.type_detail = d.id
    where 1=1
    --)t
    )
    --SELECT ROW_NUMBER () OVER (PARTITION BY t.id ORDER BY t.id), t.* FROM tbl t order BY id DESC;

    --SELECT ROW_NUMBER () OVER (order BY t.id) rec_id, t.* FROM tbl t order BY id DESC;

    SELECT * FROM(
    select t.*,max(t.status) over(partition by t.id) status1 from tbl t
    )t1 WHERE t1.status = t1.status1

    --select cno,sno,score from (
    --select t.cno,t.sno,t.score,max(t.score) over(partition by t.cno) score1 from sc t
    --)t1 where t1.score=t1.score1

  • 相关阅读:
    uva 11426 线性欧拉函数筛选+递推
    poj 2115 二元一次不定方程
    poj 2891 模线性方程组求解
    如何用Python写一个贪吃蛇AI
    Android 截屏与 WebView 长图分享经验总结
    这个时代,作为程序员,我为什么要学习小程序
    红包外挂史及AccessibilityService分析与防御
    揭密微信跳一跳小游戏那些外挂
    android录音实现不再担心—一个案例帮你解决你的问题
    区块链到底是个什么鬼?一幅漫画让你秒懂!
  • 原文地址:https://www.cnblogs.com/zmc/p/5589882.html
Copyright © 2011-2022 走看看