DB2分页语句,DB2字符型数字列排序

1.DB2分页语句(第3到第6条记录):
select * from
(select row_number() over() as num,id ,name from bas_user order by id) bas_user
where bas_user.num >= 3 and bas_user.num <= 6;

 

 

2.按字符型数字列排序:
  SELECT
   volt_id  AS  voltID,
   station_id  AS stationID,
     volt_name   AS voltName,
   vol_type AS volType,
   is_compute_balance AS isComputeBalance 
  FROM
   pgma.m_volt_level  
  WHERE STATION_ID=652  
  ORDER BY DEC(substr(volt_name,1,posstr(volt_name,'kV')-1)) DESC;

 

 

3.聚合函数,日期相减

select second(timestamp(c.t))-second(timestamp(g.t)) time,
sum(CASE WHEN a.SEQ='A' THEN c.VALUE END) A,
sum(CASE WHEN a.SEQ='B' THEN c.VALUE END) B,
sum(CASE WHEN a.SEQ='C' THEN c.VALUE END) C
from DAT_RMSID A
inner join DAT_RMS c on a.oid=c.RMSID
inner join (select min(e.t) t
            from DAT_RMSID d,DAT_RMS e
   where d.oid=e.RMSID
      and exists (select 1 from DAT_RMSID f where oid=5 and d.chv_oid = e.chv_oid and d.starttime=f.starttime)) g on(1 = 1)
where
exists (select 1 from DAT_RMSID B where oid=5 and a.chv_oid = b.chv_oid and a.starttime=b.starttime)
group by second(timestamp(c.t))-second(timestamp(g.t));

 

4.两个数求和,其中一个数为null结果就为null, 解决这种问题可以用以下函数(sql,db2,oracle通用):
COALESCE(a.comvalue,0)

 

5.查询前n条记录:
Select * FROM tbi_ettproj FETCH FIRST 100 ROWS ONLY;

 

你可能感兴趣的:(JOIN,oracle,sql,user,db2,null)