尝试返回带两位小数和逗号的值
Posted
技术标签:
【中文标题】尝试返回带两位小数和逗号的值【英文标题】:Trying to return values with two decimal places and comma 【发布时间】:2021-08-19 15:47:31 【问题描述】:我尝试使用 TRIM(VARCHAR_FORMAT( value ), '9,999,990.99') 但失败了...有什么简单的方法可以将结果转换为更易读的格式吗?我希望检索小数点后两位并使用适当的 0,000 分隔结果
非常感谢您阅读本文。 YELLOW 中的值我想更好地格式化...
WITH X AS
(
SELECT
1 TYPE, I.NAME COMPANY, C.CLIENT_ID, C.NAME CLIENT_NAME, T.BILL_NUMBER CURRENT_FB, T.WEIGHT CURRENT_WEIGHT,
TRIM(VARCHAR_FORMAT((T.CHARGES + T.XCHARGES) * CASE WHEN T.TOTAL_CHARGES = 0 THEN 0 ELSE (T.FUNCTIONAL_AMT / T.TOTAL_CHARGES) END), '9,999,990.99') as CURRENT_REVENUE,
NULL PREVIOUS_FB, NULL PREVIOUS_WEIGHT, NULL PREVIOUS_REVENUE
FROM TLORDER T
INNER JOIN CLIENT C ON C.CLIENT_ID = T.BILL_TO_CODE
INNER JOIN COMPANY_INFO_SRC I ON I.COMPANY_INFO_ID = T.COMPANY_ID
WHERE 1 = 1
AND COALESCE(T.INTERFACE_STATUS_F,0) <> -1
AND T.EXTRA_STOPS <> 'Child'
AND T.BILL_NUMBER NOT IN ('0', 'NA')
AND DATE(T.PICK_UP_BY) BETWEEN CURRENT TIMESTAMP - 90 days AND CURRENT TIMESTAMP -60 days
UNION ALL
SELECT
2 TYPE, I.NAME COMPANY, C.CLIENT_ID, C.NAME CLIENT_NAME, NULL CURRENT_FB, NULL CURRENT_WEIGHT, NULL CURRENT_REVENUE, T.BILL_NUMBER PREVIOUS_FB, T.WEIGHT PREVIOUS_WEIGHT,
TRIM(VARCHAR_FORMAT((T.CHARGES + T.XCHARGES) * CASE WHEN T.TOTAL_CHARGES = 0 THEN 0 ELSE (T.FUNCTIONAL_AMT / T.TOTAL_CHARGES) END), '9,999,990.99') AS PREVIOUS_REVENUE
FROM TLORDER T
INNER JOIN CLIENT C ON C.CLIENT_ID = T.BILL_TO_CODE
INNER JOIN COMPANY_INFO_SRC I ON I.COMPANY_INFO_ID = T.COMPANY_ID
WHERE 1 = 1
AND COALESCE(T.INTERFACE_STATUS_F,0) <> -1
AND T.EXTRA_STOPS <> 'Child'
AND T.BILL_NUMBER NOT IN ('0', 'NA')
AND DATE(T.PICK_UP_BY) BETWEEN CURRENT TIMESTAMP - 60 days AND CURRENT TIMESTAMP
)
SELECT
1 SORT, COMPANY, CLIENT_ID, CLIENT_NAME,
COUNT(DISTINCT CURRENT_FB) CURRENT_FB,
COUNT(DISTINCT PREVIOUS_FB) PREVIOUS_FB,
ROUND(CASE WHEN COUNT(PREVIOUS_FB) <> 0 THEN (COUNT(CURRENT_FB) - COUNT(PREVIOUS_FB)) / CAST(COUNT(PREVIOUS_FB) AS DOUBLE) * 100.00 ELSE 0 END, 2) FB_DIFF,
SUM(CURRENT_WEIGHT) CURRENT_WEIGHT,
SUM(PREVIOUS_WEIGHT) PREVIOUS_WEIGHT,
ROUND(CASE WHEN SUM(PREVIOUS_WEIGHT) <> 0 THEN (SUM(CURRENT_WEIGHT) - SUM(PREVIOUS_WEIGHT)) / SUM(PREVIOUS_WEIGHT) * 100.00 ELSE 0 END, 2) WEIGHT_DIFF,
TRIM(VARCHAR_FORMAT(SUM(CURRENT_REVENUE),'9,999,990.99')) as CURRENT_REVENUE,
TRIM(VARCHAR_FORMAT(SUM(PREVIOUS_REVENUE),'9,999,990.99')) as PREVIOUS_REVENUE,
ROUND(CASE WHEN SUM(PREVIOUS_REVENUE) <> 0 THEN (SUM(CURRENT_REVENUE) - SUM(PREVIOUS_REVENUE)) / SUM(PREVIOUS_REVENUE) * 100.00 ELSE 0 END, 2) REVENUE_DIFF
FROM X
GROUP BY COMPANY, CLIENT_ID, CLIENT_NAME
UNION ALL
SELECT
2 SORT, COMPANY, 'TOTAL' CLIENT_ID, NULL CLIENT_NAME,
COUNT(DISTINCT CURRENT_FB) CURRENT_FB,
COUNT(DISTINCT PREVIOUS_FB) PREVIOUS_FB,
ROUND(CASE WHEN COUNT(PREVIOUS_FB) <> 0 THEN (COUNT(CURRENT_FB) - COUNT(PREVIOUS_FB)) / CAST(COUNT(PREVIOUS_FB) AS DOUBLE) * 100.00 ELSE 0 END, 2) FB_DIFF,
SUM(CURRENT_WEIGHT) CURRENT_WEIGHT,
SUM(PREVIOUS_WEIGHT) PREVIOUS_WEIGHT,
ROUND(CASE WHEN SUM(PREVIOUS_WEIGHT) <> 0 THEN (SUM(CURRENT_WEIGHT) - SUM(PREVIOUS_WEIGHT)) / SUM(PREVIOUS_WEIGHT) * 100.00 ELSE 0 END, 2) WEIGHT_DIFF,
TRIM(VARCHAR_FORMAT(SUM(CURRENT_REVENUE),'9,999,990.99')) AS CURRENT_REVENUE,
TRIM(VARCHAR_FORMAT(SUM(PREVIOUS_REVENUE),'9,999,990.99')) AS PREVIOUS REVENUE,
ROUND(CASE WHEN SUM(PREVIOUS_REVENUE) <> 0 THEN (SUM(CURRENT_REVENUE) - SUM(PREVIOUS_REVENUE)) / SUM(PREVIOUS_REVENUE) * 100.00 ELSE 0 END, 2) REVENUE_DIFF
FROM X
GROUP BY COMPANY
【问题讨论】:
请告诉它是如何失败的,这并不明显。您不需要在 X 中进行格式化,因为您对之后的值求和。一些右括号在 X 中放错了位置,最后一个AS PREVIOUS REVENUE
应该是 `AS PREVIOUS_REVENUE`
感谢您的快速回复。我收到以下错误:IBMCLI Drivber][DB2/NT64 SQL0104N An unexpected token "," was found after .TOTAL_CHARGES) END)" 预期的令牌可能包括“FROM”SQLSTATE=42601
用END, '9,999,990.99'))
替换所有END), '9,999,990.99')
你太棒了!我进行了更改 - 我现在得到一个 [Field 'REVENUE _DIFF' is of an unsupported type] 这很奇怪,因为它以前工作过。真是太感谢你了!!
【参考方案1】:
您的问题似乎与查询中的某些语法错误有关,而不是与格式有关,这很好用。例如:
select varchar_format(2696067.32697366,'9,999,990.99')
from sysibm.sysdummy1;
结果:
1
------------
2,696,067.33
请参阅db<>fiddle 的运行示例。
【讨论】:
以上是关于尝试返回带两位小数和逗号的值的主要内容,如果未能解决你的问题,请参考以下文章
JAVA编程:输出值要求格式化为:以逗号作为每千位的间隔符,并且小数点后四舍五入为两位。例如:123.45
DB2中如果转换数值显示为两位小数,并显示千分位逗号要怎么写SQL?