「postgresql入門」CASE式で条件分岐を判定するサンプル

1.フラグを付ける
形式
select case when 条件 then xxx
as フラグ

SELECT
contentid,
detail,
reg_date,
CASE WHEN reg_date > CURRENT_TIMESTAMP() – INTERVAL 7 DAY THEN 1 ELSE 0 END AS newInfo
FROM categorytb;
ORDER BY reg_date DESC

2.case when else

SELECT
CASE
WHEN rankscore >= 70 THEN 'S’
WHEN rankscore >= 60 THEN 'A’
WHEN rankscore >= 50 THEN 'B’
ELSE 'C’
END
FROM demotbl;

DataBase

Posted by arkgame