SQL-基本语法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CSDN_FlyYoung/article/details/88708970

INSERT INTO SELECT & SELECT INTO FROM

INSERT INTO SELECT

insert into risk_warning_indicator_hourly_report
select id, risk_indicator_id, hour, value, total, (
		case 
			when last_total=0 then 0 
			else (new_total::numeric - last_total::numeric) / last_total::numeric 
		end
	) as ratio_inc, now()
from (
    select id, risk_indicator_id, hour, value, total, 
        row_number() over (partition by value order by hour) rn,
        nth_value(total, 1) over (partition by value order by hour) last_total,
        nth_value(total, 2) over (partition by value order by hour) new_total
    from risk_indicator_hourly_report
    where (hour = '2019-03-25 16:00:00' or hour = '2019-03-26 16:00:00')
) as tab 
where tab.rn = 2;

SQL INSERT INTO SELECT 语句
SELECT INTO 和 INSERT INTO SELECT 两种表复制语句

case when

-- 查询每个时间段的数据
select risk_indicator_id, hour,
	SUM(case when total > 0 and total <= 5 then 1 else 0 end) as one_to_five,
	SUM(case when total > 5 and total <= 10 then 1 else 0 end) as five_to_ten,
	SUM(case when total > 10 and total <= 15 then 1 else 0 end) as tenToFifteen,
	SUM(case when total > 15 and total <= 20 then 1 else 0 end) as fifteenToTwenty,
	SUM(case when total > 20 and total <= 25 then 1 else 0 end) as twentyToTwentyFive,
	SUM(case when total > 25 then 1 else 0 end) as twentyFivePlus
from risk_indicator_hourly_report
group by hour, risk_indicator_id

在这里插入图片描述
SQL的case when then else end语句的用法

分页

MySQL

select *
from table
offset (pageNo - 1) * pageSize
limit pageSize

drop

SQL 撤销索引、表以及数据库

insert

insert into risk_warning_indicator(name) values('ratio_inc');

update

update risk_warning_rule 
set risk_name='test_update_6', risk_type=2, risk_level=2, expressions='ratio_inc>1', whether_to_alert=true, 
	alert_freq=1, alert_emails='[email protected]', alert_email_subject='update_email1', update_time=now()
where id = 6
returning id 

SQL UPDATE 语句

猜你喜欢

转载自blog.csdn.net/CSDN_FlyYoung/article/details/88708970