SQL 语句 加减乘除法 表达式 怎么写?

SQL 语句 加减乘除法 表达式 怎么写?,第1张

SELECT distinct a购房人,a房间号,(a总房价 -

(SELECT SUM( b已收款金额 )

FROM table1 a, table2 b

WHERE a房间号 = b房间号

))

FROM table1 a,table2 b where

a房间号 = b房间号

或者按楼主的方法

SELECT distinct a购房人,a房间号,(a总房价 - (

SELECT SUM( b已收款金额 )

FROM table1 a, table2 b

WHERE a房间号 = b房间号

) )

FROM table1 a

LEFT JOIN table2 b ON

a房间号 = b房间号

不能用group by 语句,不过还是不太好

楼主写的不对,首先bSUM(已收款金额) 不能这么写,sum(b已收款金额)还行。这种left join groupby 也不能这么用。

在Oracle里面加减乘除是可以直接用+-/的,也就是说"a总房价"后面可以直接跟运算符

用ROUND函数,ROUND(COL00708,0)这个是四舍五入进位的

如果是向下取整,用floor()

update zzw set col007 = round(col00708,0) ------这个是四舍五入取整

update zzw set col007 = floor(col00708) ------这个是向下取整

select POWER(exp(sum(ln(ta))),1/count()) from 

(select 5 a from dual

union

select 8 a from dual

union

select 2 a from dual

union

select 3 a from dual) t

select case when sum(case when price =0 then 1 else 0 end ) >0 then 0 else

(case when sum(case when price<0 then 1 else 0 end )%2=0 then exp(sum(log(price)))

else -1exp(sum(log(abs(price)))) end) end from aaa_bbb where id in (xx,xx,xx)

sqlserver:

select abcisnull(d,1) from table

null跟任何值计算都是null,isnull函数判断d列若是null就改为1

可以

方法一

sqlserver

select a/b,a+b,ab

数字类型的计算,可以直接这样写的

方法二

select aa/bb,aa+bb

from a left join b on ac=bc

Transact-SQL介绍

Transact-SQL语言是用在微软的SQL Server 的关系型数据库管理系统中编程语言。

主要有六大类:

算术运算符、赋值运算符、位运算符、比较运算符、逻辑运算符和字符串联运算符。

算术运算符包括(+)、减(-)、乘()、除(/)和取模(%)

赋值运算符"="

位运算符 " & ^ |"

比较运算符 =、>、<、>=、<=、<>、!=、!>、!<

逻辑运算符 AND、OR、NOT

字符串联运算符 +

sql中提供的聚合函数,通常包括sum,count,avg,max,min,但不包括乘法;

比如有这样一个数据集:

id x

1 1

1 2

1 3

2 4

2 5

2 6

;

想实现成如下形式

id prob_x

1 6

2 120

即让第一组中123=6

第二组中456=120

用data步中first+retain可以实现,但data需要先排序;

而sql的聚合函数中也没有相乘这一函数,于是有些牛人想到通过数学转化,将乘法转为加法,而sql中可以

用sum实现加法聚合。

说来也简单,主要想法就是AB=exp(lnAB)=exp(lnA+lnB);

select id,exp(sum(log(x))) as prob_x from data_set group by id;

=============================================

但我觉得也需要补充一点东西:

首先有0的情况下,log函数的定义域为{x>0},有0出现的时候,改组最后结果肯定为0;

其次是有负数的情况,需要对绝对值进行计算,计算每组负数的个数,如果负数是偶数个,那么结果为正,

如果为奇数个,则需要在结果上添上负号。

最后可以写成这样

select id,(case when sum(case when x=0 then 1 else 0 end) >0 then 0

when mod(sum(case when x<0 then 1 else 0 end),2)=0 then exp(sum(log(x)))

else -1exp(sum(log(abs(x)))) end) as prod_x from data_set group by id;

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/13496572.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-09-01

发表评论

登录后才能评论

评论列表(0条)

    保存