English 中文(简体)
如何计算 SQL 中的比例和为零的除法
原标题:How to calculate a ratio and cater for division by zero in SQL

报告应该每天显示P、U、D、F和M栏的总和,以及比例:M/P+U(以合计形式)。

我对比率有问题,我不知道如何在零点前满足分数。

TYP | TIMESTAMP               | P   | U   | D   | F  | M
------------------------------------------------------------
L   | 2012-04-27 15:47:02.000 | 0   | 949 | 0   | 0  | 949
L   | 2012-04-27 15:48:18.000 | 0   | 949 | 0   | 0  | 949
L   | 2012-04-30 17:15:01.000 | 0   | 0   | 4051| 0  | 0
L   | 2012-04-30 17:44:44.000 | 0   | 984 | 5   | 0  | 986
L   | 2012-05-02 11:12:01.000 | 2117| 0   | 0   | 0  | 0
L   | 2012-05-02 11:12:09.000 | 149 | 4   | 210 | 0  | 157
L   | 2012-05-02 11:12:11.000 | 77  | 0   | 30  | 0  | 43

我的查询:

SELECT
CONVERT(date,TIMESTAMP,112) As  DAY ,
SUM(P) As PAS,
SUM(U) As UFR,
SUM(D) As DES,
SUM(F) As FIR,
SUM(M) As MOL,
[M%] = ISNULL( (SUM(M) / NULLIF( SUM(P)+SUM(U), 0 ) )*100, 0),
FROM DATASET
GROUP BY CONVERT(date,TIMESTAMP,112) ORDER BY CONVERT(date,TIMESTAMP,112) DESC

最新:这是报告

 DAY         |  PAS   | UFR   | DES   | FIR   | MOL   | M%
----------------------------------------------------------------
2012-05-02   | 2343   |  4    |  240  | 0     |  200  | 0
2012-04-30   | 0      |  984  |  4056 | 0     |  986  | 100
2012-04-27   | 0      |  1898 |  0    | 0     |  1898 | 100
最佳回答

问题在于您不核算整数分割 。

Select Cast([Timestamp] As Date) As  Day 
  , Sum(P) As Pas
  , Sum(U) As Ufr
  , Sum(D) As Des
  , Sum(F) As Fir
  , Sum(M) As Mol
  , IsNull( (Sum(M) / NullIf( Sum(P) * 1.0000 + Sum(U), 0 ) ) *100, 0) As [M%]
From Dataset
Group By Cast([Timestamp] As Date)
Order By [Day] Desc

http://www.sqlfiddle.com/#! 3/891b0/10" rel=“nofollow” >SQL Fiddle 版本

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签