如何在Microsoft SQL Server中划分两个表达式

时间:2017-03-29 15:21:41

标签: sql

如何在Microsoft SQL Server中划分两个表达数字作为结果的表达式?我尝试用/,但这没有用

SELECT COUNT(*) FROM A 
SELECT COUNT(*) FROM B WHERE c = 1

我试过/但这没有用

SELECT COUNT(*) 
FROM A/SELECT COUNT(*) FROM B WHERE c = 1

2 个答案:

答案 0 :(得分:0)

它可能依赖于RDBMS,这适用于sql server。

SELECT ACounts/ CAST(BCounts AS Decimal(29,20))
FROM (SELECT COUNT(*) AS ACounts FROM A) AS suba
, (SELECT COUNT(*) AS BCounts FROM B WHERE c = 1) AS subb

请注意,我将BCounts转换为任意大小的小数,否则它将舍入到最接近的整数。如果那是您想要的,请忽略该部分。

这也有效

SELECT (SELECT COUNT(*) AS ACounts FROM A)/ CAST((SELECT COUNT(*) AS BCounts FROM B WHERE c = 1) AS Decimal(29,20))

答案 1 :(得分:0)

使用子查询:

SELECT (a.countA / b.countB) As divisionResult 
FROM 
    (SELECT COUNT(*) AS countA FROM A) a,
    (SELECT COUNT(*) AS countB FROM B WHERE c = 1) b
;