{"id":201,"date":"2023-08-23T16:13:55","date_gmt":"2023-08-23T08:13:55","guid":{"rendered":"http:\/\/lingbo.online\/?p=201"},"modified":"2023-09-20T22:47:55","modified_gmt":"2023-09-20T14:47:55","slug":"sql_boy","status":"publish","type":"post","link":"https:\/\/lingbo.online\/index.php\/algorithm_learning\/sql_boy\/","title":{"rendered":"SQL\u9898\u76ee\u7ec3\u4e60"},"content":{"rendered":"<h3>\u67e5\u8be2\u6bcf\u4e2a\u7528\u6237\u6700\u65b0\u7684\u4e00\u6761\u8ba2\u5355<\/h3>\n<p>\u8868\u7ed3\u6784\uff1aid,user_id,order_status,create_time,update_time<br \/>\n\u89e3\u51b3\u601d\u8def\uff1a\u5b50\u67e5\u8be2\u52a0\u8868\u8fde\u63a5\u67e5\u8be2,\u9996\u5148\u5c06\u8868\u683c\u6309<code>user_id<\/code> group\uff0c\u4f7f\u7528<code>max()<\/code>\u51fd\u6570\u5339\u914d\u6bcf\u4e2aid\u4e0b\u7684<code>create_time<\/code>\uff0c\u63a5\u7740\u5229\u7528<code>user_id<\/code>\u548c<code>create_time<\/code>\u81ea\u8fde\u63a5\uff0c\u83b7\u5f97\u6700\u7ec8\u7684\u8ba2\u5355\u8bb0\u5f55<\/p>\n<pre><code class=\"language-SQL\">select b.* from(\n    select max(create_time) as create_time,user_id\n    from order_record\n    group by user_id) a\nleft join order_record b\non a.user_id=b.user_id\n    and a.create_time=b.create_time\n)<\/code><\/pre>\n<h3>\u65b0\u8001\u7528\u6237\u91d1\u989d\u67e5\u8be2<\/h3>\n<p>\u6709\u4e00\u5f20\u5168\u91cf\u7684\u4ea4\u6613\u8868\u548c\u4e00\u5f20\u65b0\u7528\u6237\u4ea4\u6613\u8868\uff0c\u4f7f\u7528sql\u7edf\u8ba1\u65b0\u7528\u6237\u548c\u8001\u7528\u6237\u6628\u65e5\u6210\u4ea4\u7684\u603b\u91d1\u989d\u3002<\/p>\n<pre><code class=\"language-SQL\">select\n    case when t2.use_id is not null then &#039;new&#039; else &#039;old&#039; end as user_type,\n    sum(t1.money) as total_amount\nfrom all_user as t1\nleft join new_user as t2\non t1.user_id=t2.user_id\nwhere Date(t1.datetime)=CURDATE()-INTERVAL 1 DAY\ngroup by user_type<\/code><\/pre>\n<h3>\u725b\u5ba2SQL11.\u6bcf\u65e5\u7684\u65e5\u6d3b\u6570\u53ca\u65b0\u7528\u6237\u7684\u5360\u6bd4<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u7528\u6237\u884c\u4e3a\u65e5\u5fd7\u8868tb_user_log<\/th>\n<th>id<\/th>\n<th>uid<\/th>\n<th>artical_id<\/th>\n<th>in_time<\/th>\n<th>out_time<\/th>\n<th>sign_cin<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>101<\/td>\n<td>9001<\/td>\n<td>2021-10-31 10:00:00<\/td>\n<td>2021-10-31 10:00:09<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>102<\/td>\n<td>9001<\/td>\n<td>2021-10-31 10:00:00<\/td>\n<td>2021-10-31 10:00:09<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>101<\/td>\n<td>0<\/td>\n<td>2021-11-01 10:00:00<\/td>\n<td>2021-11-01 10:00:42<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>102<\/td>\n<td>9001<\/td>\n<td>2021-11-01 10:00:00<\/td>\n<td>2021-11-01 10:00:09<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>108<\/td>\n<td>9001<\/td>\n<td>2021-11-01 10:00:01<\/td>\n<td>2021-11-01 10:00:50<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>108<\/td>\n<td>9001<\/td>\n<td>2021-11-02 10:00:01<\/td>\n<td>2021-11-02 10:00:50<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>104<\/td>\n<td>9001<\/td>\n<td>2021-11-02 10:00:28<\/td>\n<td>2021-11-02 10:00:50<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>106<\/td>\n<td>9001<\/td>\n<td>2021-11-02 10:00:28<\/td>\n<td>2021-11-02 10:00:50<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>9<\/td>\n<td>108<\/td>\n<td>9001<\/td>\n<td>2021-11-03 10:00:01<\/td>\n<td>2021-11-03 10:00:50<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>10<\/td>\n<td>109<\/td>\n<td>9002<\/td>\n<td>2021-11-03 11:00:55<\/td>\n<td>2021-11-03 11:00:59<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>11<\/td>\n<td>104<\/td>\n<td>9003<\/td>\n<td>2021-11-03 11:00:45<\/td>\n<td>2021-11-03 11:00:55<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>12<\/td>\n<td>105<\/td>\n<td>9003<\/td>\n<td>2021-11-03 11:00:53<\/td>\n<td>2021-11-03 11:00:59<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>13<\/td>\n<td>106<\/td>\n<td>9003<\/td>\n<td>2021-11-03 11:00:45<\/td>\n<td>2021-11-03 11:00:55<\/td>\n<td>0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\uff08uid-\u7528\u6237ID, artical_id-\u6587\u7ae0ID, in_time-\u8fdb\u5165\u65f6\u95f4, out_time-\u79bb\u5f00\u65f6\u95f4, sign_in-\u662f\u5426\u7b7e\u5230\uff09<\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong>\u7edf\u8ba1\u6bcf\u5929\u7684\u65e5\u6d3b\u6570\u53ca\u65b0\u7528\u6237\u5360\u6bd4<\/p>\n<ul>\n<li>\u65b0\u7528\u6237\u5360\u6bd4=\u5f53\u5929\u7684\u65b0\u7528\u6237\u6570\u00f7\u5f53\u5929\u6d3b\u8dc3\u7528\u6237\u6570\uff08\u65e5\u6d3b\u6570\uff09\u3002<\/li>\n<li>\u5982\u679cin_time\u8fdb\u5165\u65f6\u95f4\u548cout_time\u79bb\u5f00\u65f6\u95f4\u8de8\u5929\u4e86\uff0c\u5728\u4e24\u5929\u91cc\u90fd\u8bb0\u4e3a\u8be5\u7528\u6237\u6d3b\u8dc3\u8fc7\u3002<\/li>\n<li>\u65b0\u7528\u6237\u5360\u6bd4\u4fdd\u75592\u4f4d\u5c0f\u6570\uff0c\u7ed3\u679c\u6309\u65e5\u671f\u5347\u5e8f\u6392\u5e8f\u3002<\/li>\n<\/ul>\n<h4>\u5206\u6790<\/h4>\n<ul>\n<li>\u5efa\u7acb\u4e00\u5f20\u767b\u5f55\u8868\uff0c\u8bb0\u5f55\u6bcf\u5929\u767b\u5f55\u7684\u7528\u6237\uff0c\u5e76\u6309\u5929\u5bf9\u7528\u6237\u8fdb\u884c\u53bb\u91cd<\/li>\n<li>\u4f7f\u7528\u7a97\u53e3\u51fd\u6570\u5bf9\u6bcf\u4e2a\u7528\u6237\u7684\u767b\u5f55\u65e5\u671f\u8fdb\u884c\u6392\u5e8f\uff0c\u5e8f\u53f7\u4e3a1\u5219\u4e3a\u65b0\u7528\u6237<\/li>\n<\/ul>\n<h4>\u89e3\u7b54<\/h4>\n<pre><code class=\"language-SQL\">with t1 as(\n    select uid,date(in_time) dt\n    from tb_user_log\n    union \/\/union\u5b9e\u73b0\u53bb\u91cd\n    select uid,date(out_time) dt\n    from tb_user_log\n),\nwith t2 as(\n    uid,dt\n    row_number()over(partition by uid order by dt) rn \/\/\u5bf9\u7528\u6237\u767b\u5f55\u65e5\u671f\u8fdb\u884c\u6392\u5e8f\n    from t1\n)\nselect dt,\n    count(uid) dau,\n    round(sum(if(rn=1,1,0))\/count(uid),2) as uv_new_ration\nfrom t2\ngroup by dt\norder by dt<\/code><\/pre>\n<h3>\u6b21\u65e5\u7559\u5b58\u7387\u8ba1\u7b97<\/h3>\n<p>\u5b9e\u9645\u4e1a\u52a1\u4e2d\uff08\u7279\u522b\u662f\u7535\u5546\u4e1a\u52a1\u5206\u6790\uff09\uff0c\u9700\u8981\u8ba1\u7b97\u4eca\u5929\u7528\u6237\u767b\u5f55\uff0c\u7b2c\u4e8c\u5929\u8fd8\u4f1a\u767b\u5f55\u7684\u7528\u6237\u6240\u5360\u7684\u6bd4\u7387\uff0c\u4e5f\u5c31\u662f\u6b21\u65e5\u7559\u5b58\u7387\u7684\u6982\u5ff5\u3002<br \/>\n\u8ba1\u7b97\u601d\u8def\u5c31\u662f\u5c06\u8868\u8fdb\u884c\u81ea\u8fde\u63a5\uff08\u6761\u4ef6\u662f\u5de6\u8868\u65e5\u671f+1=\u53f3\u8868\u65e5\u671f\uff09\uff0c\u6ee1\u8db3\u6761\u4ef6\u7684\u8bb0\u5f55\u6570\u5c31\u662f\u6b21\u65e5\u767b\u5f55\u7684\u7528\u6237\u603b\u6570\uff0c\u518d\u9664\u4ee5\u5de6\u8868\u8bb0\u5f55\u603b\u6570\uff08\u7528\u6237\u6570\uff09\u5c31\u662f\u6b21\u65e5\u7559\u5b58\u7387\u3002<\/p>\n<pre><code class=\"language-SQL\">select count(date2)\/count(date1) as avg_ret\nfrom(\n    select\n        distinct a.uid,\n        a.date as date1,\n        b.date as date2\n    from tableName a\n    left join(\n    select distinct uid,date\n    from tableName\n    )b\n    on a.uid=b.uid\n    and date_add(a.date,interval 1 day)=b.date\n    )t<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u67e5\u8be2\u6bcf\u4e2a\u7528\u6237\u6700\u65b0\u7684\u4e00\u6761\u8ba2\u5355 \u8868\u7ed3\u6784\uff1aid,user_id,order_status,create_time,update_time &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[23,2],"tags":[25,30],"class_list":["post-201","post","type-post","status-publish","format-standard","hentry","category-leecode","category-algorithm_learning","tag-leecode","tag-sql"],"_links":{"self":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/comments?post=201"}],"version-history":[{"count":4,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/201\/revisions"}],"predecessor-version":[{"id":505,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/201\/revisions\/505"}],"wp:attachment":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/media?parent=201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/categories?post=201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/tags?post=201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}