Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MariaDB中滞后函数的默认值_Mariadb - Fatal编程技术网

MariaDB中滞后函数的默认值

MariaDB中滞后函数的默认值,mariadb,Mariadb,我正在尝试构建一个视图,该视图允许我跟踪连续两个月的支付值之间的差异。然而,当一个数字丢失时,这将是因为它是第一个条目,因此支付的金额为0。目前,我使用下面的表示前面的图,因为在MariaDB中没有实现[,default]参数 CASE WHEN ( NOT(policy_agent_month.policy_agent_month_id IS NOT NULL AND LAG(days_paid, 1) OVER (PARTITION BY claim_id O

我正在尝试构建一个视图,该视图允许我跟踪连续两个月的支付值之间的差异。然而,当一个数字丢失时,这将是因为它是第一个条目,因此支付的金额为0。目前,我使用下面的表示前面的图,因为在MariaDB中没有实现
[,default]
参数

CASE WHEN (
    NOT(policy_agent_month.policy_agent_month_id IS NOT NULL        
    AND LAG(days_paid, 1) OVER (PARTITION BY claim_id ORDER BY month_id ) IS NULL)) THEN        
         LAG(days_paid, 1) OVER ( PARTITION BY claim_id ORDER BY month_id)        
    ELSE 
         0        
    END
我的问题是,我有大约30个变量,这个函数需要应用,这使得我的代码不可读,非常笨拙。有更好的解决方案吗?

您可以在MariaDB 10.2+中使用CTE(公共表表达式)来预计算常用表达式,并将其命名以供以后使用:

with
x as ( -- first we compute the CTE that we name "x"
  select
    *,
    coalesce(
      LAG(days_paid, 1) OVER (PARTITION BY claim_id ORDER BY month_id), 
      123456
    ) as prev_month -- this expression gets the name "prev_month"
  from my_table -- or a simple/complex join here
)
select -- now the main query
  prev_month
from x
... -- rest of your query here where "prev_month" is computed.
在主查询中,
prev_month
具有滞后值,或默认值
123456
为空。

您可以使用MariaDB 10.2+中的CTE(公共表表达式)预计算常用表达式,并将其命名以供以后使用:

with
x as ( -- first we compute the CTE that we name "x"
  select
    *,
    coalesce(
      LAG(days_paid, 1) OVER (PARTITION BY claim_id ORDER BY month_id), 
      123456
    ) as prev_month -- this expression gets the name "prev_month"
  from my_table -- or a simple/complex join here
)
select -- now the main query
  prev_month
from x
... -- rest of your query here where "prev_month" is computed.

在主查询中,
prev_month
具有滞后值,或者默认值
123456
为空。

为什么将
一起使用

SELECT province, tot_pop,
       tot_pop - COALESCE(
              (LAG(tot_pop) OVER (ORDER BY tot_pop ASC)),
                        0) AS delta
    FROM provinces
    ORDER BY tot_pop asc;

+---------------------------+----------+---------+
| province                  | tot_pop  | delta   |
+---------------------------+----------+---------+
| Nunavut                   |    14585 |   14585 |
| Yukon                     |    21304 |    6719 |
| Northwest Territories     |    24571 |    3267 |
| Prince Edward Island      |    63071 |   38500 |
| Newfoundland and Labrador |   100761 |   37690 |
| New Brunswick             |   332715 |  231954 |
| Nova Scotia               |   471284 |  138569 |
| Saskatchewan              |   622467 |  151183 |
| Manitoba                  |   772672 |  150205 |
| Alberta                   |  2481213 | 1708541 |
| British Columbia          |  3287519 |  806306 |
| Quebec                    |  5321098 | 2033579 |
| Ontario                   | 10071458 | 4750360 |
+---------------------------+----------+---------+
13 rows in set (0.00 sec)
但是,它并不便宜(至少在MySQL 8.0中是如此); 这张桌子有13行

FLUSH STATUS;
SELECT ...
SHOW SESSION STATUS LIKE 'Handler%';
MySQL 8.0:
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Handler_read_rnd           | 89    |
| Handler_read_rnd_next      | 52    |
| Handler_write              | 26    |
  (and others)

MariaDB 10.3:
| Handler_read_rnd           | 77    |
| Handler_read_rnd_next      | 42    |
| Handler_tmp_write          | 13    |
| Handler_update             | 13    |

为什么将
一起使用

SELECT province, tot_pop,
       tot_pop - COALESCE(
              (LAG(tot_pop) OVER (ORDER BY tot_pop ASC)),
                        0) AS delta
    FROM provinces
    ORDER BY tot_pop asc;

+---------------------------+----------+---------+
| province                  | tot_pop  | delta   |
+---------------------------+----------+---------+
| Nunavut                   |    14585 |   14585 |
| Yukon                     |    21304 |    6719 |
| Northwest Territories     |    24571 |    3267 |
| Prince Edward Island      |    63071 |   38500 |
| Newfoundland and Labrador |   100761 |   37690 |
| New Brunswick             |   332715 |  231954 |
| Nova Scotia               |   471284 |  138569 |
| Saskatchewan              |   622467 |  151183 |
| Manitoba                  |   772672 |  150205 |
| Alberta                   |  2481213 | 1708541 |
| British Columbia          |  3287519 |  806306 |
| Quebec                    |  5321098 | 2033579 |
| Ontario                   | 10071458 | 4750360 |
+---------------------------+----------+---------+
13 rows in set (0.00 sec)
但是,它并不便宜(至少在MySQL 8.0中是如此); 这张桌子有13行

FLUSH STATUS;
SELECT ...
SHOW SESSION STATUS LIKE 'Handler%';
MySQL 8.0:
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Handler_read_rnd           | 89    |
| Handler_read_rnd_next      | 52    |
| Handler_write              | 26    |
  (and others)

MariaDB 10.3:
| Handler_read_rnd           | 77    |
| Handler_read_rnd_next      | 42    |
| Handler_tmp_write          | 13    |
| Handler_update             | 13    |

对于这种情况,LAG()和LEAD()的默认值始终为1条记录“后退”或“前进”
LAG()
LAG(,1)
的含义相同。在其他版本的SQL中,“default”参数指的是处理空值。我现在不是在为“距离”而挣扎。因为它的标题,你对它有“问题”或者一个问题。。我建议你读一读。@TheImpler的答案有什么问题?“其他版本的SQL中的'default'参数指的是处理空值”,因为它似乎是你所追求的这个“default”值的有效解决方法。看起来没有什么问题。在我提交之前,我只是先做一些实现工作。在这方面,LAG()和LEAD()的默认值总是1条记录“back”或“forward”
LAG()
LAG(,1)
的含义相同。在其他版本的SQL中,“default”参数指的是处理空值。我现在不是在为“距离”而挣扎。因为它的标题,你对它有“问题”或者一个问题。。我建议你读一读。@TheImpler的答案有什么问题?“其他版本的SQL中的'default'参数指的是处理空值”,因为它似乎是你所追求的这个“default”值的有效解决方法。看起来没有什么问题。在我承诺之前,我只是先着手实施。