{"id":6013,"date":"2025-03-26T19:49:00","date_gmt":"2025-03-26T11:49:00","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=6013"},"modified":"2025-03-26T11:31:52","modified_gmt":"2025-03-26T03:31:52","slug":"hive-sql%e5%a6%82%e4%bd%95%e5%8f%91%e7%8e%b0%e6%97%a5%e6%9c%9f%e8%be%83%e6%96%b0%e4%bd%86%e8%bd%af%e4%bb%b6%e7%89%88%e6%9c%ac%e8%be%83%e8%80%81%e7%9a%84%e6%83%85%e5%86%b5","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/6013.html","title":{"rendered":"Hive SQL\u5982\u4f55\u53d1\u73b0\u65e5\u671f\u8f83\u65b0\u4f46\u8f6f\u4ef6\u7248\u672c\u8f83\u8001\u7684\u60c5\u51b5"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u7b80\u5355\u6574\u7406\u4e00\u4e0b\uff0c\u65b9\u4fbf\u540e\u9762\u6709\u9700\u8981\u7684\u65f6\u5019\u53c2\u8003\u3002<strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">\u6838\u5fc3\u601d\u8def\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u6309\u7167\u65e5\u671f\u6392\u5e8f\uff0c\u5e76\u68c0\u67e5\u6bcf\u4e2a\u65e5\u671f\u7684\u8f6f\u4ef6\u7248\u672c\u662f\u5426\u6bd4\u4e4b\u524d\u7684\u7248\u672c\u66f4\u65e7\u3002<\/mark><\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*\n\u6ce8\u610f\u4e8b\u9879\uff1a\n1. \u5148\u505a\u6570\u636e\u6e05\u6d17\u548c\u53bb\u91cd\n2. \u6bd4\u8f83\u9700\u8981\u57fa\u4e8e\u76f8\u540c\u7684\u8bbe\u5907\u7c7b\u578b\u6765\u6bd4\u8f83\uff0c\u6bd4\u5982 Mac \u7684\u548c Mac \u7684\u6bd4\u8f83\u3001Windows\u548cWindows\u7684\u6bd4\u8f83\n3. \u6bd4\u8f83\u9700\u8981\u57fa\u4e8e\u540c\u4e00\u4e2a\u4eba\u7684\u540c\u4e00\u7c7b\u8bbe\u5907\u6765\u6bd4\u8f83\uff0c\u6240\u4ee5 lag \u8981\u7ed3\u5408 PARTITION BY \u8fdb\u884c\u9002\u5f53\u5206\u533a\n*\/\nwith user_version_info as (\nSELECT DISTINCT\n  substr(`timestamp`,1,10) as dt,\n  user_name,\n  regexp_extract(ua, '(Chrome\/&#91;^ ]+)', 1) AS software_ver,\n  split(replace(ua, ')', '('),'\\\\(')&#91;1] as ua1\n,ua\n,case\nwhen ua like '%iPhone%' then 'iPhone'\nwhen ua like '%Android%' then 'Android'\nwhen ua like '%Windows%' then 'Windows'\nwhen ua like '%Macintosh%' then 'Macintosh'\nelse 'unknown'\nend as device_tag\nFROM\n  ixyzero.access_logs\nWHERE\n  date BETWEEN '20250101' AND '20250101'\n  and ua not like '%keywords%' --\u7528\u4e8e\u6392\u9664\u4ece\u7279\u5b9a\u5ba2\u6237\u7aef\u53d1\u8d77\u7684\u8bf7\u6c42\uff08\u56e0\u4e3a\u5b83\u4eec\u7684Chrome\u6d4f\u89c8\u5668\u7248\u672c\u592a\u4f4e\uff0c\u4f1a\u5f71\u54cd\u6b63\u5e38\u5206\u6790\uff09\n)\n,cleaned_logs as (\n    SELECT\n        user_name,\n        device_tag,ua1,ua,\n        dt, software_ver\n        ,lag(dt) OVER(PARTITION BY user_name,device_tag ORDER BY dt) as last_dt\n        ,lag(software_ver) OVER(PARTITION BY user_name,device_tag ORDER BY dt) as last_ver\n    FROM\n        user_version_info\n)\n\nSELECT\n*\nFROM\ncleaned_logs\nWHERE software_ver &lt; last_ver\nand dt &lt;&gt; last_dt --\u7528\u4e8e\u6392\u9664\u5347\u7ea7\u7684\u90a3\u4e00\u5929(\u540c\u65f6\u5b58\u5728\u9ad8\u4f4e2\u4e2a\u7248\u672c)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u501f\u52a9\u901a\u4e49\u5343\u95ee\u6765\u601d\u8003\u548c\u89e3\u51b3\u95ee\u9898\u7684\u8fc7\u7a0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>hive SQL\u5982\u4f55\u63d0\u53d6\u65e5\u5fd7UserAgent\u5b57\u6bb5\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\u4e32\uff1f<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u5e38\u89c4\u7684UserAgent\u5b57\u6bb5\u5185\u5bb9\u4e00\u822c\u957f\u8fd9\u6837\uff1a\n-- Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/129.0.0.0 Safari\/537.36\n\n,regexp_extract(ua, '(Chrome\/&#91;^ ]+)', 1) AS chrome_ver --\u63d0\u53d6Chrome\u6d4f\u89c8\u5668\u7248\u672c\n,split(replace(ua, ')', '('),'\\\\(')&#91;1] as ua1 --\u63d0\u53d6\u7b2c\u4e00\u4e2a\u62ec\u53f7\u4e2d\u7684\u5185\u5bb9<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>hive SQL\u5982\u4f55\u5206\u6790\u5b9a\u4f4d\u51fa\u90a3\u79cd\u65e5\u671f\u6bd4\u8f83\u65b0\uff0c\u4f46\u662f\u4f7f\u7528\u7684\u8f6f\u4ef6\u7248\u672c\u5374\u66f4\u8001\u7684\u60c5\u51b5\u5462\uff1f<\/li>\n<\/ol>\n\n\n\n<p>\u6bd4\u5982\u4e00\u884c\u65e5\u5fd7\u4e2d\u7684\u6709\u591a\u4e2a\u5b57\u6bb5 <code>date<\/code> \u8868\u793a\u65e5\u671f\uff0c<code>software_version<\/code> \u8868\u793a\u8f6f\u4ef6\u7248\u672c\uff0c\u5982\u4f55\u5206\u6790\u51fa\u90a3\u4e9b\u6709\u65b0\u65e7\u7248\u672c\u8f6f\u4ef6\u4ea4\u66ff\u4f7f\u7528\u7684\u60c5\u51b5\u5462\uff1f<\/p>\n\n\n\n<p>\u5728 Hive SQL \u4e2d\u5206\u6790\u548c\u5b9a\u4f4d\u65e5\u671f\u8f83\u65b0\u4f46\u8f6f\u4ef6\u7248\u672c\u8f83\u8001\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\u3002<strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">\u6838\u5fc3\u601d\u8def\u662f\u6309\u7167\u65e5\u671f\u6392\u5e8f\uff0c\u5e76\u68c0\u67e5\u6bcf\u4e2a\u65e5\u671f\u7684\u8f6f\u4ef6\u7248\u672c\u662f\u5426\u6bd4\u4e4b\u524d\u7684\u7248\u672c\u66f4\u65e7\u3002<\/mark><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5047\u8bbe\u8868\u7ed3\u6784<\/h3>\n\n\n\n<p>\u5047\u8bbe\u4f60\u7684\u8868\u540d\u4e3a <code>logs<\/code>\uff0c\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>date<\/code>\uff1a\u8868\u793a\u65e5\u671f\uff08\u683c\u5f0f\u4e3a <code>YYYY-MM-DD<\/code> \u6216\u5176\u4ed6\u53ef\u6bd4\u8f83\u7684\u683c\u5f0f\uff09\u3002<\/li>\n\n\n\n<li><code>software_version<\/code>\uff1a\u8868\u793a\u8f6f\u4ef6\u7248\u672c\u53f7\uff08\u4f8b\u5982 <code>1.2.3<\/code> \u6216 <code>v1.2.3<\/code>\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4e3a\u4e86\u4fbf\u4e8e\u5206\u6790\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u65e5\u671f\u5b57\u6bb5<\/strong>\u662f\u53ef\u6bd4\u8f83\u7684\uff08\u5982\u5b57\u7b26\u4e32\u683c\u5f0f\u7684\u65e5\u671f\u9700\u8981\u8f6c\u6362\u4e3a\u65e5\u671f\u7c7b\u578b\uff09\u3002<\/li>\n\n\n\n<li><strong>\u7248\u672c\u53f7\u5b57\u6bb5<\/strong>\u662f\u53ef\u6bd4\u8f83\u7684\uff08\u9700\u8981\u5c06\u7248\u672c\u53f7\u4ece\u5b57\u7b26\u4e32\u683c\u5f0f\u8f6c\u6362\u4e3a\u6570\u503c\u6216\u6807\u51c6\u5316\u683c\u5f0f\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u6570\u636e\u51c6\u5907\uff1a\u6807\u51c6\u5316\u65e5\u671f\u548c\u7248\u672c\u53f7<\/h4>\n\n\n\n<p>\u5982\u679c <code>date<\/code> \u548c <code>software_version<\/code> \u662f\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u6211\u4eec\u9700\u8981\u5148\u5c06\u5176\u6807\u51c6\u5316\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c06 <code>date<\/code> \u8f6c\u6362\u4e3a\u65e5\u671f\u7c7b\u578b\u3002<\/li>\n\n\n\n<li>\u5c06 <code>software_version<\/code> \u6807\u51c6\u5316\u4e3a\u53ef\u6bd4\u8f83\u7684\u5f62\u5f0f\uff08\u5982\u5206\u9694\u7b26\u66ff\u6362\u3001\u62c6\u5206\u4e3a\u591a\u4e2a\u5b57\u6bb5\u7b49\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u793a\u4f8b\uff1a\u6807\u51c6\u5316\u65e5\u671f\u548c\u7248\u672c\u53f7\nSELECT\n  date,\n  software_version,\n  from_unixtime(unix_timestamp(date, 'yyyy-MM-dd')) AS standardized_date, -- \u8f6c\u6362\u4e3a\u65e5\u671f\u7c7b\u578b\n  regexp_replace(software_version, '&#91;^0-9.]', '') AS clean_version -- \u79fb\u9664\u975e\u6570\u5b57\u548c\u70b9\u7684\u5b57\u7b26\nFROM logs;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u6309\u65e5\u671f\u6392\u5e8f\u5e76\u6bd4\u8f83\u7248\u672c\u53f7<\/h4>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u6309\u65e5\u671f\u6392\u5e8f\uff0c\u5e76\u6bd4\u8f83\u6bcf\u4e2a\u8bb0\u5f55\u7684\u7248\u672c\u53f7\u4e0e\u5176\u524d\u4e00\u6761\u8bb0\u5f55\u7684\u7248\u672c\u53f7\uff0c\u627e\u51fa\u65e5\u671f\u8f83\u65b0\u4f46\u7248\u672c\u53f7\u8f83\u8001\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>WITH ranked_logs AS (\n  SELECT\n    date,\n    software_version,\n    LAG(software_version) OVER (ORDER BY date) AS prev_version, -- \u83b7\u53d6\u524d\u4e00\u6761\u8bb0\u5f55\u7684\u7248\u672c\u53f7\n    LAG(date) OVER (ORDER BY date) AS prev_date -- \u83b7\u53d6\u524d\u4e00\u6761\u8bb0\u5f55\u7684\u65e5\u671f\n  FROM logs\n),\ncleaned_logs AS (\n  SELECT\n    date,\n    software_version,\n    prev_version,\n    prev_date,\n    CAST(regexp_replace(software_version, '&#91;^0-9.]', '') AS STRING) AS clean_current_version, -- \u6e05\u6d17\u5f53\u524d\u7248\u672c\u53f7\n    CAST(regexp_replace(prev_version, '&#91;^0-9.]', '') AS STRING) AS clean_prev_version -- \u6e05\u6d17\u524d\u4e00\u6761\u8bb0\u5f55\u7684\u7248\u672c\u53f7\n  FROM ranked_logs\n)\nSELECT\n  date,\n  software_version,\n  prev_date,\n  prev_version\nFROM cleaned_logs\nWHERE clean_current_version &lt; clean_prev_version; -- \u5f53\u524d\u7248\u672c\u53f7\u6bd4\u524d\u4e00\u6761\u8bb0\u5f55\u7684\u7248\u672c\u53f7\u66f4\u65e7<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u89e3\u91ca<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>LAG<\/code> \u51fd\u6570<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"has-vivid-red-color has-text-color has-link-color wp-elements-5d2aa8f1b8780d0d78fb0228c2ba41bc\"><code>LAG(column)<\/code> \u662f\u7a97\u53e3\u51fd\u6570\uff0c\u7528\u4e8e\u83b7\u53d6\u5f53\u524d\u884c\u4e4b\u524d\u7684\u4e00\u884c\u6570\u636e\uff08\u6309\u6307\u5b9a\u7684\u6392\u5e8f\u89c4\u5219\uff09\u3002<\/li>\n\n\n\n<li>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528 <code>LAG<\/code> \u6765\u83b7\u53d6\u524d\u4e00\u6761\u8bb0\u5f55\u7684\u65e5\u671f\u548c\u7248\u672c\u53f7\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7248\u672c\u53f7\u6bd4\u8f83<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>regexp_replace<\/code> \u6e05\u6d17\u7248\u672c\u53f7\uff0c\u79fb\u9664\u975e\u6570\u5b57\u548c\u70b9\u7684\u5b57\u7b26\uff08\u4f8b\u5982\u4ece <code>v1.2.3<\/code> \u63d0\u53d6 <code>1.2.3<\/code>\uff09\u3002<\/li>\n\n\n\n<li>\u5c06\u6e05\u6d17\u540e\u7684\u7248\u672c\u53f7\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6216\u6570\u503c\u5f62\u5f0f\u8fdb\u884c\u6bd4\u8f83\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8fc7\u6ee4\u6761\u4ef6<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>WHERE clean_current_version &lt; clean_prev_version<\/code>\uff1a\u7b5b\u9009\u51fa\u5f53\u524d\u7248\u672c\u53f7\u6bd4\u524d\u4e00\u6761\u8bb0\u5f55\u7248\u672c\u53f7\u66f4\u65e7\u7684\u60c5\u51b5\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\u6570\u636e\u4e0e\u7ed3\u679c<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u8f93\u5165\u6570\u636e<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>date<\/th><th>software_version<\/th><\/tr><\/thead><tbody><tr><td>2023-01-01<\/td><td>v1.2.3<\/td><\/tr><tr><td>2023-01-02<\/td><td>v1.2.5<\/td><\/tr><tr><td>2023-01-03<\/td><td>v1.2.4<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2\u7ed3\u679c<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>date<\/th><th>software_version<\/th><th>prev_date<\/th><th>prev_version<\/th><\/tr><\/thead><tbody><tr><td>2023-01-03<\/td><td>v1.2.4<\/td><td>2023-01-02<\/td><td>v1.2.5<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u89e3\u91ca\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728 <code>2023-01-03<\/code> \u7684\u8bb0\u5f55\u4e2d\uff0c\u7248\u672c\u53f7 <code>v1.2.4<\/code> \u6bd4\u524d\u4e00\u5929\u7684 <code>v1.2.5<\/code> \u66f4\u65e7\uff0c\u56e0\u6b64\u88ab\u7b5b\u9009\u51fa\u6765\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7248\u672c\u53f7\u683c\u5f0f<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u7248\u672c\u53f7\u683c\u5f0f\u590d\u6742\uff08\u5982\u5305\u542b\u5b57\u6bcd\u3001\u7279\u6b8a\u5b57\u7b26\u6216\u975e\u6807\u51c6\u683c\u5f0f\uff09\uff0c\u9700\u8981\u8fdb\u4e00\u6b65\u6e05\u6d17\u548c\u6807\u51c6\u5316\u3002<\/li>\n\n\n\n<li class=\"has-vivid-red-color has-text-color has-link-color wp-elements-00151d428b65fe547173cb8b6a142dfe\">\u5bf9\u4e8e\u591a\u6bb5\u7248\u672c\u53f7\uff08\u5982 <code>1.2.3.4<\/code>\uff09\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u5176\u62c6\u5206\u4e3a\u591a\u4e2a\u5b57\u6bb5\uff08\u5982 <code>major<\/code>, <code>minor<\/code>, <code>patch<\/code>\uff09\uff0c\u7136\u540e\u9010\u6bb5\u6bd4\u8f83\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6027\u80fd\u4f18\u5316<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u6570\u636e\u91cf\u8f83\u5927\uff0c\u5efa\u8bae\u5bf9 <code>date<\/code> \u5b57\u6bb5\u5efa\u7acb\u7d22\u5f15\uff0c\u6216\u8005\u6839\u636e\u5b9e\u9645\u9700\u6c42\u5206\u533a\u8868\u4ee5\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u65f6\u95f4\u8303\u56f4\u9650\u5236<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u53ea\u9700\u8981\u5206\u6790\u7279\u5b9a\u65f6\u95f4\u6bb5\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u5728\u67e5\u8be2\u4e2d\u6dfb\u52a0 <code>WHERE date BETWEEN ...<\/code> \u8fdb\u884c\u8fc7\u6ee4\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6709\u6548\u5730\u5206\u6790\u548c\u5b9a\u4f4d\u65e5\u671f\u8f83\u65b0\u4f46\u8f6f\u4ef6\u7248\u672c\u8f83\u8001\u7684\u60c5\u51b5\uff0c\u4ece\u800c\u53d1\u73b0\u6f5c\u5728\u7684\u95ee\u9898\u6216\u5f02\u5e38\u884c\u4e3a\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>question<br>\u8868\u793a\u8f6f\u4ef6\u7248\u672c\u7684software_version\u662f\u591a\u6bb5\u7248\u672c\u53f7\u683c\u5f0f\u7684\uff0c\u4e14\u9700\u8981\u4ee5\u6570\u503c\u5f62\u5f0f\u8fdb\u884c\u6bd4\u8f83\uff0c\u8bf7\u7ed9\u51fa\u5b8c\u6574\u53ef\u7528\u7684SQL\u4ee3\u7801<\/p>\n\n\n\n<p>answer<\/p>\n\n\n\n<p>\u5728 Hive SQL \u4e2d\uff0c\u5982\u679c <code>software_version<\/code> \u662f\u591a\u6bb5\u7248\u672c\u53f7\u683c\u5f0f\uff08\u4f8b\u5982 <code>1.2.3.4<\/code>\uff09\uff0c\u5e76\u4e14\u9700\u8981\u4ee5\u6570\u503c\u5f62\u5f0f\u8fdb\u884c\u6bd4\u8f83\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6807\u51c6\u5316\u7248\u672c\u53f7<\/strong>\uff1a\u5c06\u591a\u6bb5\u7248\u672c\u53f7\u62c6\u5206\u4e3a\u591a\u4e2a\u5b57\u6bb5\uff08\u5982 <code>major<\/code>, <code>minor<\/code>, <code>patch<\/code> \u7b49\uff09\u3002<\/li>\n\n\n\n<li class=\"has-vivid-red-color has-text-color has-link-color wp-elements-b69256e5dd93c82b4edc19a958672d16\"><strong>\u9010\u6bb5\u6bd4\u8f83<\/strong>\uff1a\u6309\u7167\u4f18\u5148\u7ea7\uff08\u4ece\u5de6\u5230\u53f3\uff09\u9010\u6bb5\u6bd4\u8f83\u6bcf\u4e2a\u5b57\u6bb5\u7684\u6570\u503c\u5927\u5c0f\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u7a97\u53e3\u51fd\u6570<\/strong>\uff1a\u5229\u7528 <code>LAG<\/code> \u51fd\u6570\u83b7\u53d6\u524d\u4e00\u6761\u8bb0\u5f55\u7684\u7248\u672c\u53f7\uff0c\u5e76\u8fdb\u884c\u9010\u6bb5\u6bd4\u8f83\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684 SQL \u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5047\u8bbe\u8868\u7ed3\u6784<\/h3>\n\n\n\n<p>\u5047\u8bbe\u4f60\u7684\u8868\u540d\u4e3a <code>logs<\/code>\uff0c\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>date<\/code>\uff1a\u65e5\u671f\u5b57\u6bb5\uff08\u683c\u5f0f\u4e3a <code>YYYY-MM-DD<\/code>\uff09\u3002<\/li>\n\n\n\n<li><code>software_version<\/code>\uff1a\u8f6f\u4ef6\u7248\u672c\u53f7\u5b57\u6bb5\uff08\u683c\u5f0f\u4e3a <code>1.2.3.4<\/code> \u6216\u7c7b\u4f3c\u683c\u5f0f\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b8c\u6574 SQL \u4ee3\u7801<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>WITH parsed_logs AS (\n  -- \u89e3\u6790\u7248\u672c\u53f7\u4e3a\u591a\u4e2a\u5b57\u6bb5\n  SELECT\n    date,\n    software_version,\n    CAST(SPLIT(software_version, '\\\\.')&#91;0] AS INT) AS major, -- \u4e3b\u7248\u672c\u53f7\n    CAST(SPLIT(software_version, '\\\\.')&#91;1] AS INT) AS minor, -- \u6b21\u7248\u672c\u53f7\n    CAST(SPLIT(software_version, '\\\\.')&#91;2] AS INT) AS patch, -- \u4fee\u8ba2\u7248\u672c\u53f7\n    CAST(SPLIT(software_version, '\\\\.')&#91;3] AS INT) AS build  -- \u6784\u5efa\u7248\u672c\u53f7\uff08\u53ef\u9009\uff09\n  FROM logs\n),\nranked_logs AS (\n  -- \u4f7f\u7528\u7a97\u53e3\u51fd\u6570\u83b7\u53d6\u524d\u4e00\u6761\u8bb0\u5f55\u7684\u7248\u672c\u4fe1\u606f\n  SELECT\n    date,\n    software_version,\n    major,\n    minor,\n    patch,\n    build,\n    LAG(major) OVER (ORDER BY date) AS prev_major, -- \u524d\u4e00\u6761\u8bb0\u5f55\u7684\u4e3b\u7248\u672c\u53f7\n    LAG(minor) OVER (ORDER BY date) AS prev_minor, -- \u524d\u4e00\u6761\u8bb0\u5f55\u7684\u6b21\u7248\u672c\u53f7\n    LAG(patch) OVER (ORDER BY date) AS prev_patch, -- \u524d\u4e00\u6761\u8bb0\u5f55\u7684\u4fee\u8ba2\u7248\u672c\u53f7\n    LAG(build) OVER (ORDER BY date) AS prev_build, -- \u524d\u4e00\u6761\u8bb0\u5f55\u7684\u6784\u5efa\u7248\u672c\u53f7\n    LAG(date) OVER (ORDER BY date) AS prev_date   -- \u524d\u4e00\u6761\u8bb0\u5f55\u7684\u65e5\u671f\n  FROM parsed_logs\n)\n-- \u7b5b\u9009\u51fa\u65e5\u671f\u8f83\u65b0\u4f46\u7248\u672c\u53f7\u8f83\u65e7\u7684\u60c5\u51b5\nSELECT\n  date,\n  software_version,\n  prev_date,\n  CONCAT_WS('.', prev_major, prev_minor, prev_patch, prev_build) AS prev_software_version\nFROM ranked_logs\nWHERE\n  major &lt; prev_major OR\n  (major = prev_major AND minor &lt; prev_minor) OR\n  (major = prev_major AND minor = prev_minor AND patch &lt; prev_patch) OR\n  (major = prev_major AND minor = prev_minor AND patch = prev_patch AND build &lt; prev_build);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u89e3\u91ca<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. <strong>\u89e3\u6790\u7248\u672c\u53f7<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>SPLIT<\/code> \u51fd\u6570\u5c06 <code>software_version<\/code> \u5b57\u7b26\u4e32\u6309\u70b9\u53f7 <code>.<\/code> \u5206\u9694\u6210\u591a\u4e2a\u90e8\u5206\u3002<\/li>\n\n\n\n<li>\u5c06\u6bcf\u4e2a\u90e8\u5206\u8f6c\u6362\u4e3a\u6574\u6570\u7c7b\u578b\uff08<code>CAST(... AS INT)<\/code>\uff09\uff0c\u4ee5\u4fbf\u8fdb\u884c\u6570\u503c\u6bd4\u8f83\u3002<\/li>\n\n\n\n<li>\u4f8b\u5982\uff0c\u5bf9\u4e8e\u7248\u672c\u53f7 <code>1.2.3.4<\/code>\uff1a\n<ul class=\"wp-block-list\">\n<li><code>major = 1<\/code><\/li>\n\n\n\n<li><code>minor = 2<\/code><\/li>\n\n\n\n<li><code>patch = 3<\/code><\/li>\n\n\n\n<li><code>build = 4<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. <strong>\u7a97\u53e3\u51fd\u6570\u83b7\u53d6\u524d\u4e00\u6761\u8bb0\u5f55<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>LAG<\/code> \u51fd\u6570\u83b7\u53d6\u5f53\u524d\u8bb0\u5f55\u7684\u524d\u4e00\u6761\u8bb0\u5f55\u7684\u7248\u672c\u4fe1\u606f\uff08\u5305\u62ec\u4e3b\u7248\u672c\u53f7\u3001\u6b21\u7248\u672c\u53f7\u7b49\uff09\u3002<\/li>\n\n\n\n<li><code>LAG<\/code> \u7684\u6392\u5e8f\u4f9d\u636e\u662f <code>date<\/code> \u5b57\u6bb5\uff0c\u786e\u4fdd\u6211\u4eec\u6309\u65f6\u95f4\u987a\u5e8f\u5206\u6790\u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3. <strong>\u9010\u6bb5\u6bd4\u8f83\u7248\u672c\u53f7<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6309\u7167\u4f18\u5148\u7ea7\u9010\u6bb5\u6bd4\u8f83\u7248\u672c\u53f7\uff1a\n<ul class=\"wp-block-list\">\n<li>\u9996\u5148\u6bd4\u8f83\u4e3b\u7248\u672c\u53f7\uff08<code>major<\/code>\uff09\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u4e3b\u7248\u672c\u53f7\u76f8\u540c\uff0c\u5219\u6bd4\u8f83\u6b21\u7248\u672c\u53f7\uff08<code>minor<\/code>\uff09\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u6b21\u7248\u672c\u53f7\u4e5f\u76f8\u540c\uff0c\u5219\u6bd4\u8f83\u4fee\u8ba2\u7248\u672c\u53f7\uff08<code>patch<\/code>\uff09\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u4fee\u8ba2\u7248\u672c\u53f7\u4ecd\u7136\u76f8\u540c\uff0c\u5219\u6bd4\u8f83\u6784\u5efa\u7248\u672c\u53f7\uff08<code>build<\/code>\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u5982\u679c\u5f53\u524d\u8bb0\u5f55\u7684\u7248\u672c\u53f7\u5728\u4efb\u610f\u4e00\u6bb5\u4e0a\u5c0f\u4e8e\u524d\u4e00\u6761\u8bb0\u5f55\u7684\u7248\u672c\u53f7\uff0c\u5219\u8ba4\u4e3a\u5f53\u524d\u8bb0\u5f55\u7684\u7248\u672c\u53f7\u66f4\u65e7\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4. <strong>\u7b5b\u9009\u6761\u4ef6<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>WHERE<\/code> \u5b50\u53e5\u7528\u4e8e\u7b5b\u9009\u51fa\u65e5\u671f\u8f83\u65b0\u4f46\u7248\u672c\u53f7\u8f83\u65e7\u7684\u60c5\u51b5\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\u6570\u636e\u4e0e\u7ed3\u679c<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u8f93\u5165\u6570\u636e<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>date<\/th><th>software_version<\/th><\/tr><\/thead><tbody><tr><td>2023-01-01<\/td><td>1.2.3.4<\/td><\/tr><tr><td>2023-01-02<\/td><td>1.2.5.0<\/td><\/tr><tr><td>2023-01-03<\/td><td>1.2.4.9<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2\u7ed3\u679c<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>date<\/th><th>software_version<\/th><th>prev_date<\/th><th>prev_software_version<\/th><\/tr><\/thead><tbody><tr><td>2023-01-03<\/td><td>1.2.4.9<\/td><td>2023-01-02<\/td><td>1.2.5.0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u89e3\u91ca\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728 <code>2023-01-03<\/code> \u7684\u8bb0\u5f55\u4e2d\uff0c\u7248\u672c\u53f7 <code>1.2.4.9<\/code> \u6bd4\u524d\u4e00\u5929\u7684 <code>1.2.5.0<\/code> \u66f4\u65e7\uff08\u56e0\u4e3a <code>patch=4<\/code> \u5c0f\u4e8e <code>patch=5<\/code>\uff09\uff0c\u56e0\u6b64\u88ab\u7b5b\u9009\u51fa\u6765\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7248\u672c\u53f7\u957f\u5ea6\u4e0d\u4e00\u81f4<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"has-vivid-red-color has-text-color has-link-color wp-elements-9e6903c4c2b6bab33bfe8a40abf8708b\">\u5982\u679c\u67d0\u4e9b\u7248\u672c\u53f7\u7f3a\u5c11\u67d0\u4e9b\u6bb5\uff08\u5982 <code>1.2<\/code> \u6216 <code>1.2.3<\/code>\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u9ed8\u8ba4\u503c\u586b\u5145\u7f3a\u5931\u7684\u6bb5\u3002\u4f8b\u5982\uff1a<br><code>sql COALESCE(CAST(SPLIT(software_version, '\\\\.')[3] AS INT), 0) AS build<\/code><br>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u7248\u672c\u53f7\u90fd\u6709\u5b8c\u6574\u7684\u56db\u6bb5\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6027\u80fd\u4f18\u5316<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u6570\u636e\u91cf\u8f83\u5927\uff0c\u5efa\u8bae\u5bf9 <code>date<\/code> \u5b57\u6bb5\u5efa\u7acb\u7d22\u5f15\uff0c\u6216\u8005\u6839\u636e\u5b9e\u9645\u9700\u6c42\u5206\u533a\u8868\u4ee5\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u590d\u6742\u7248\u672c\u53f7\u683c\u5f0f<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u7248\u672c\u53f7\u4e2d\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff08\u5982 <code>v1.2.3<\/code> \u6216 <code>1.2-beta<\/code>\uff09\uff0c\u9700\u8981\u5148\u6e05\u6d17\u7248\u672c\u53f7\uff0c\u79fb\u9664\u975e\u6570\u5b57\u548c\u70b9\u7684\u5b57\u7b26\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6709\u6548\u5730\u5206\u6790\u548c\u5b9a\u4f4d\u65e5\u671f\u8f83\u65b0\u4f46\u8f6f\u4ef6\u7248\u672c\u8f83\u8001\u7684\u60c5\u51b5\uff0c\u4ece\u800c\u53d1\u73b0\u6f5c\u5728\u7684\u95ee\u9898\u6216\u5f02\u5e38\u884c\u4e3a\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>\u901a\u4e49\u5343\u95ee<br><a href=\"https:\/\/tongyi.aliyun.com\/qianwen\/\">https:\/\/tongyi.aliyun.com\/qianwen\/<\/a><\/p>\n\n\n\n<p>DeepSeek<br><a href=\"https:\/\/chat.deepseek.com\/\">https:\/\/chat.deepseek.com\/<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u7b80\u5355\u6574\u7406\u4e00\u4e0b\uff0c\u65b9\u4fbf\u540e\u9762\u6709\u9700\u8981\u7684\u65f6\u5019\u53c2\u8003\u3002\u6838\u5fc3\u601d\u8def\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u6309\u7167\u65e5\u671f\u6392\u5e8f\uff0c\u5e76\u68c0\u67e5\u6bcf\u4e2a\u65e5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,23,7],"tags":[1261,659,2019,1262,19,1373],"class_list":["post-6013","post","type-post","status-publish","format-standard","hentry","category-database","category-knowledgebase-2","category-programing","tag-hive","tag-lag","tag-regexp_extract","tag-sql","tag-tips","tag-1373"],"views":1035,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/6013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=6013"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/6013\/revisions"}],"predecessor-version":[{"id":6014,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/6013\/revisions\/6014"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=6013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=6013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=6013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}