{"id":237993,"date":"2020-12-28T09:14:37","date_gmt":"2020-12-28T01:14:37","guid":{"rendered":"http:\/\/4563.org\/?p=237993"},"modified":"2020-12-28T09:14:37","modified_gmt":"2020-12-28T01:14:37","slug":"%e6%b1%82%e5%8a%a9%e4%b8%80%e6%9d%a1-sql-%e8%af%ad%e5%8f%a5%e7%9a%84%e5%86%99%e6%b3%95%ef%bc%8c%e5%88%86%e7%bb%84%e6%9f%a5%e8%af%a2%ef%bc%8c%e6%b1%82%e6%af%8f%e4%b8%aa%e5%ad%a6%e7%94%9f%e7%ac%ac","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=237993","title":{"rendered":"\u6c42\u52a9\u4e00\u6761 SQL \u8bed\u53e5\u7684\u5199\u6cd5\uff0c\u5206\u7ec4\u67e5\u8be2\uff0c\u6c42\u6bcf\u4e2a\u5b66\u751f\u7b2c\u4e00\u6b21\u8003\u8bd5\u6210\u7ee9\u548c\u6700\u540e\u4e00\u6b21\u8003\u8bd5\u6210\u7ee9\u7684\u5dee\u503c"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u6c42\u52a9\u4e00\u6761 SQL \u8bed\u53e5\u7684\u5199\u6cd5\uff0c\u5206\u7ec4\u67e5\u8be2\uff0c\u6c42\u6bcf\u4e2a\u5b66\u751f\u7b2c\u4e00\u6b21\u8003\u8bd5\u6210\u7ee9\u548c\u6700\u540e\u4e00\u6b21\u8003\u8bd5\u6210\u7ee9\u7684\u5dee\u503c               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : LeeReamond <\/span>  <span><i><\/i> 6<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u76ee\u524d\u6709\u4e00\u8868\u5982\u4e0b\uff0c\u5171\u4e09\u5217<\/p>\n<pre><code>\u5b66\u751f ID   \u8003\u8bd5\u65f6\u95f4         \u6210\u7ee9 0        2020-01-01      60 0        2020-02-01      70 0        2020-03-01      80 1        2020-02-14      90 1        2020-03-15      80 <\/code><\/pre>\n<p>\u60f3\u8981\u6267\u884c\u4e00\u6761\u8bed\u53e5\u641c\u7d22\uff0c\u5f97\u5230\u5982\u4e0b\u7ed3\u679c<\/p>\n<pre><code>\u5b66\u751f ID     \u7b2c\u4e00\u6b21\u6210\u7ee9      \u6700\u540e\u4e00\u6b21\u6210\u7ee9   \u5dee\u503c 0          60             80            20 1          90             80            -10 <\/code><\/pre>\n<p>\u4e5f\u5c31\u662f\u60f3\u8981\u901a\u8fc7 group by \u6839\u636e\u5b66\u751f ID \u8fdb\u884c\u5206\u7ec4\uff0c\u4e4b\u540e\u53d6\u51fa\u67d0\u7ec4\u4e2d\u7684\u521d\u6b21\u548c\u672b\u6b21\u3002 \u5e73\u53f0 Oracle\uff0c\u770b\u4e86\u4e00\u4e0b Oracle \u5206\u7ec4\u67e5\u8be2\u7684\u6559\u5b66\uff0c\u4f3c\u4e4e Oracle \u5206\u7ec4\u91cc\u9762\u53ea\u6709 max\uff0cmin\uff0cavg \u4e4b\u7c7b\u7684\u51fd\u6570 \u6ca1\u6709\u529e\u6cd5\u6309\u987a\u5e8f\u53d6\u67d0\u6761\u67d0\u6761\u5417\uff1f\u6709\u6ca1\u6709\u5927\u4f6c\u8bb2\u89e3\u4e00\u4e0b\uff0c\u8c22\u8c22<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>20<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4775317\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : kiracyan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             order by \u8003\u8bd5\u65f6\u95f4                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4775318\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : loliordie <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Order by \u8003\u8bd5\u65f6\u95f4 \u548c order by \u8003\u8bd5\u65f6\u95f4 desc \u7ec4\u5408\u8d77\u6765\u5373\u53ef                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4775319\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : beichenhpy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             mysql \u7684\u8bdd\u6211\u60f3\u662f \u6309\u7167\u65f6\u95f4 asc desc \u7136\u540e limit 1 ? \u4e24\u4e2a\u7ed3\u679c union all \u4e00\u4e0b\uff1f\u3002\u3002Oracle \u6ca1\u7528\u8fc7\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4775320\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : LeeReamond <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @kiracyan <br \/>@loliordie <br \/>order by \u4e4b\u540e\u5462\uff0c\u5982\u4f55\u5728\u5206\u7ec4\u4e2d\u53d6\u51fa\u5f00\u5934\u7ed3\u5c3e\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4775321\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : loading <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6700\u597d\u8bf4\u4e00\u4e0b\u662f\u54ea\u578b\u6570\u636e\u5e93\uff0c\u6709\u7279\u6b8a\u8bed\u6cd5\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4775322\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : loading <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             oracle \u8981\u7528 row number \u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4775323\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : qiayue <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6709\u4e00\u4e2a\u7279\u522b\u7b80\u5355\u7684\u529e\u6cd5\uff0c\u7528\u6237\u8868\u52a0\u4e24\u4e2a\u5b57\u6bb5\uff0c\u9996\u6b21\u8003\u8bd5\u5206\u6570\u548c\u6700\u540e\u4e00\u6b21\u8003\u8bd5\u5206\u6570\uff0c\u5b9e\u9645\u4ea7\u54c1\u4e2d\uff0c\u8fd9\u662f\u6700\u4f73\u89e3\u6cd5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4775324\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yeqizhang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e3b\u81ea\u5df1\u627e\u5230\u89e3\u6cd5\u7684\u8bdd\u5206\u4eab\u4e00\u4e0b\u54c8\uff0c\u6211\u4e5f\u5b66\u4e60\u5b66\u4e60                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4775325\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : l00t <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u591a\u5e74\u4e0d\u5199\u6709\u4e9b\u5177\u4f53\u7684\u5fd8\u8bb0\u4e86\uff0c\u6211\u5c31\u63d0\u51e0\u4e2a\u5173\u952e\u5b57\uff0c\u4e3b\u4f60\u81ea\u5df1\u53bb\u67e5\u4e00\u4e0b\u8865\u5168\u5427\u3002<\/p>\n<p>first_value(\u6210\u7ee9)over(partition by ID order by \u65f6\u95f4), last_value(\u6210\u7ee9)over(partition by ID order by \u65f6\u95f4)                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4775326\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : LeeReamond <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @l00t \u611f\u8c22\u5927\u4f6c\uff0c\u6210\u529f\u5b9e\u73b0\u4e86\u3002\u4e0d\u8fc7\u8fd9\u4e2a\u6211\u505a\u51fa\u6765\u7684\u7ed3\u679c\u662f\uff0c\u6bd4\u5982\u6e90\u6570\u636e\u5e93\u5171\u6709\u4e24\u79cd\u5b66\u751f ID\uff0c\u90a3\u4e48\u6211\u671f\u671b\u8f93\u51fa\u4e24\u884c\uff0c\u4f46\u662f\u4ed6\u5b9e\u9645\u8f93\u51fa\u7684\u884c\u6570\u4e0e\u8003\u8bd5\u6b21\u6570\u76f8\u7b49\uff0c\u7136\u540e\u6bcf\u4e00\u884c\u6570\u636e\u90fd\u4e00\u6837\uff0c\u8fd9\u79cd\u7ed3\u679c\u51fa\u6765\u600e\u4e48\u5904\u7406\uff1f<\/p>\n<p>\u8fdb\u884c group by \u5417\uff1f\u611f\u89c9\u53c8\u6d6a\u8d39\u4e86\u4e00\u6b21\u7b97\u529b\u3002\u751f\u4ea7\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4775327\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : dzdh <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @LeeReamond \u6700\u4f73\u5b9e\u8df5\u770b#7                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4775328\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : l00t <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @LeeReamond #10 \u52a0\u4e2a distinct                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4775329\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : isnullstring <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @qiayue \u54c8\u54c8\u54c8\uff0c\u6ca1\u6709\u6bd4\u9759\u6001\u6570\u636e\u6765\u66f4\u5feb\u7684\u89e3\u6cd5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4775330\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ebony0319 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             select studentId,max(score),min(score),max(score)-min(score) from score s where 1=(<br \/> select count(1) from score s1 where s.score&gt;=s1.score and s.studentId=s1.studentId<br \/> ) or 1=(<br \/> select count(1) from score s1 where s.score&lt;=s1.score and s.studentId=s1.studentId<br \/> ) group by studentId                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4775331\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ebony0319 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             [![rqRyNj.png]( https:\/\/s3.ax1x.com\/2020\/12\/30\/rqRyNj.png)]( https:\/\/imgchr.com\/i\/rqRyNj)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4775332\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ebony0319 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u62b1\u6b49,\u5ba1\u9519\u9898\u4e86.\u6211\u662f\u6309\u7167\u6700\u9ad8\u5206,\u6700\u4f4e\u5206\u6765\u5199\u7684.\u5427\u91cc\u9762\u7684\u6761\u4ef6\u6539\u6210 s.time&gt;=s1.time \u5373\u53ef                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4775333\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : l00t <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ebony0319 #16 \u6761\u4ef6\u53ef\u4ee5\u6539\uff0c\u4f46\u4f60\u805a\u5408\u51fd\u6570\u6253\u7b97\u7528\u4ec0\u4e48\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4775334\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ebony0319 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @l00t \u4e0d\u53d8                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4775335\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : isir1234 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             select student_id, gap<br \/>from (select student_id,<br \/> last_value(score) over (partition by student_id order by created_at) &#8211;<br \/> first_value(score) over (partition by student_id order by created_at) AS gap,<br \/> row_number() over (partition by student_id order by created_at desc) rn<br \/> from exam) tmp<br \/>where rn = 1;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4775336\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : l00t <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ebony0319 #18 \u4e0d\u53d8\u4e0d\u5c31\u9519\u4e86\uff1f\u7b2c\u4e00\u6b21\u8003\u8bd5\u53c8\u4e0d\u662f\u6700\u9ad8\u5206\uff0c\u7b2c\u4e8c\u6b21\u53c8\u4e0d\u662f\u6700\u4f4e\u5206\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u6c42\u52a9\u4e00\u6761 SQL \u8bed\u53e5\u7684\u5199\u6cd5\uff0c\u5206\u7ec4&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/237993"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=237993"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/237993\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=237993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=237993"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=237993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}