我有一个数据库,可以根据标签确定不同的值。标签确定其是否为豁免价值的地方。例如,2 =非豁免,3 =豁免。如果我运行查询,我的结果看起来像这样
|Name |EXEMPT VALUE|NON EXEMPT VALUE|Total Value|
|X |100.00 |200.00 |300.00 |
|Y |0.00 |4.50 |4.50 |
Y的豁免值实际上是一个小于一的数字,0.30。 我没有创建数据库,所以我无法控制值的数据类型。数据类型是数字,这是我的查询返回值为0.00的原因吗?我应该将数字转换为另一种数据类型以获得正确的结果。我的查询如下。
SELECT
NAME
,SUM(CASE WHEN EXEMPTIONSTATUS = 2 THEN TOTAL_VALUE ELSE 0 END) AS 'NON EXEMPT VALUE'
,SUM(CASE WHEN EXEMPTIONSTATUS = 3 THEN TOTAL_VALUE ELSE 0 END) AS 'EXEMPT VALUE'
SUM(TOTAL_VALUE) AS 'TOTAL VALUE'
FROM ORDER_ACCOUNT JOIN ACCOUNT_INVOICE
WHERE ORDER_ACCOUNT.DATE BETWEEN 'M/D/YEAR' AND 'M/D/YEAR'
GROUP BY NAME
ORDER BY NAME ASC
答案 0 :(得分:0)
尝试使用* 1.0
和ELSE 0.0
并在每个VALUE
SUM
SELECT
NAME,
SUM(CASE WHEN EXEMPTIONSTATUS = 2 THEN TOTAL_VALUE * 1.0
ELSE 0.0 END) AS 'NON EXEMPT VALUE',
SUM(CASE WHEN EXEMPTIONSTATUS = 3 THEN TOTAL_VALUE * 1.0
ELSE 0.0 END) AS 'EXEMPT VALUE',
SUM(TOTAL_VALUE) AS 'TOTAL VALUE'
FROM ORDER_ACCOUNT JOIN ACCOUNT_INVOICE
WHERE ORDER_ACCOUNT.DATE BETWEEN 'M/D/YEAR' AND 'M/D/YEAR'
GROUP BY NAME
ORDER BY NAME ASC