posgre

 
select STRING_AGG(schooltypeno,',') from dm.codeschooltype where schooltypeno like '36%'

361,367,369,363,365,364,362,366

 

 

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

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
posted @ 2024-03-04 11:01  花生与酒  阅读(3)  评论(0编辑  收藏  举报