pgsql 同比或者下降

  WITH total_t AS (
          select COUNT ( T.* ) AS COUNT FROM table t
 t.create_time BETWEEN to_timestamp( #{startTime},'yyyy-mm-dd')
                 and to_timestamp( #{endTime},'yyyy-mm-dd')

        ),
        type_hbc_t as(
     select COUNT ( T.* ) AS COUNT FROM table t
   t.create_time BETWEEN to_timestamp( #{startTime},'yyyy-mm-dd')
                 and to_timestamp( #{endTime},'yyyy-mm-dd')
        ),
        type_tbc_t as (
          select COUNT ( T.* ) AS COUNT FROM table t
 t.create_time BETWEEN to_timestamp( #{startTime},'yyyy-mm-dd')
                 and to_timestamp( #{endTime},'yyyy-mm-dd')
        )
        SELECT
        t2.count as total,
      (case when t3.COUNT - t2.COUNT > 0 then 'up' else 'down' end ) as hb_up_down,
		(case when t4.COUNT - t2.COUNT > 0 then 'up' else 'down' end ) as tb_up_down,
         CASE WHEN t3.COUNT - t2.COUNT > 0 THEN round((t3.COUNT - t2.COUNT)*100.0/t2.count::numeric ,2)
        else round(abs(t3.COUNT - t2.COUNT)*100.0/t2.count::numeric ,2)  END AS hb,
         CASE WHEN t4.COUNT - t2.COUNT > 0 THEN round((t4.COUNT - t2.COUNT) *100.0/t2.count::numeric ,2)
        else round(abs(t4.COUNT - t2.COUNT)*100.0/t4.count::numeric ,2)  END AS tb
        FROM total_t t2, type_hbc_t t3,type_tbc_t t4 





<sql id="hbConditionSql">
        <where>
            <if test="type != null and type != '' and type=='1'.toString()">
                <![CDATA[ T.create_time BETWEEN  #{startTime}::timestamp + '-1 day'
            and  #{endTime}::timestamp + '- 1 day' ]]>
            </if>

            <if test="type != null and type != '' and type=='2'.toString()">
                <![CDATA[ T.create_time BETWEEN  #{startTime}::timestamp + '-1 week'
            and  #{endTime}::timestamp + '- 1 week' ]]>
            </if>

            <if test="type != null and type != '' and type=='3'.toString()">
                <![CDATA[ T.create_time BETWEEN  #{startTime}::timestamp + '-1 month'
            and  #{endTime}::timestamp + '-1 month' ]]>
            </if>

            <if test="type != null and type != '' and type=='4'.toString()">
             <![CDATA[ T.create_time BETWEEN  #{startTime}::timestamp + '-3 month'
              and  #{endTime}::timestamp + '-3 month' ]]>
            </if>

            <if test="type != null and type != '' and type=='4'.toString()">
                <![CDATA[ T.create_time BETWEEN  #{startTime}::timestamp + '-1 year'
              and  #{endTime}::timestamp + '-1 year' ]]>
            </if>

        </where>
    </sql>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pgsql半同步复制是指在PostgreSQL数据库中实现的一种数据复制机制,它提供了更高的数据可靠性和容错能力。在传统的异步复制中,主数据库将数据更改写入日志文件,并将其发送给备份数据库进行复制。而在半同步复制中,主数据库在提交事务之前会等待至少一个备份数据库确认已经接收到并写入了相同的数据。 半同步复制的工作原理如下: 1. 主数据库将数据更改写入日志文件,并将其发送给备份数据库。 2. 备份数据库接收到数据后,会向主数据库发送一个确认消息。 3. 主数据库在收到至少一个备份数据库的确认消息后,才会提交事务。 这种机制可以确保在主数据库发生故障时,备份数据库中已经复制的数据是完整和一致的。因此,半同步复制提供了更高的数据可靠性,减少了数据丢失的风险。 要启用pgsql半同步复制,需要进行以下配置: 1. 在主数据库和备份数据库的配置文件中,设置参数synchronous_commit为on。 2. 在主数据库的配置文件中,设置参数synchronous_standby_names为备份数据库的连接信息。 3. 在备份数据库的配置文件中,设置参数hot_standby为on。 需要注意的是,pgsql半同步复制会增加主数据库的写入延迟,因为主数据库需要等待至少一个备份数据库的确认消息。因此,在选择是否启用半同步复制时,需要权衡数据可靠性和性能之间的关系。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值