1. sqlServer中SQL语句如何对两个group By后的查询结果进行相除
你错误的问题点在于两个子查询之间没有任何关联,不能直接除。首先我们要建立关联关系。再除
select a.值1,(case when isnull(b.值2,0)=0 then 0 else a.值1/b.值2) ---记得除数为0处理
from (select 值2,SUM(值1) AS 值1 from 表1 group by 值2) a
innor join (看情况使用innor join 还是 full 还是Left)
(select 值2,sum(值1) AS 值1 from 表2 group by 值2) b on A.值2=b.值2
2. SQL 语句删除问题同时删除两个表内关联的数据
一个sql语句是没办法执行两个删除操作,如果你要实现上面的功能,有以下几个选择:
1.用外键关联删除,把B表的uid设成外键关联A表的ID,并关联删除操作
2.用存储过程,用事务来处理实现;
望采纳!
3. sql server 2个查询结果相除
declare @a float
declare @b float
select @a= count(*) from table1
select @b = count(*) from table2
select @a/@b
楼上的方法,如果只是COUNT条数,没有小数点啊。。。 要转格式:
select a.a/b.b from
(select cast(count(*)as float) as a from table1) as a,
(select cast(count(*)as float) as b from table2) as b
4. SQL如何做除法
SQL做除法的步骤:
select
t.[origin-destination],t.[SH/LANE/MOT] /(select count(1) from ['TMS$'])ASPERCENTAGEFROM (代码1) t
group by [origin-destination],t.[SH/LANE/MOT]
having t.[SH/LANE/MOT] /count(*) <= 0.01
注:两个count都是int,相除会没有小数部分,所以应该都给转成带小数的数。
cast as numeric(10,4) 。
(4)sql把两个不同表的数据相除扩展阅读
例题,表明为chuqinqk 列名 迟到 1(次)一个月的总天数为30 求迟到率。
select * from table1 where 工资>2500 and 工资<3000 //同上
select 姓名 from table1 where 性别='0' and 工资='4000'
select * from table1 where not 工资= 3200
select * from table1 order by 工资desc //将工资按照降序排列
select * from table1 order by 工资 asc //将工资按照升序排列
select * from table1 where year(出身日期)=1987 //查询table1 中所有出身在1987的人select * from table1 where name like
'%张' /'%张%' /'张%' //查询1,首位字‘张’3,尾位字‘张’2,模糊查询
select * from table1 order by money desc //查询表1按照工资的降序排列表1 (升序为asc)
select * from table1 where brithday is null //查询表1 中出身日期为空的人
SQL语言,是结构化查询语言(StructuredQueryLanguage)的简称。
5. sql 两个表记录数相除
可以试下如下方法:
select table1.count(*)*1.00/table2.count(*) as 百分比
from table1, table2
另处,你的方法在sql server中可以的话,一般来说在access中也可用,两种数据库都是完全遵守SQL语法标准的。只是一些规则存储过程等无法移植。
6. SQL如何做除法
这样:
select
t.[origin-destination],t.[SH/LANE/MOT] /(select count(1) from ['TMS$'] )ASPERCENTAGE
FROM (代码1) t
group by [origin-destination],t.[SH/LANE/MOT]
having t.[SH/LANE/MOT] /count(*) <= 0.01
注:两个count都是int,相除会没有小数部分,所以应该都给转成带小数的数。
cast as numeric(10,4) 。
(6)sql把两个不同表的数据相除扩展阅读:
SQL中除法运算的实现
R(X,Y)÷S(Y,Z)的运算用结构化语言SQL 语句可表达为下列形式:
select distinct R.X from R R1
where not exists
(
select S.Y from S
where not exists
(
select * from R R2
where R2.X=R1.X and R2.Y=S.Y
)
)
7. sql语句的写法——把两个数相除(Oracle)
select (select count(*) as a from...)/(select count(*) as b from...
) from al
8. sql统计两个表数据然后相除,怎么写
要相除的是什么?某个字段的统计还是数据量统计还是其他的?
selectt1.a/t2.bfrom(
(selectcount(1)afromtable1)t1,
(selectcount(1)bfromtable2)t2
);