{"id":16319,"date":"2025-01-04T11:12:28","date_gmt":"2025-01-04T03:12:28","guid":{"rendered":"https:\/\/www.orczhou.com\/?p=16319"},"modified":"2025-01-04T11:12:28","modified_gmt":"2025-01-04T03:12:28","slug":"sql-game-ticket-for-mysql","status":"publish","type":"post","link":"https:\/\/www.orczhou.com\/index.php\/2025\/01\/sql-game-ticket-for-mysql\/","title":{"rendered":"\u4f7f\u7528MySQL\u5b9e\u73b0\u8f66\u7968\u79d2\u6740\u7684\u8fdb\u9636\u6311\u6218"},"content":{"rendered":"\n<p>\u8fd9\u662f\u7b2c\u4e8c\u6b21 SQL \u7f16\u7a0b\u5927\u8d5b<sup>[<a href=\"https:\/\/www.ninedata.cloud\/sql_train2024\">1<\/a>]<\/sup>\uff0c\u6211\u4f9d\u65e7\u662f\u8bc4\u59d4\u4e4b\u4e00\uff0c\u6240\u4ee5\u81ea\u5df1\u4e5f\u5c1d\u8bd5\u4e86\u72ec\u7acb\u5b8c\u6210\u8be5\u95ee\u9898\u7684\u6311\u6218\u3002\u8fd9\u6b21\u5927\u8d5b\u5206\u4e3a\u201c\u666e\u901a\u6311\u6218\u201d\u548c\u201c\u8fdb\u9636\u6311\u6218\u201d\u3002\u5176\u4e2d\u666e\u901a\u6311\u6218\u8f83\u4e3a\u7b80\u5355\uff0c\u672c\u6587\u4e3b\u8981\u8ba8\u8bba\u81ea\u5df1\u5b8c\u6210\u8fdb\u9636\u6311\u6218\u8fc7\u7a0b\u4e2d\u7684\u60f3\u6cd5\u4e0e\u601d\u8def\u3002<\/p>\n\n\n\n\n\n\n<h3 class=\"wp-block-heading\">\u95ee\u9898\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u539f\u59cb\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u53c2\u8003\uff1a<a href=\"https:\/\/www.ninedata.cloud\/sql_train2024\">NineData \u7b2c\u4e8c\u5c4a\u6570\u636e\u5e93\u7f16\u7a0b\u5927\u8d5b \u7528\u4e00\u6761SQL\u79d2\u6740\u706b\u8f66\u7968<\/a>\uff0c\u672c\u6587\u4ec5\u8003\u8651\u5176\u4e2d\u7684\u201c\u8fdb\u9636\u6311\u6218\u201d\u3002\u8fd9\u91cc\u8be5\u201c\u8fdb\u9636\u6311\u6218\u95ee\u9898\u201d\u590d\u8ff0\u5982\u4e0b\u3002<\/p>\n\n\n\n<p>\u6709\u5982\u4e0b\u4e24\u5f20\u8868\u5b58\u653e\u7740\u4e58\u5ba2\u4fe1\u606f\u548c\u5217\u8f66\u4fe1\u606f\uff0c\u4f7f\u7528\u4e00\u6761SQL\u7ed9\u6bcf\u4e2a\u4e58\u5ba2\u5206\u914d\u4e00\u8d9f\u5217\u8f66\u4ee5\u53ca\u5bf9\u5e94\u7684\u5ea7\u4f4d\u53f7\uff0c\u9700\u8981\u6ce8\u610f\uff0c\u9700\u8981\u8003\u8651\u8fdb\u9636\u6311\u6218\u7684\u4e00\u4e9b\u8981\u6c42\uff0c\u6bd4\u5982\uff0c\u6bcf\u8d9f\u5217\u8f66\u53ef\u4ee5\u53d1\u552e10%\u7684\u65e0\u5ea7\u8f66\u7968\uff1b\u8f66\u7968\u9700\u8981\u6709\u9650\u53d1\u552e\u6709\u5ea7\u8f66\u7968\uff0c\u7136\u540e\u624d\u5f00\u59cb\u53d1\u552e\u65e0\u5ea7\u8f66\u7968\u3002<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code has-small-font-size\"><code lang=\"sql\" class=\"language-sql\">mysql&gt; desc passenger;\n+-------------------+-------------+------+-----+\n| Field             | Type        | Null | Key |\n+-------------------+-------------+------+-----+\n| passenger_id      | varchar(16) | NO   | PRI |\n| departure_station | varchar(32) | NO   |     |\n| arrival_station   | varchar(32) | NO   |     |\n+-------------------+-------------+------+-----+<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code has-small-font-size\"><code lang=\"sql\" class=\"language-sql\">mysql&gt; desc train;\n+-------------------+-------------+------+-----+\n| Field             | Type        | Null | Key |\n+-------------------+-------------+------+-----+\n| train_id          | varchar(8)  | NO   | PRI |\n| departure_station | varchar(32) | NO   |     |\n| arrival_station   | varchar(32) | NO   |     |\n| seat_count        | int         | NO   |     |\n+-------------------+-------------+------+-----+<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>\u793a\u4f8b\u6570\u636e\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code class=\"\">mysql&gt; select * from passenger limit 3;\n+--------------+-------------------+-----------------+\n| passenger_id | departure_station | arrival_station |\n+--------------+-------------------+-----------------+\n| P00000001    | \u4e0a\u6d77              | \u798f\u5dde            |\n| P00000002    | \u6210\u90fd              | \u6210\u90fd            |\n| P00000003    | \u4e4c\u9c81\u6728\u9f50          | \u592a\u539f            |\n+--------------+-------------------+-----------------+<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code class=\"\">mysql&gt; select * from train limit 3;\n+----------+-------------------+-----------------+------------+\n| train_id | departure_station | arrival_station | seat_count |\n+----------+-------------------+-----------------+------------+\n| G1006    | \u91cd\u5e86              | \u5317\u4eac            |       1600 |\n| G1007    | \u676d\u5dde              | \u798f\u5dde            |        600 |\n| G1008    | \u6d4e\u5357              | \u5408\u80a5            |        800 |\n+----------+-------------------+-----------------+------------+<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u89e3\u9898\u601d\u8def<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5bf9\u4e58\u5ba2\u8fdb\u884c\u7f16\u53f7<\/h4>\n\n\n\n<p>\u9996\u5148\u5229\u7528\u6570\u636e\u5e93\u7684<code>Windows Function<\/code>\u529f\u80fd\u5bf9\u6240\u6709\u7684\u4e58\u5ba2\u5148\u5206\u7ec4\u518d\u7f16\u53f7\uff0c\u5177\u4f53\u7684\uff0c\u6309\u7167\u201c\u51fa\u53d1\u7ad9\u201d\u548c\u201c\u5230\u8fbe\u7ad9\u201d\u5206\u7ec4\uff0c\u7136\u540e\u5728\u7ec4\u5185\u8fdb\u884c\u7f16\u53f7\u3002\u6b21\u7f16\u53f7\u5219\u4e3a\u540e\u7eed\u4e58\u5ba2\u8f66\u7968\u5206\u914d\u7684\u7f16\u53f7\u3002\u4f8b\u5982\uff0c\u4ece A \u5230 B \u5730\uff0c\u4e00\u5171\u6709 2420 \u4e2a\u4e58\u5ba2\u3002\u90a3\u4e48\u4e58\u5ba2\u7684\u7f16\u53f7\u5219\u662f1&#8230;2420\uff1b\u518d\u6709\u4e58\u5ba2\u4ece C \u5230 D \u5730\uff0c\u5171\u67091800\u4e2a\u4e58\u5ba2\uff0c\u5219\u7f16\u53f7\u5219\u4e3a 1 &#8230; 1800\u3002\u5927\u6982\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u5982\u4e0b\u7684 SQL \u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">ROW_NUMBER() over(PARTITION BY departure_station,arrival_station) as seq<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5bf9\u5217\u8f66\u8fdb\u884c\u6392\u5e8f\u548c\u8ba1\u7b97<\/h4>\n\n\n\n<p>\u4e0e\u4e58\u5ba2\u7c7b\u4f3c\u7684\uff0c\u5148\u6309\u7167\u51fa\u53d1\u548c\u5230\u8fbe\u7ad9\u70b9\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u5217\u8f66\u80fd\u591f\u5bb9\u7eb3\u7684\u4e58\u5ba2\u6570\u91cf\uff0c\u5373\u5ea7\u4f4d\u6570\u91cf\u7684 1.1 \u500d\u3002\u7136\u540e\uff0c\u5728\u5206\u7ec4\u5185\u8fdb\u884c\u201c\u7d2f\u52a0\u201d\u8ba1\u7b97\uff0c\u8be5\u7d2f\u52a0\u8ba1\u7b97\uff0c\u9700\u7b97\u51fa\u6bcf\u4e2a\u5217\u8f66\u80fd\u591f\u8fd0\u8f7d\u4e58\u5ba2\u7684\u8d77\u59cb\u5e8f\u53f7\u548c\u7ed3\u675f\u5e8f\u53f7\u3002\u4f8b\u5982\uff0c\u4ece A \u5230 B\u5730\uff0c\u5171\u6709\u5217\u8f66 G01 \u548c G07 \uff0c\u5e76\u5206\u522b\u6709 600 \u548c 1600 \u4e2a\u5ea7\u4f4d\u3002\u90a3\u4e48\uff0c\u7ecf\u8fc7\u4e0a\u8ff0\u7684\u7d2f\u52a0\u8ba1\u7b97\uff0c\u5217\u8f66 G01 \u80fd\u591f\u8fd0\u8f7d\u7684\u4e58\u5ba2\u7f16\u53f7\u5e94\u8be5\u662f 1 \u5230 660\uff0c\u800c G01 \u80fd\u591f\u8fd0\u8f7d\u7684\u4e58\u5ba2\u7f16\u53f7\u5219\u4e3a 661 \u5230 2420 \uff08\u5373\u4e3a 660 + 1600*110%\uff09\u3002<\/p>\n\n\n\n<p>\u4e0a\u8ff0\u8ba1\u7b97\u4e5f\u53ef\u4ee5\u4f7f\u7528 Window Function\u6765\u5b9e\u73b0\uff0c\u53c2\u8003\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">        sum(seat_count*1.1)\n          over (\n                 PARTITION BY departure_station,arrival_station\n                 ORDER BY train_id\n               ) as p_seat_to ,<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5408\u5e76\u8ba1\u7b97\u7ed3\u679c<\/h4>\n\n\n\n<p>\u7136\u540e\uff0c\u5c06\u4e0a\u8ff0\u7ecf\u8fc7\u8ba1\u7b97\u7684\u4e58\u5ba2\u8868\u548c\u5217\u8f66\u8868\u8fdb\u884c JOIN \uff0c\u6761\u4ef6\u662f \u8d77\u59cb\u7ad9\u548c\u5230\u8fbe\u7ad9\u76f8\u540c\uff0c\u4e14\u4e58\u5ba2\u7f16\u53f7\u5728\u5217\u8f66\u7f16\u53f7\u4e4b\u95f4\u3002\u5982\u679c\uff0c\u4e58\u5ba2\u65e0\u6cd5\u5173\u8054\u51fa\u5217\u8f66\uff0c\u5219\u8868\u793a\u65e0\u6cd5\u5206\u914d\u5217\u8f66\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8be5\u65b9\u6848\u7684\u6700\u7ec8 SQL<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">SELECT\n  p_01.p_id,\n  p_01.d_s,\n  p_01.a_s,\n  t_01.train_id as t_id,\n  p_01.seq, -- passager seq from d_s to a_s\n  t_01.seat_count,\n  @p_seat_from := (t_01.p_seat_to-t_01.seat_count*1.1 + 1) as seat_from, -- train seat from(start index)\n  t_01.p_seat_to as seat_to, -- train seat from(start index)\n\n  if(p_01.seq &gt;= p_seat_to-seat_count*0.1 + 1, \"ti_no_seat\",\"...\") as ti_no_seat,\n                \n  \n  @seq_in_train := p_01.seq - @p_seat_from + 1  as seq_in_train, -- seq in the train\n\n  @carriage_id := ceil(@seq_in_train\/100) as t_carr_id, -- for carriage id \n\n  @row_id := ceil((@seq_in_train%100)\/5) as row_id, -- row_id\n\n  @seat_id := ceil((@seq_in_train%100)%5) seat_id  -- 0,1,2,3,4  A B C E F\n\n\nFROM\n     (\n       select\n           ROW_NUMBER() over(PARTITION BY departure_station,arrival_station) as seq ,\n           passenger_id as p_id,\n           departure_station as d_s,\n           arrival_station as a_s\n       from\n       passenger\n     ) as p_01\n\n     LEFT JOIN\n\n    (\n      select\n        seat_count,\n        sum(seat_count*1.1)\n          over (\n                 PARTITION BY departure_station,arrival_station\n                 ORDER BY train_id\n               ) as p_seat_to ,\n        train_id,\n        departure_station as d_s ,\n        arrival_station as a_s\n      from\n      train\n    ) t_01\n\n    ON\n            p_01.seq &gt;= p_seat_to-seat_count*1.1 + 1\n        and p_01.seq &lt;= p_seat_to\n        and p_01.d_s =  t_01.d_s\n        and p_01.a_s =  t_01.a_s<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e0a\u8ff0\u5b9e\u73b0\u7684\u95ee\u9898<\/h3>\n\n\n\n<p>\u8fd9\u6837\u7684\u5b9e\u73b0\uff0c\u662f\u53ef\u4ee5\u5b8c\u6210\u76f8\u5173\u7684\u5ea7\u4f4d\u5206\u914d\u3002\u4f46\u662f\uff0c\u5374\u4f1a\u51fa\u73b0\u4e00\u4e2a\u4e0d\u5408\u7406\u7684\u60c5\u51b5\uff0c\u5373\u53ef\u80fd\u6709\u8f66\u6b21\u7684\u5ea7\u4f4d\u6ca1\u6709\u5206\u914d\u5b8c\uff0c\u4f46\u662f\u6709\u4e00\u90e8\u5206\u4e58\u5ba2\u5374\u88ab\u5206\u914d\u5230\u4e86\u65e0\u5ea7\u7684\u8f66\u6b21\u3002\u6bd4\u5982\uff0c\u4eceA\u5230B\u7684\u8f66\u6b21\uff0c\u6709\u4e24\u73ed\uff0c\u7b2c\u4e00\u73ed\u8f66600\u4e2a\u5ea7\u4f4d\uff0c\u7b2c\u4e8c\u72481600\u4e2a\u5ea7\u4f4d\uff0c\u4e00\u5171\u6709 800 \u4e2a\u4e58\u5ba2\u7684\u8bdd\uff0c\u90a3\u4e48\u8fd9\u91cc\u5206\u914d\u81ea\u7531\u5ea6\u5c31\u6bd4\u8f83\u9ad8\uff0c\u6bd4\u5982\u8fd9\u79cd\u60c5\u51b5\u4f9d\u65e7\u5206\u914d\u4e86 220 \u4e2a\u65e0\u5ea7\u7684\u5ea7\u4f4d\uff0c\u662f\u5426\u662f\u6ee1\u8db3\u8981\u6c42\u7684\u3002<\/p>\n\n\n\n<p>\u5728\u6700\u521d\uff0c\u8be5\u8d5b\u9898\u8fd8\u672a\u5bf9\u5916\u53d1\u5e03\u65f6\uff0c\u662f\u6ca1\u6709\u8be5\u9650\u5236\u7684\u3002\u800c\u540e\uff0c\u53d1\u73b0\u8be5\u6f0f\u6d1e\u540e\uff0c\u65b0\u589e\u4e86\u4e00\u4e2a\u89c4\u5219\uff0c\u5373\u9700\u8981\u5148\u628a\u6709\u5ea7\u7684\u7968\u4f18\u5148\u5206\u914d\uff0c\u518d\u5206\u914d\u65e0\u5ea7\u7684\u8f66\u7968\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8003\u8651\u4f18\u5148\u5206\u914d\u6709\u5ea7<\/h3>\n\n\n\n<p>\u8003\u8651\u4f18\u5148\u5206\u914d\u6709\u5ea7\u7684\u8f66\u7968\uff0c\u518d\u5bf9\u4e0a\u8ff0\u5b9e\u73b0\u8fdb\u884c\u4e00\u5b9a\u7a0b\u5ea6\u7684\u4fee\u6539\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u91cd\u65b0\u8003\u8651\u5bf9\u5217\u8f66\u7684\u7f16\u53f7\u548c\u8ba1\u7b97<\/h4>\n\n\n\n<p>\u5bf9\u4e8e\u6bcf\u4e00\u8d9f\u5217\u8f66<code>X<\/code>\uff0c\u6784\u9020\u4e00\u4e2a\u865a\u62df\u5217\u8f66<code>X'<\/code>\uff0c\u8be5\u865a\u62df\u5217\u8f66<code>X'<\/code>\u865a\u62df\u7684\u8d1f\u8d23\u6240\u6709\u7684<code>X<\/code>\u5217\u8f66\u7684\u5ea7\u7968\u3002\u800c\u5728\u7ed9\u5217\u8f66\u4e2d\u8ba1\u7b97\u8d77\u59cb\u548c\u7ed3\u675f\u4e58\u5ba2\u7f16\u53f7\u65f6\uff0c\u5219\u4f18\u5148\u8ba1\u7b97\u539f\u5217\u8f66\u7684\u7f16\u53f7\u8303\u56f4\uff0c\u5728\u6240\u6709\u7684\u539f\u5217\u8f66\u7f16\u53f7\u8ba1\u7b97\u5b8c\u6210\u540e\uff0c\u518d\u8ba1\u7b97<code>X'<\/code>\u7684\u4e58\u5ba2\u7f16\u53f7\u8303\u56f4\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u4f7f\u7528 CTEs \u5b9e\u73b0\u8be5\u8868\u8fbe\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">WITH \n  t_no_seat_virtual AS (\n        select train_id as t_id,departure_station as d_s,arrival_station as a_s,seat_count, seat_count*0.1 as seat_count_no_seat \n        from train ),\n  t_include_no_seat AS (\n        select t_id,d_s ,a_s ,seat_count, \"with_seat\" as if_seat\n        from t_no_seat_virtual\n        union \n        select t_id,d_s ,a_s ,seat_count_no_seat, \"no_seat\" as if_seat\n        from t_no_seat_virtual)\nSELECT * from t_include_no_seat ORDER BY t_id<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5305\u542b\u865a\u62df\u5217\u8f66\u7684\u8868 t_include_no_seat <\/h4>\n\n\n\n<p>\u7136\u540e\u628a\u4e0a\u9762\u7684\u5b9e\u73b0\u4e2d\uff0c\u8868train\u66ff\u6362\u6210\u8fd9\u91cc\u7684 t_include_no_seat \uff0c\u5305\u542b\u4e86\u989d\u5916\u7684\u201c\u865a\u62df\u5217\u8f66\u201d\uff0c\u5b8c\u6574\u7684 SQL \u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">WITH\n  t_no_seat_virtual AS (\n        select train_id as t_id,departure_station as d_s,arrival_station as a_s,seat_count, seat_count*0.1 as seat_count_no_seat\n        from train ),\n  t_include_no_seat AS (\n        select t_id,d_s ,a_s ,seat_count, 0 as if_no_seat\n        from t_no_seat_virtual\n        union\n        select t_id,d_s ,a_s ,seat_count_no_seat, 1 as if_no_seat\n        from t_no_seat_virtual)\nselect\n  seat_count,\n  sum(seat_count)\n    over (\n           PARTITION BY d_s,a_s\n           ORDER BY t_id,if_no_seat\n         ) as p_seat_to ,\n  t_id,\n  d_s ,\n  a_s,\n  if_no_seat\nfrom\nt_include_no_seat<\/code><\/pre>\n\n\n\n<p>\u8fd4\u56de\u7684\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"apacheconf\" class=\"language-apacheconf\">+------------+-----------+------+--------+--------+------------+\n| seat_count | p_seat_to | t_id | d_s    | a_s    | if_no_seat |\n+------------+-----------+------+--------+--------+------------+\n|      600.0 |     600.0 | G109 | \u4e0a\u6d77   | \u5317\u4eac   |          0 |\n|       60.0 |     660.0 | G109 | \u4e0a\u6d77   | \u5317\u4eac   |          1 |\n|     1600.0 |    2260.0 | G40  | \u4e0a\u6d77   | \u5317\u4eac   |          0 |\n|      160.0 |    2420.0 | G40  | \u4e0a\u6d77   | \u5317\u4eac   |          1 |\n|     1600.0 |    4020.0 | G70  | \u4e0a\u6d77   | \u5317\u4eac   |          0 |\n|      160.0 |    4180.0 | G70  | \u4e0a\u6d77   | \u5317\u4eac   |          1 |\n|     1600.0 |    1600.0 | G113 | \u4e0a\u6d77   | \u5e7f\u5dde   |          0 |\n|      160.0 |    1760.0 | G113 | \u4e0a\u6d77   | \u5e7f\u5dde   |          1 |\n|     1600.0 |    3360.0 | G26  | \u4e0a\u6d77   | \u5e7f\u5dde   |          0 |\n|      160.0 |    3520.0 | G26  | \u4e0a\u6d77   | \u5e7f\u5dde   |          1 |\n|     1600.0 |    5120.0 | G48  | \u4e0a\u6d77   | \u5e7f\u5dde   |          0 |\n|      160.0 |    5280.0 | G48  | \u4e0a\u6d77   | \u5e7f\u5dde   |          1 |\n|     1600.0 |    1600.0 | G52  | \u4e0a\u6d77   | \u6210\u90fd   |          0 |\n|      160.0 |    1760.0 | G52  | \u4e0a\u6d77   | \u6210\u90fd   |          1 |\n|     1600.0 |    3360.0 | G8   | \u4e0a\u6d77   | \u6210\u90fd   |          0 |\n|      160.0 |    3520.0 | G8   | \u4e0a\u6d77   | \u6210\u90fd   |          1 |\n|     1600.0 |    1600.0 | G107 | \u4e0a\u6d77   | \u6b66\u6c49   |          0 |\n|      160.0 |    1760.0 | G107 | \u4e0a\u6d77   | \u6b66\u6c49   |          1 |\n|     1600.0 |    3360.0 | G17  | \u4e0a\u6d77   | \u6b66\u6c49   |          0 |\n|      160.0 |    3520.0 | G17  | \u4e0a\u6d77   | \u6b66\u6c49   |          1 |<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7f16\u53f7\u7684 ORDER BY \u9700\u8981\u6309\u7167 if_no_seat,t_id \u8fdb\u884c\u6392\u5e8f\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u4fdd\u969c\uff0c\u4f18\u5148\u5206\u914d\u6709\u5ea7\u4f4d\u7684\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">WITH\n  t_no_seat_virtual AS (\n        select train_id as t_id,departure_station as d_s,arrival_station as a_s,seat_count, seat_count*0.1 as seat_count_no_seat\n        from train ),\n  t_include_no_seat AS (\n        select t_id,d_s ,a_s ,seat_count, 0 as if_no_seat\n        from t_no_seat_virtual\n        union\n        select t_id,d_s ,a_s ,seat_count_no_seat, 1 as if_no_seat\n        from t_no_seat_virtual)\nselect\n  sum(seat_count)\n    over (\n           PARTITION BY d_s,a_s\n           ORDER BY     if_no_seat,t_id\n         ) as p_seat_to ,\n  seat_count,\n  t_id,\n  d_s ,\n  a_s,\n  if_no_seat\nfrom\nt_include_no_seat<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u6309\u7167\u4e58\u5ba2\u5e8f\u53f7\u5206\u914d\u5ea7\u4f4d<\/h4>\n\n\n\n<p>\u4e0e\u524d\u8ff0\u7684\u5b9e\u73b0\u76f8\u540c\uff0c\u9996\u5148\u6309\u7167\u59cb\u53d1\u548c\u5230\u8fbe\u7ad9\u70b9\u5c06\u65c5\u5ba2\u8868\u4e0e\u201c\u865a\u62df\u5217\u8f66\u201d\u8868\u5173\u8054\u3002\u5982\u679c\u81ea\u5df1\u5e8f\u5217\u843d\u5728\u67d0\u4e2a\u5217\u8f66\u7684\u533a\u95f4\u4e2d\u5c31\u8868\u793a\u6709\u5ea7\u4f4d\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5173\u4e8e row \u7684\u5206\u914d\u5f02\u5e38\u95ee\u9898<\/h4>\n\n\n\n<p>\u6309\u7167\u4e0a\u8ff0\u7684\u5206\u914d\uff0c\u4f1a\u5c06\u7f16\u53f7\u4e3a 100 \u80cc\u4e66\u7684\u4eba\uff0c\u5206\u914d\u4e3a 0F\uff0c\u800c\u6b63\u786e\u7684\u5e94\u8be5\u662f 20 F\u3002\u6240\u4ee5\uff0c\u9700\u8981\u989d\u5916\u5904\u7406\u8be5\u6570\u503c\u3002\u5177\u4f53\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">ceil((( p_01.seq-t_01.p_seat_to + t_01.seat_count )%100)\/5) <\/code><\/pre>\n\n\n\n<p>\u4fee\u6539\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">IF( (p_01.seq-t_01.p_seat_to + t_01.seat_count)%100 = 0, \"20\" ,ceil((( p_01.seq-t_01.p_seat_to + t_01.seat_count )%100)\/5))  <\/code><\/pre>\n\n\n\n<p>\u8fd9\u90e8\u5206\u4ee3\u7801\u5b9e\u73b0\u8f83\u4e3a\u5197\u957f\uff0c\u66f4\u597d\u7684\u65b9\u6cd5\u662f\u5148\u8ba1\u7b97\u504f\u501a\u503c\uff0c\u7136\u540e\u4f7f\u7528\u5b57\u7b26\u4e32\u622a\u53d6\u51fd\u6570\u622a\u53d6\uff0c\u800c\u65e0\u9700\u5199\u8fd9\u4e48\u591a\u7684<code>CASE ... WHEN<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b8c\u6574\u7684SQL<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">WITH \n  t_no_seat_virtual AS (\n        select train_id as t_id,departure_station as d_s,arrival_station as a_s,seat_count, seat_count*0.1 as seat_count_no_seat \n        from train ),\n  t_include_no_seat AS (\n        select t_id,d_s ,a_s ,seat_count, 0 as if_no_seat\n        from t_no_seat_virtual\n        union \n        select t_id,d_s ,a_s ,seat_count_no_seat, 1 as if_no_seat\n        from t_no_seat_virtual)\n\nSELECT\n  p_01.p_id,\n  p_01.d_s,\n  p_01.a_s,\n  t_01.t_id as t_id,\n  p_01.seq, -- passager seq from d_s to a_s\n  t_01.seat_count,\n  t_01.if_no_seat,\n  @p_seat_from := (t_01.p_seat_to-t_01.seat_count + 1) as seat_from, -- train seat from(start index)\n  t_01.p_seat_to as seat_to, -- train seat from(start index)\n  \n  @seq_in_train := p_01.seq - @p_seat_from + 1  as seq_in_train, -- seq in the train\n\n  @carriage_id := ceil(@seq_in_train\/100) as t_carr_id, -- for carriage id \n\n  @row_id := ceil((@seq_in_train%100)\/5) as row_id, -- row_id\n\n  @seat_id := ceil((@seq_in_train%100)%5) seat_id,  -- 0,1,2,3,4  A B C E F\n\n  CASE\n    WHEN @seat_id = 1 THEN CONCAT(@row_id,\"A\")\n    WHEN @seat_id = 2 THEN CONCAT(@row_id,\"B\")\n    WHEN @seat_id = 3 THEN CONCAT(@row_id,\"C\")\n    WHEN @seat_id = 4 THEN CONCAT(@row_id,\"E\")\n    WHEN @seat_id = 0 THEN CONCAT(@row_id,\"F\")\n    ELSE \"ERROR\"\n  END as seat_index\n\nFROM\n     (\n       select\n           ROW_NUMBER() over(PARTITION BY departure_station,arrival_station) as seq ,\n           passenger_id as p_id,\n           departure_station as d_s,\n           arrival_station as a_s\n       from\n       passenger\n     ) as p_01\n\n     LEFT JOIN\n\n    (\n      select\n        seat_count,\n        sum(seat_count)\n          over (\n                 PARTITION BY d_s,a_s\n                 ORDER BY     if_no_seat,t_id\n               ) as p_seat_to ,\n        t_id,\n        d_s ,\n        a_s ,\n        if_no_seat\n      from\n      t_include_no_seat\n    ) t_01\n\n    ON\n            p_01.seq &gt;= p_seat_to-seat_count + 1\n        and p_01.seq &lt;= p_seat_to\n        and p_01.d_s =  t_01.d_s\n        and p_01.a_s =  t_01.a_s<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fee\u6b63SQL<\/h4>\n\n\n\n<p>\u6700\u540e\u6309\u7167\u9898\u76ee\u8981\u6c42\uff0c\u5bf9\u8f93\u51fa\u7ed3\u679c\u505a\u4e00\u4e9b\u4fee\u6b63\u3002\u5177\u4f53\u7684\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6309\u8981\u6c42\uff0c\u5982\u679c\u5206\u914d\u5ea7\u4f4d\u4e3a\u65e0\u5ea7\u7684\uff0c\u5219\u5728\u8f66\u53a2\u53f7\u5c55\u793a\u4e3a&#8221;&#8221;\uff0c\u5ea7\u4f4d\u53f7\u663e\u793a&#8221;\u65e0\u5ea7&#8221;\u3002<\/li>\n\n\n\n<li>\u5220\u9664\u4e2d\u95f4\u8ba1\u7b97\u7ed3\u679c\uff0c\u4e3a\u4e86\u4fdd\u8bc1\u6027\u80fd\uff0c\u5c31\u4e0d\u5728\u5916\u9762\u518d\u5957\u4e00\u5c42\u4e86\uff0c\u4e8b\u5b9e\u4e0a\uff0c\u5957\u4e00\u5c42\u53ef\u8bfb\u6027\u4f1a\u66f4\u597d<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">WITH \n  t_no_seat_virtual AS (\n        select train_id as t_id,\n               departure_station as d_s,\n               arrival_station as a_s,\n               seat_count, seat_count*0.1 as seat_count_no_seat \n        from train ),\n  t_include_no_seat AS (\n        select t_id,d_s ,a_s ,seat_count, 0 as if_no_seat\n        from t_no_seat_virtual\n        union \n        select t_id,d_s ,a_s ,seat_count_no_seat, 1 as if_no_seat\n        from t_no_seat_virtual)\n\nSELECT\n  p_01.p_id,\n  p_01.d_s,\n  p_01.a_s,\n  t_01.t_id as t_id,\n  p_01.seq, -- passager seq from d_s to a_s\n  t_01.seat_count,\n  t_01.if_no_seat,\n  @p_seat_from := (t_01.p_seat_to-t_01.seat_count + 1) as seat_from, -- train seat from(start index)\n  t_01.p_seat_to as seat_to, -- train seat from(start index)\n  \n  @seq_in_train := p_01.seq - @p_seat_from + 1  as seq_in_train, -- seq in the train\n\n  -- @carriage_id := ceil(@seq_in_train\/100) as t_carr_id, -- for carriage id \n\n  IF(if_no_seat, \"\" , @carriage_id := ceil(@seq_in_train\/100) ) as t_carr_id,\n\n  @row_id := ceil((@seq_in_train%100)\/5) as row_id, -- row_id\n\n  @seat_id := IF( !isnull(t_01.t_id) and if_no_seat,-1,ceil((@seq_in_train%100)%5)) as seat_id,  -- 0,1,2,3,4  A B C E F\n\n  CASE\n    WHEN @seat_id = 1  THEN CONCAT(@row_id,\"A\")\n    WHEN @seat_id = 2  THEN CONCAT(@row_id,\"B\")\n    WHEN @seat_id = 3  THEN CONCAT(@row_id,\"C\")\n    WHEN @seat_id = 4  THEN CONCAT(@row_id,\"E\")\n    WHEN @seat_id = 0  THEN CONCAT(@row_id,\"F\")\n    WHEN @seat_id = -1 THEN \"\u65e0\u5ea7\"\n    ELSE NULL\n  END as seat_index\n\nFROM\n     (\n       select\n           ROW_NUMBER() over(PARTITION BY departure_station,arrival_station) as seq ,\n           passenger_id as p_id,\n           departure_station as d_s,\n           arrival_station as a_s\n       from\n       passenger\n     ) as p_01\n\n     LEFT JOIN\n\n    (\n      select\n        seat_count,\n        sum(seat_count)\n          over (\n                 PARTITION BY d_s,a_s\n                 ORDER BY     if_no_seat,t_id\n               ) as p_seat_to ,\n        t_id,\n        d_s ,\n        a_s ,\n        if_no_seat\n      from\n      t_include_no_seat\n    ) t_01\n\n    ON\n            p_01.seq &gt;= p_seat_to-seat_count + 1\n        and p_01.seq &lt;= p_seat_to\n        and p_01.d_s =  t_01.d_s\n        and p_01.a_s =  t_01.a_s<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6b63\u786e\u6027\u9a8c\u8bc1<\/h3>\n\n\n\n<p>\u8fd9\u4e2a SQL \u7684\u6b63\u786e\u6027\u5e76\u4e0d\u597d\u9a8c\u8bc1\u3002\u800c\u4e8b\u5b9e\u4e0a\uff0c\u53ea\u8981\u80fd\u591f\u6b63\u786e\u7684\u5b8c\u6210\u8fd9\u6761SQL\uff0c\u57fa\u672c\u4e0a\u5c31\u5df2\u7ecf\u6253\u8d25\u4e8680%\u7684\u9009\u624b\u4e86\u3002\u5982\u679c\u6027\u80fd\u518d\u6709\u4e00\u4e9b\u4f18\u5316\uff0c\u57fa\u672c\u4e0a\u5df2\u7ecf\u662f\u524d10%\u7684\u9009\u624b\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u4ece\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u5bf9SQL\u6b63\u786e\u6027\u505a\u521d\u6b65\u9a8c\u8bc1\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u7cfb\u5217\uff08\u59cb\u53d1\u7ad9\u548c\u5230\u8fbe\u7ad9\u76f8\u540c\uff09\u7684\u8f66\u6b21\u8fdb\u884c\u7edf\u8ba1\uff0c\u7edf\u8ba1\u5ea7\u4f4d\u6570\u91cf\u548c\u65c5\u5ba2\u6570\u91cf\uff0c\u7136\u540e\u770b\u5b9e\u9645\u5206\u914d\u60c5\u51b5\u662f\u5426\u7b26\u5408<\/li>\n\n\n\n<li>\u624b\u52a8\u68c0\u8f66\u8f66\u53a2\u3001\u5ea7\u4f4d\u53f7\u5206\u914d\u7684\u60c5\u51b5<\/li>\n\n\n\n<li>\u68c0\u67e5\u67d0\u4e2a\u7cfb\u5217\uff0c\u65e0\u5ea7\u548c\u6709\u5ea7\u7684\u4e58\u5ba2\u6570\u91cf<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u5ea7\u4f4d\u4f9b\u9700\u7edf\u8ba1<\/h4>\n\n\n\n<p>\u5ea7\u4f4d\u7684\u4f9b\u9700\u60c5\u51b5\u6709\u5982\u4e0b\u4e09\u79cd\uff1a(a) \u5ea7\u7968\u4f9b\u5e94\u5145\u503c (b) \u52a0\u4e0a\u65e0\u5ea7\u540e\u4f9b\u5e94\u5145\u503c (c) \u4f9b\u5e94\u4e0d\u8db3\u3002\u5b8c\u6574\u7684\u4f9b\u9700\u8ba1\u7b97\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">\ncase #1\nti_supply |__________________________|___________|\nti_needed |&lt;-------------------&gt;|\n\ncase #2\nti_supply |__________________________|___________|\nti_needed |&lt;------------------------------&gt;|\n\ncase #3\nti_supply |__________________________|___________|\nti_needed |&lt;--------------------------------------------&gt;|\n\n-- ti_ always short for tickets\n-- p_  always short for passager\n-- t_  always short for train\nselect\n    p_needed.d_s,\n    p_needed.a_s,\n    ti_needed,\n    ti_supply,\n    if( ti_needed &gt; ti_supply, ti_needed - ti_supply , 0 )  as p_without_ti,\n    if( ti_needed &gt; ti_supply\/1.1 ,  round(ti_supply\/1.1,0) , ti_needed ) as p_with_ti_with_seat,\n    if( ti_needed &lt;= ti_supply\/1.1 ,   -- case #1\n        0,\n        if( ti_needed &lt;= ti_supply,    -- case #2\n            round(ti_needed - ti_supply\/1.1,0) ,\n            ti_supply\/11               -- case #3\n          )\n      ) as p_with_ti_without_seat\nfrom\n  (\n    select\n      -- passenger_id as p_id,\n      departure_station as d_s,\n      arrival_station as a_s,\n      count(1) as ti_needed\n    from\n      passenger\n    group by\n      departure_station,arrival_station\n  ) p_needed\n  ,\n  (\n    select\n      -- train_id t_id,\n      departure_station as d_s ,\n      arrival_station as a_s,\n      1.1*sum(seat_count) as ti_supply\n    from\n      train\n    group by\n      departure_station,arrival_station\n  ) t_supply\nWHERE\n        p_needed.d_s = t_supply.d_s\n    and p_needed.a_s = t_supply.a_s<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u8fd4\u56de\u7684\u4f9b\u9700\u8868\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">+--------+--------+-----------+-----------+--------------+---------------------+------------------------+\n| d_s    | a_s    | ti_needed | ti_supply | p_without_ti | p_with_ti_with_seat | p_with_ti_without_seat |\n+--------+--------+-----------+-----------+--------------+---------------------+------------------------+\n| \u4e0a\u6d77   | \u5317\u4eac   |      2460 |    4180.0 |            0 |                2460 |                      0 |\n| \u4e0a\u6d77   | \u5e7f\u5dde   |      2406 |    5280.0 |            0 |                2406 |                      0 |\n| \u4e0a\u6d77   | \u6210\u90fd   |      2421 |    3520.0 |            0 |                2421 |                      0 |\n| \u4e0a\u6d77   | \u6b66\u6c49   |      2454 |    7700.0 |            0 |                2454 |                      0 |\n| \u4e0a\u6d77   | \u6df1\u5733   |      2388 |    7040.0 |            0 |                2388 |                      0 |\n| \u5317\u4eac   | \u4e0a\u6d77   |      2381 |    1760.0 |        621.0 |                1600 |              160.00000 |\n| \u5317\u4eac   | \u5e7f\u5dde   |      2448 |    3520.0 |            0 |                2448 |                      0 |\n| \u5317\u4eac   | \u6210\u90fd   |      2384 |    3520.0 |            0 |                2384 |                      0 |\n| \u5317\u4eac   | \u676d\u5dde   |      2478 |    3520.0 |            0 |                2478 |                      0 |\n| \u5317\u4eac   | \u6b66\u6c49   |      2404 |    5940.0 |            0 |                2404 |                      0 |\n| \u5317\u4eac   | \u6df1\u5733   |      2342 |    3520.0 |            0 |                2342 |                      0 |\n| \u5e7f\u5dde   | \u4e0a\u6d77   |      2339 |    4180.0 |            0 |                2339 |                      0 |\n| \u5e7f\u5dde   | \u5317\u4eac   |      2368 |    1760.0 |        608.0 |                1600 |              160.00000 |\n| \u5e7f\u5dde   | \u6210\u90fd   |      2332 |    3520.0 |            0 |                2332 |                      0 |\n| \u5e7f\u5dde   | \u676d\u5dde   |      2407 |    5280.0 |            0 |                2407 |                      0 |\n| \u5e7f\u5dde   | \u6b66\u6c49   |      2320 |    3520.0 |            0 |                2320 |                      0 |\n| \u5e7f\u5dde   | \u6df1\u5733   |      2352 |    1760.0 |        592.0 |                1600 |              160.00000 |\n| \u6210\u90fd   | \u4e0a\u6d77   |      2422 |    4180.0 |            0 |                2422 |                      0 |\n| \u6210\u90fd   | \u5317\u4eac   |      2318 |    5940.0 |            0 |                2318 |                      0 |\n| \u6210\u90fd   | \u5e7f\u5dde   |      2450 |    1760.0 |        690.0 |                1600 |              160.00000 |\n| \u6210\u90fd   | \u676d\u5dde   |      2343 |    5280.0 |            0 |                2343 |                      0 |\n| \u6210\u90fd   | \u6b66\u6c49   |      2415 |    5280.0 |            0 |                2415 |                      0 |\n| \u6210\u90fd   | \u6df1\u5733   |      2364 |    2420.0 |            0 |                2200 |                    164 |\n| \u676d\u5dde   | \u5317\u4eac   |      2389 |    1760.0 |        629.0 |                1600 |              160.00000 |\n| \u676d\u5dde   | \u6210\u90fd   |      2370 |    1760.0 |        610.0 |                1600 |              160.00000 |\n| \u676d\u5dde   | \u6df1\u5733   |      2387 |   10560.0 |            0 |                2387 |                      0 |\n| \u6b66\u6c49   | \u4e0a\u6d77   |      2323 |    5280.0 |            0 |                2323 |                      0 |\n| \u6b66\u6c49   | \u5317\u4eac   |      2453 |    5280.0 |            0 |                2453 |                      0 |\n| \u6b66\u6c49   | \u5e7f\u5dde   |      2395 |   10560.0 |            0 |                2395 |                      0 |\n| \u6b66\u6c49   | \u6210\u90fd   |      2337 |    1760.0 |        577.0 |                1600 |              160.00000 |\n| \u6b66\u6c49   | \u676d\u5dde   |      2428 |    3520.0 |            0 |                2428 |                      0 |\n| \u6b66\u6c49   | \u6df1\u5733   |      2390 |    5280.0 |            0 |                2390 |                      0 |\n| \u6df1\u5733   | \u4e0a\u6d77   |      2251 |    3520.0 |            0 |                2251 |                      0 |\n| \u6df1\u5733   | \u5317\u4eac   |      2309 |    7040.0 |            0 |                2309 |                      0 |\n| \u6df1\u5733   | \u6210\u90fd   |      2341 |    3520.0 |            0 |                2341 |                      0 |\n| \u6df1\u5733   | \u676d\u5dde   |      2412 |     660.0 |       1752.0 |                 600 |               60.00000 |\n| \u6df1\u5733   | \u6b66\u6c49   |      2329 |   10120.0 |            0 |                2329 |                      0 |\n+--------+--------+-----------+-----------+--------------+---------------------+------------------------+<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">SQL \u8ba1\u7b97\u8fd4\u56de\u7ed3\u679c\u7edf\u8ba1<\/h4>\n\n\n\n<p>\u5148\u4f7f\u7528 <code>CREATE TABLE t_ret ...<\/code>\u5c06\u7ed3\u679c\u96c6\u5b58\u50a8\u4e00\u4e2a\u4e2d\u95f4\u7684\u4e34\u65f6\u8868\u3002<\/p>\n\n\n\n<p>\u7136\u540e\uff0c\u518d\u8ba1\u7b97 SQL\u8fd4\u56de\u7ed3\u679c\u8868\u4e2d\u7684\u7edf\u8ba1\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">SELECT \n  d_s,a_s,\n  CASE\n    WHEN ISNULL(seat_index)  THEN \"p_without_ti\"\n    WHEN seat_index = \"\u65e0\u5ea7\"  THEN \"p_with_ti_without_seat\"\n    ELSE \"p_with_ti_with_seat\"\n  END as p_status,\n  COUNT(1)\nFROM t_ret\nGROUP BY d_s,a_s , p_status<\/code><\/pre>\n\n\n\n<p>\u8fd4\u56de\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">+--------+--------+------------------------+----------+\n| d_s    | a_s    | p_status               | COUNT(1) |\n+--------+--------+------------------------+----------+\n| \u4e0a\u6d77   | \u5317\u4eac   | p_with_ti_with_seat    |     2460 |\n| \u4e0a\u6d77   | \u5e7f\u5dde   | p_with_ti_with_seat    |     2406 |\n| \u4e0a\u6d77   | \u6210\u90fd   | p_with_ti_with_seat    |     2421 |\n| \u4e0a\u6d77   | \u676d\u5dde   | p_without_ti           |     2373 |\n| \u4e0a\u6d77   | \u6b66\u6c49   | p_with_ti_with_seat    |     2454 |\n| \u4e0a\u6d77   | \u6df1\u5733   | p_with_ti_with_seat    |     2388 |\n| \u5317\u4eac   | \u4e0a\u6d77   | p_with_ti_with_seat    |     1600 |\n| \u5317\u4eac   | \u4e0a\u6d77   | p_with_ti_without_seat |      160 |\n| \u5317\u4eac   | \u4e0a\u6d77   | p_without_ti           |      621 |\n| \u5317\u4eac   | \u5e7f\u5dde   | p_with_ti_with_seat    |     2448 |\n| \u5317\u4eac   | \u6210\u90fd   | p_with_ti_with_seat    |     2384 |\n| \u5317\u4eac   | \u676d\u5dde   | p_with_ti_with_seat    |     2478 |\n| \u5317\u4eac   | \u6b66\u6c49   | p_with_ti_with_seat    |     2404 |\n| \u5317\u4eac   | \u6df1\u5733   | p_with_ti_with_seat    |     2342 |\n| \u5e7f\u5dde   | \u4e0a\u6d77   | p_with_ti_with_seat    |     2339 |\n| \u5e7f\u5dde   | \u5317\u4eac   | p_with_ti_with_seat    |     1600 |\n| \u5e7f\u5dde   | \u5317\u4eac   | p_with_ti_without_seat |      160 |\n| \u5e7f\u5dde   | \u5317\u4eac   | p_without_ti           |      608 |\n| \u5e7f\u5dde   | \u6210\u90fd   | p_with_ti_with_seat    |     2332 |\n| \u5e7f\u5dde   | \u676d\u5dde   | p_with_ti_with_seat    |     2407 |\n| \u5e7f\u5dde   | \u6b66\u6c49   | p_with_ti_with_seat    |     2320 |\n| \u5e7f\u5dde   | \u6df1\u5733   | p_with_ti_with_seat    |     1600 |\n| \u5e7f\u5dde   | \u6df1\u5733   | p_with_ti_without_seat |      160 |\n| \u5e7f\u5dde   | \u6df1\u5733   | p_without_ti           |      592 |\n| \u6210\u90fd   | \u4e0a\u6d77   | p_with_ti_with_seat    |     2422 |\n| \u6210\u90fd   | \u5317\u4eac   | p_with_ti_with_seat    |     2318 |\n| \u6210\u90fd   | \u5e7f\u5dde   | p_with_ti_with_seat    |     1600 |\n| \u6210\u90fd   | \u5e7f\u5dde   | p_with_ti_without_seat |      160 |\n| \u6210\u90fd   | \u5e7f\u5dde   | p_without_ti           |      690 |\n| \u6210\u90fd   | \u676d\u5dde   | p_with_ti_with_seat    |     2343 |\n| \u6210\u90fd   | \u6b66\u6c49   | p_with_ti_with_seat    |     2415 |\n| \u6210\u90fd   | \u6df1\u5733   | p_with_ti_with_seat    |     2200 |\n| \u6210\u90fd   | \u6df1\u5733   | p_with_ti_without_seat |      164 |\n| \u676d\u5dde   | \u4e0a\u6d77   | p_without_ti           |     2376 |\n| \u676d\u5dde   | \u5317\u4eac   | p_with_ti_with_seat    |     1600 |\n| \u676d\u5dde   | \u5317\u4eac   | p_with_ti_without_seat |      160 |\n| \u676d\u5dde   | \u5317\u4eac   | p_without_ti           |      629 |\n| \u676d\u5dde   | \u5e7f\u5dde   | p_without_ti           |     2401 |\n| \u676d\u5dde   | \u6210\u90fd   | p_with_ti_with_seat    |     1600 |\n| \u676d\u5dde   | \u6210\u90fd   | p_with_ti_without_seat |      160 |\n| \u676d\u5dde   | \u6210\u90fd   | p_without_ti           |      610 |\n| \u676d\u5dde   | \u6b66\u6c49   | p_without_ti           |     2353 |\n| \u676d\u5dde   | \u6df1\u5733   | p_with_ti_with_seat    |     2387 |\n| \u6b66\u6c49   | \u4e0a\u6d77   | p_with_ti_with_seat    |     2323 |\n| \u6b66\u6c49   | \u5317\u4eac   | p_with_ti_with_seat    |     2453 |\n| \u6b66\u6c49   | \u5e7f\u5dde   | p_with_ti_with_seat    |     2395 |\n| \u6b66\u6c49   | \u6210\u90fd   | p_with_ti_with_seat    |     1600 |\n| \u6b66\u6c49   | \u6210\u90fd   | p_with_ti_without_seat |      160 |\n| \u6b66\u6c49   | \u6210\u90fd   | p_without_ti           |      577 |\n| \u6b66\u6c49   | \u676d\u5dde   | p_with_ti_with_seat    |     2428 |\n| \u6b66\u6c49   | \u6df1\u5733   | p_with_ti_with_seat    |     2390 |\n| \u6df1\u5733   | \u4e0a\u6d77   | p_with_ti_with_seat    |     2251 |\n| \u6df1\u5733   | \u5317\u4eac   | p_with_ti_with_seat    |     2309 |\n| \u6df1\u5733   | \u5e7f\u5dde   | p_without_ti           |     2387 |\n| \u6df1\u5733   | \u6210\u90fd   | p_with_ti_with_seat    |     2341 |\n| \u6df1\u5733   | \u676d\u5dde   | p_with_ti_with_seat    |      600 |\n| \u6df1\u5733   | \u676d\u5dde   | p_with_ti_without_seat |       60 |\n| \u6df1\u5733   | \u676d\u5dde   | p_without_ti           |     1752 |\n| \u6df1\u5733   | \u6b66\u6c49   | p_with_ti_with_seat    |     2329 |\n+--------+--------+------------------------+----------+<\/code><\/pre>\n\n\n\n<p>\u4ece\u4e0a\u8ff0\u7684\u4e24\u4e2a\u7ed3\u679c\u5bf9\u6bd4\u6765\u770b\uff0c\u6311\u9009\u4e86\u51e0\u4e2a\u6765\u770b\uff0c\u6570\u636e\u662f\u4e00\u81f4\u7684\u3002\u6bd4\u5982\uff0c\u201c\u6df1\u5733-&gt;\u676d\u5dde\u201d\uff0c\u4e0a\u9762\u8868\u683c\u8ba1\u7b97\u5f97<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">| \u6df1\u5733   | \u676d\u5dde   |      2412 |     660.0 |       1752.0 |                 600 |               60.00000 |<\/code><\/pre>\n\n\n\n<p>\u5bf9\u6bd4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">| \u6df1\u5733   | \u676d\u5dde   | p_with_ti_with_seat    |      600 |\n| \u6df1\u5733   | \u676d\u5dde   | p_with_ti_without_seat |       60 |\n| \u6df1\u5733   | \u676d\u5dde   | p_without_ti           |     1752 |<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u7684 600\u300160\u30011752\u4e5f\u662f\u4e00\u81f4\u7684\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6700\u540e\u6309\u7167\u8f93\u51fa\u8fdb\u884c\u8c03\u6574<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">WITH\n  t_no_seat_virtual AS (\n    select\n      train_id as t_id,\n      departure_station as d_s,\n      arrival_station as a_s,\n      seat_count,\n      seat_count*0.1 as seat_count_no_seat\n    from train\n  ),\n  t_include_no_seat AS (\n    select t_id,d_s ,a_s ,seat_count, 0 as if_no_seat\n    from t_no_seat_virtual\n    union\n    select t_id,d_s ,a_s ,seat_count_no_seat, 1 as if_no_seat\n    from t_no_seat_virtual\n  )\nSELECT\n  p_01.p_id,         -- output 01\n  p_01.d_s,          -- output 02\n  p_01.a_s,          -- output 03\n  t_01.t_id as t_id, -- output 04\n  IF(\n      if_no_seat,\n      \"\" ,\n      ceil((p_01.seq-t_01.p_seat_to + t_01.seat_count)\/100)\n  ) as t_carr_id, -- output 05\n\n  CASE IF( !isnull(t_01.t_id) and if_no_seat,-1,ceil((( p_01.seq-t_01.p_seat_to + t_01.seat_count )%100)%5))\n    WHEN 1  THEN CONCAT( ceil((( p_01.seq-t_01.p_seat_to + t_01.seat_count )%100)\/5) ,\"A\")\n    WHEN 2  THEN CONCAT( ceil((( p_01.seq-t_01.p_seat_to + t_01.seat_count )%100)\/5) ,\"B\")\n    WHEN 3  THEN CONCAT( ceil((( p_01.seq-t_01.p_seat_to + t_01.seat_count )%100)\/5) ,\"C\")\n    WHEN 4  THEN CONCAT( ceil((( p_01.seq-t_01.p_seat_to + t_01.seat_count )%100)\/5) ,\"E\")\n    WHEN 0  THEN CONCAT( IF( (p_01.seq-t_01.p_seat_to + t_01.seat_count)%100 = 0, \"20\" ,ceil((( p_01.seq-t_01.p_seat_to + t_01.seat_count )%100)\/5)) ,\"F\")\n    WHEN -1 THEN \"\u65e0\u5ea7\"\n    ELSE NULL\n  END as seat_index   -- output 06\nFROM\n  (\n    select\n      ROW_NUMBER() over(PARTITION BY departure_station,arrival_station) as seq ,\n      passenger_id as p_id,\n      departure_station as d_s,\n      arrival_station as a_s\n    from\n    passenger\n  ) as p_01\n\n  LEFT JOIN\n\n  (\n    select\n      seat_count,\n      sum(seat_count)\n        over (\n               PARTITION BY d_s,a_s\n               ORDER BY     if_no_seat,t_id\n             ) as p_seat_to ,\n      t_id,\n      d_s ,\n      a_s ,\n      if_no_seat\n    from\n    t_include_no_seat\n  ) t_01\n\n  ON\n        p_01.seq &gt;= p_seat_to-seat_count + 1\n    and p_01.seq &lt;= p_seat_to\n    and p_01.d_s =  t_01.d_s\n    and p_01.a_s =  t_01.a_s\nORDER BY p_01.p_id<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8d5b\u9898\u4e0e\u6570\u636e<\/h3>\n\n\n\n<p>\u539f\u59cb\u7684\u6bd4\u8d5b\u9898\u76ee\u53c2\u8003\uff1a<a href=\"https:\/\/www.ninedata.cloud\/sql_train2024\">https:\/\/www.ninedata.cloud\/sql_train2024<\/a> \u8fd9\u91cc\u4ec5\u8ba8\u8bba\u5176\u4e2d\u7684\u201c\u8fdb\u9636\u6311\u6218\u201d\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8868\u5b9a\u4e49\uff08MySQL\uff09<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">CREATE DATABASE `game_ticket`;\n\nuse game_ticket;\n\nCREATE TABLE `passenger` (\n  `passenger_id` varchar(16) NOT NULL,\n  `departure_station` varchar(32) NOT NULL,\n  `arrival_station` varchar(32) NOT NULL,\n  PRIMARY KEY (`passenger_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n\nCREATE TABLE `train` (\n  `train_id` varchar(8) NOT NULL,\n  `departure_station` varchar(32) NOT NULL,\n  `arrival_station` varchar(32) NOT NULL,\n  `seat_count` int NOT NULL,\n  PRIMARY KEY (`train_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u8d5b\u9898\u6570\u636e<\/h4>\n\n\n\n<p>\u4e3a\u4e86\u65b9\u4fbf\u8c03\u8bd5\uff0c\u8fd9\u91cc\u4fdd\u6301\u4e86\u4e00\u4efdCSV\u7684\u8f93\u5165\u5982\u4e0b\uff0c\u4f9b\u6d4b\u8bd5\u4f7f\u7528\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.orczhou.com\/mydata\/game_ticket_passenger.csv.gz\">passenger.csv<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.orczhou.com\/mydata\/game_ticket_train.csv.gz\">train.csv<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u76f8\u5173\u9605\u8bfb<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6bd4\u8d5b\u539f\u8bd5\u9898\u8bf4\u660e\uff1a<a href=\"https:\/\/www.ninedata.cloud\/sql_train2024\">\u7528\u4e00\u6761SQL\u8bed\u53e5\u79d2\u6740\u706b\u8f66\u7968<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.orczhou.com\/index.php\/2024\/12\/using-polardb-parallel-query\/\">\u4f7f\u7528PolarDB\u7684\u5e76\u884c\u67e5\u8be2\u4f18\u5316\u5927\u8d5bSQL<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.orczhou.com\/index.php\/2024\/03\/a-sql-for-24-point-game\/\">\u4f7f\u7528\u4e00\u6761MySQL SQL\u8bed\u53e5\u5b8c\u621024\u70b9\u8ba1\u7b97<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u7b2c\u4e8c\u6b21 SQL \u7f16\u7a0b\u5927\u8d5b[1]\uff0c\u6211\u4f9d\u65e7\u662f\u8bc4\u59d4\u4e4b\u4e00\uff0c\u6240\u4ee5\u81ea\u5df1\u4e5f\u5c1d\u8bd5\u4e86\u72ec\u7acb\u5b8c\u6210\u8be5\u95ee\u9898\u7684\u6311\u6218\u3002\u8fd9\u6b21\u5927\u8d5b\u5206\u4e3a\u201c\u666e\u901a\u6311\u6218\u201d\u548c\u201c\u8fdb\u9636\u6311\u6218\u201d\u3002\u5176\u4e2d\u666e\u901a\u6311\u6218\u8f83\u4e3a\u7b80\u5355\uff0c\u672c\u6587\u4e3b\u8981\u8ba8\u8bba\u81ea\u5df1\u5b8c\u6210\u8fdb\u9636\u6311\u6218\u8fc7\u7a0b\u4e2d\u7684\u60f3\u6cd5\u4e0e\u601d\u8def\u3002 \u95ee\u9898\u63cf\u8ff0 \u539f\u59cb\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u53c2\u8003\uff1aNineData \u7b2c\u4e8c\u5c4a\u6570\u636e\u5e93\u7f16\u7a0b\u5927\u8d5b \u7528\u4e00\u6761SQL\u79d2\u6740\u706b\u8f66\u7968\uff0c\u672c\u6587\u4ec5\u8003\u8651\u5176\u4e2d\u7684\u201c\u8fdb\u9636\u6311\u6218\u201d\u3002\u8fd9\u91cc\u8be5\u201c\u8fdb\u9636\u6311\u6218\u95ee\u9898\u201d\u590d\u8ff0\u5982\u4e0b\u3002 \u6709\u5982\u4e0b\u4e24\u5f20\u8868\u5b58\u653e\u7740\u4e58\u5ba2\u4fe1\u606f\u548c\u5217\u8f66\u4fe1\u606f\uff0c\u4f7f\u7528\u4e00\u6761SQL\u7ed9\u6bcf\u4e2a\u4e58\u5ba2\u5206\u914d\u4e00\u8d9f\u5217\u8f66\u4ee5\u53ca\u5bf9\u5e94\u7684\u5ea7\u4f4d\u53f7\uff0c\u9700\u8981\u6ce8\u610f\uff0c\u9700\u8981\u8003\u8651\u8fdb\u9636\u6311\u6218\u7684\u4e00\u4e9b\u8981\u6c42\uff0c\u6bd4\u5982\uff0c\u6bcf\u8d9f\u5217\u8f66\u53ef\u4ee5\u53d1\u552e10%\u7684\u65e0\u5ea7\u8f66\u7968\uff1b\u8f66\u7968\u9700\u8981\u6709\u9650\u53d1\u552e\u6709\u5ea7\u8f66\u7968\uff0c\u7136\u540e\u624d\u5f00\u59cb\u53d1\u552e\u65e0\u5ea7\u8f66\u7968\u3002 \u793a\u4f8b\u6570\u636e\u5982\u4e0b\uff1a \u89e3\u9898\u601d\u8def \u5bf9\u4e58\u5ba2\u8fdb\u884c\u7f16\u53f7 \u9996\u5148\u5229\u7528\u6570\u636e\u5e93\u7684Windows Function\u529f\u80fd\u5bf9\u6240\u6709\u7684\u4e58\u5ba2\u5148\u5206\u7ec4\u518d\u7f16\u53f7\uff0c\u5177\u4f53\u7684\uff0c\u6309\u7167\u201c\u51fa\u53d1\u7ad9\u201d\u548c\u201c\u5230\u8fbe\u7ad9\u201d\u5206\u7ec4\uff0c\u7136\u540e\u5728\u7ec4\u5185\u8fdb\u884c\u7f16\u53f7\u3002\u6b21\u7f16\u53f7\u5219\u4e3a\u540e\u7eed\u4e58\u5ba2\u8f66\u7968\u5206\u914d\u7684\u7f16\u53f7\u3002\u4f8b\u5982\uff0c\u4ece A \u5230 B \u5730\uff0c\u4e00\u5171\u6709 2420 \u4e2a\u4e58\u5ba2\u3002\u90a3\u4e48\u4e58\u5ba2\u7684\u7f16\u53f7\u5219\u662f1&#8230;2420\uff1b\u518d\u6709\u4e58\u5ba2\u4ece C \u5230 D \u5730\uff0c\u5171\u67091800\u4e2a\u4e58\u5ba2\uff0c\u5219\u7f16\u53f7\u5219\u4e3a 1 &#8230; 1800\u3002\u5927\u6982\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u5982\u4e0b\u7684 SQL \u4ee3\u7801\u5b9e\u73b0\uff1a \u5bf9\u5217\u8f66\u8fdb\u884c\u6392\u5e8f\u548c\u8ba1\u7b97 \u4e0e\u4e58\u5ba2\u7c7b\u4f3c\u7684\uff0c\u5148\u6309\u7167\u51fa\u53d1\u548c\u5230\u8fbe\u7ad9\u70b9\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u5217\u8f66\u80fd\u591f\u5bb9\u7eb3\u7684\u4e58\u5ba2\u6570\u91cf\uff0c\u5373\u5ea7\u4f4d\u6570\u91cf\u7684 1.1 \u500d\u3002\u7136\u540e\uff0c\u5728\u5206\u7ec4\u5185\u8fdb\u884c\u201c\u7d2f\u52a0\u201d\u8ba1\u7b97\uff0c\u8be5\u7d2f\u52a0\u8ba1\u7b97\uff0c\u9700\u7b97\u51fa\u6bcf\u4e2a\u5217\u8f66\u80fd\u591f\u8fd0\u8f7d\u4e58\u5ba2\u7684\u8d77\u59cb\u5e8f\u53f7\u548c\u7ed3\u675f\u5e8f\u53f7\u3002\u4f8b\u5982\uff0c\u4ece A \u5230 B\u5730\uff0c\u5171\u6709\u5217\u8f66 G01 \u548c G07 \uff0c\u5e76\u5206\u522b\u6709 600 \u548c 1600 \u4e2a\u5ea7\u4f4d\u3002\u90a3\u4e48\uff0c\u7ecf\u8fc7\u4e0a\u8ff0\u7684\u7d2f\u52a0\u8ba1\u7b97\uff0c\u5217\u8f66 G01 \u80fd\u591f\u8fd0\u8f7d\u7684\u4e58\u5ba2\u7f16\u53f7\u5e94\u8be5\u662f 1 \u5230 660\uff0c\u800c G01 \u80fd\u591f\u8fd0\u8f7d\u7684\u4e58\u5ba2\u7f16\u53f7\u5219\u4e3a 661 \u5230 2420 \uff08\u5373\u4e3a 660 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":16999,"comment_status":"open","ping_status":"closed","sticky":false,"template":"wp-custom-template-a-1440-px-width-template","format":"standard","meta":{"_eb_attr":"","inline_featured_image":false,"_tocer_settings":[],"footnotes":""},"categories":[1],"tags":[],"class_list":["post-16319","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-simplelife"],"_links":{"self":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/16319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/comments?post=16319"}],"version-history":[{"count":45,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/16319\/revisions"}],"predecessor-version":[{"id":17014,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/16319\/revisions\/17014"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media\/16999"}],"wp:attachment":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media?parent=16319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/categories?post=16319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/tags?post=16319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}