首页 > 其他分享 >posgre

posgre

时间:2024-03-04 11:13:07浏览次数:31  
标签:case CASE column WHEN table balance posgre

==================

select 
    tablename,
    case when substring(tablename,5,1)= 'f' then 'a' -- (1)
    when substring(tablename,5,1)= 'p' then 'b'      -- (2)
    else 'c' end as flg                              -- (3)
from pg_tables
where schemaname != 'pg_catalog' order by tablename;

==========================
https://www.postgresql.org/docs/current/functions-conditional.html
SELECT * FROM test;

 a
---
 1
 2
 3

SELECT a,
       CASE WHEN a=1 THEN 'one'
            WHEN a=2 THEN 'two'
            ELSE 'other'
       END
    FROM test;

 a | case
---+-------
 1 | one
 2 | two
 3 | other
 ============================
https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-case/
SELECT 
  SUM (
    CASE WHEN rental_rate = 0.99 THEN 1 ELSE 0 END
  ) AS "Economy", 
  SUM (
    CASE WHEN rental_rate = 2.99 THEN 1 ELSE 0 END
  ) AS "Mass", 
  SUM (
    CASE WHEN rental_rate = 4.99 THEN 1 ELSE 0 END
  ) AS "Premium" 
FROM 
  film;
Code language: SQL (Structured Query Language) (sql)

The result of the query is as follows:

 Economy | Mass | Premium
---------+------+---------
     341 |  323 |     336
(1 row)
====================================

 Update different column on condition using CASE statement

 
update table_A
set column_A = case when     (column_A>table_B.balance) then value else column_A end,
    column_B = case when not (column_A>table_B.balance) 
                     and     (column_B>table_B.balance) then value else column_B end,
    column_C = case when not (column_A>table_B.balance) 
                     and not (column_B>table_B.balance)
                     and     (column_C>table_B.balance) then value else column_C end
from table_B
on table_A.Id=table_B.id

标签:case,CASE,column,WHEN,table,balance,posgre
From: https://www.cnblogs.com/lxgbky/p/18051406

相关文章

  • centos7安装posgresql
    安装#安装源sudoyuminstall-yhttps://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm#安装PostgreSQLcentos7上安装更高版本缺依赖,比较麻烦sudoyuminstall-ypostgresql14-server#初始化数据库sudo/usr/......
  • 关于posgresql
    posgresql介绍posgresql与posgres是一种数据库没有区别。PostgreSQL是以加州大学伯克利分校计算机系开发的POSTGRES,是一个功能强大的开源对象关系数据库管理系统(ORDB......