sql statement series (advanced computing) [chapter eight hundred of Chapter XVI]

Foreword

Introduces two practical sql query.
1. Calculate the average time, maximum and minimum removal.
2. Modify the accumulated value.

Calculate the mean time, the removal of the maximum and minimum

sql server:

select AVG(sal) from(
select sal,MIN(SAL) over() min_sal,MAX(SAL) max_sal 
from EMP) x
where sal not in(
min_sal,max_sal
)

mysql:

select AVG(sal) 
from EMP
where SAL not in(
(select MIN(sal) from EMP),
(select MAX(sal) from EMP)
)

Modify the cumulative value

Suppose a table:
the SELECT * from Xinyong

To achieve the effect is as follows:

It is when the TRX is pr add, subtraction py at the time.

answer:

select case when  v.TRX='py' then 'PAYMENT' else 'PURCHASE' end as trx_type,v.AMT,
(select sum(case when x.TRX='py' then -x.AMT else x.AMT end)
from xinyong x
where v.ID>=x.ID
) as balance
from xinyong v

Guess you like

Origin www.cnblogs.com/aoximin/p/12617111.html