2011-04-09 48 views
5

的商和余数我有一个包含一个employee表:如何获得师

emp id  Sum 
------  --- 
1   7 
2   6 

我想要一个SQL查询将在Sum与8

+0

这听起来像功课选择地板(列/除数)... – 2011-04-09 06:25:53

+4

这仍然听起来像一个有效的问题。 – 2011-04-09 07:36:38

回答

1

得到商和余数你可以使用%运算符来获得余数。这是一个例子。

SELECT Round(17/4) -- quotient without decimal 
SELECT 17 % 4 -- remainder 
2

你的qoutient的返回类型是什么?如果你不关心它是一个浮点还是一个整数(整数)。你可以试试这个。

SELECT 
     (sum/8) AS qoutient, 
     (sum % 8) AS reminder 
    FROM employee 
10

使用integer divisionmod运营商获得商和余数:

SELECT 
    emp_id, 
    sum, 
    sum/8 AS Result, 
    sum div 8 AS Quotient, 
    sum mod 8 AS Remainder 
FROM employee 
emp_id sum Result Quotient Remainder 
1  7 0.8750 0   7 
2  6 0.7500 0   6 
3  9 1.1250 1   1 
4  10 1.2500 1   2 
5  11 1.3750 1   3 
6  12 1.5000 1   4 
7  13 1.6250 1   5 
8  14 1.7500 1   6 
9  15 1.8750 1   7 
10  16 2.0000 2   0 
0

使用地板的功能,从双