- SUM(ROUND(case when S1.SAL_NOS_PRM_AMT<=0 then S1.SAL_AMT
- when S1.SAL_AMT<=0 then 0
- else S1.SAL_AMT / S1.SAL_NOS_PRM_AMT
- end)
- ) ADD_COUNT,
ROUND() 函数
ROUND 函数用于把数值字段舍入为指定的小数位数。
SQL ROUND() 语法
SELECT ROUND(column_name,decimals) FROM table_name
参数 | 描述 |
---|---|
column_name | 必需。要舍入的字段。 |
decimals | 必需。规定要返回的小数位数。 |
SQL ROUND() 实例
我们拥有下面这个 "Products" 表:
Prod_Id | ProductName | Unit | UnitPrice |
---|---|---|---|
1 | gold | 1000 g | 32.35 |
2 | silver | 1000 g | 11.56 |
3 | copper | 1000 g | 6.85 |
现在,我们希望把名称和价格舍入为最接近的整数。
我们使用如下 SQL 语句:
SELECT ProductName, ROUND(UnitPrice,0) as UnitPrice FROM Products
结果集类似这样:
ProductName | UnitPrice |
---|---|
gold | 32 |
silver | 12 |
copper | 7 |
——————————————————————————————————————————————————————————————————————
sql语言中有没有类似C语言中的switch case的语句??
没有,用case when 来代替就行了.
例如,下面的语句显示中文年月
select getdate() as 日期,case month(getdate())
when 11 then '十一'
when 12 then '十二'
else substring('一二三四五六七八九十', month(getdate()),1)
end+'月' as 月份
=================================================
CASE 可能是 SQL 中被误用最多的关键字之一。虽然你可能以前用过这个关键字来创建字段,但是它还具有更多用法。例如,你可以在 WHERE 子句中使用CASE。
首先让我们看一下 CASE 的语法。在一般的 SELECT 中,其语法如下:
SELECT <myColumnSpec> =
CASE
WHEN <A> THEN <somethingA>
WHEN <B> THEN <somethingB>
ELSE <somethingE>
END
在上面的代码中需要用具体的参数代替尖括号中的内容。下面是一个简单的例子:
USE pubs
GO
SELECT
Title,
'Price Range' =
CASE
WHEN price IS NULL THEN 'Unpriced'
WHEN price < 10 THEN 'Bargain'
WHEN price BETWEEN 10 and 20 THEN 'Average'
ELSE 'Gift to impress relatives'
END
FROM titles
ORDER BY price
GO
这是 CASE 的典型用法,但是使用 CASE 其实可以做更多的事情。比方说下面的 GROUP BY 子句中的 CASE:
SELECT 'Number of Titles', Count(*)
FROM titles
GROUP BY
CASE
WHEN price IS NULL THEN 'Unpriced'
WHEN price < 10 THEN 'Bargain'
WHEN price BETWEEN 10 and 20 THEN 'Average'
ELSE 'Gift to impress relatives'
END
GO
你甚至还可以组合这些选项,添加一个 ORDER BY 子句,如下所示:
USE pubs
GO
SELECT
CASE
WHEN price IS NULL THEN 'Unpriced'
WHEN price < 10 THEN 'Bargain'
WHEN price BETWEEN 10 and 20 THEN 'Average'
ELSE 'Gift to impress relatives'
END AS Range,
Title
FROM titles
GROUP BY
CASE
WHEN price IS NULL THEN 'Unpriced'
WHEN price < 10 THEN 'Bargain'
WHEN price BETWEEN 10 and 20 THEN 'Average'
ELSE 'Gift to impress relatives'
END,
Title
ORDER BY
CASE
WHEN price IS NULL THEN 'Unpriced'
WHEN price < 10 THEN 'Bargain'
WHEN price BETWEEN 10 and 20 THEN 'Average'
ELSE 'Gift to impress relatives'
END,
Title
GO
注意,为了在 GROUP BY 块中使用 CASE,查询语句需要在 GROUP BY 块中重复 SELECT 块中的 CASE 块。
除了选择自定义字段之外,在很多情况下 CASE 都非常有用。再深入一步,你还可以得到你以前认为不可能得到的分组排序结果集。
复杂SQL求和、除法为0判断、返回2位小数例子:
- select t1.*,t2.sum_jh_m,ROUND((case when t1.rcydl_jh_m = 0 then 0 else t1.rcydl_m / rcydl_jh_m end),2)*100 wcl_m,(t1.rcydl_y - t2.sum_jh_m) cqc,ROUND(case when t2.sum_jh_m = 0 then 0 else t1.rcydl_y / t2.sum_jh_m end)*100 wcl_m2,ROUND(case when t1.rcydl_jh_y = 0 then 0 else t1.rcydl_y / t1.rcydl_jh_y end)*100 wcl_y from
- (select t.qybl,t.rcydl_m,j.rcydl_jh_m,t.rcydl_y,t.rcydl_jh_y,p.projectid,p.projectname from TJ_KF_CLHZ t,tj_kf_clhz_jh j,sys_project p where t.sjlx = 3 and t.sjlx = j.sjlx and j.ny = '201306' and t.projectid = j.projectid and t.projectid = p.projectid and RQ = (select max(RQ) from TJ_KF_CLHZ where sjlx = t.sjlx and projectid = t.projectid and to_char(RQ,'yyyymm') = '201306') order by p.xh) t1
- left join
- (select j.projectid,sum(j.rcydl_jh_m) sum_jh_m from tj_kf_clhz_jh j where sjlx = 3 and ny <= 201306 and ny >= 201301 group by projectid) t2
- on t1.projectid = t2.projectid
本文来自:http://blog.csdn.net/hwt0101/article/details/9900541
相关网址:
http://www.cnblogs.com/qiantuwuliang/archive/2009/06/03/1495770.html