我有两个问题,我不知道如何做一个简单的数学运算。
SELECT count(*) FROM CUSTOMERINFO
where member between '2013-01-01' and '2013-12-31'
返回:7823
select COUNT(DISTINCT ORDERINFO.OrderID) from orderinfo, orderiteminfo
where code = '810samp'
AND ORDERINFO.OrderID = ORDERITEMINFO.OrderID
AND orderinfo.PrepareDate between '2013-01-01' and '2013-12-31'
返回:4106
我需要返回查询:4106/7823 X 100
答案 0 :(得分:1)
没有经过测试的代码,但这应该适合你:
SELECT (SELECT count(*) FROM CUSTOMERINFO
where member between '2013-01-01' and '2013-12-31') /
(select COUNT(DISTINCT ORDERINFO.OrderID) from orderinfo, orderiteminfo
where code = '810samp'
AND ORDERINFO.OrderID = ORDERITEMINFO.OrderID
AND orderinfo.PrepareDate between '2013-01-01' and '2013-12-31')
* 100
Alternate将是
DECLARE @int1 as int
DECLARE @int2 as int
SET @int1 = (SELECT count(*) FROM CUSTOMERINFO
where member between '2013-01-01' and '2013-12-31')
SET @int2 = (select COUNT(DISTINCT ORDERINFO.OrderID) from orderinfo, orderiteminfo
where code = '810samp'
AND ORDERINFO.OrderID = ORDERITEMINFO.OrderID
AND orderinfo.PrepareDate between '2013-01-01' and '2013-12-31')
SELECT @int1/@int2 X 100
答案 1 :(得分:1)
只需使用以下语法:
SELECT (
CAST((<your first query>) as decimal)
/(<your second query>)
* 100
) AS Percents;
所以你的查询应该是
SELECT (
CAST((select COUNT(DISTINCT ORDERINFO.OrderID) from orderinfo, orderiteminfo
where code = '810samp'
AND ORDERINFO.OrderID = ORDERITEMINFO.OrderID
AND orderinfo.PrepareDate between '2013-01-01' and '2013-12-31') as decimal)
/(SELECT count(*) FROM CUSTOMERINFO
where member between '2013-01-01' and '2013-12-31')
* 100
) AS Percents;