sql - SQL 查询输出 - 小数位,整数太多
问题描述
我有一个正常运行的查询,我正在合并两个表,删除重复项并将数据保留为最近的生效日期。
表格如下所示:
1.TAX_TABLE---------
tax_id description
1 AZ State
2 AZ-Maricopa Co
4 AZ-Maricopa/Mesa
2.Tax_RATE_TABLE-------
tax_id effective_date tax_percent
1 2015-01-01 00:00:00.000 5.6
2 2015-01-01 00:00:00.000 0.7
4 2015-01-01 00:00:00.000 1.75
4 2019-03-01 00:00:00.000 2
我当前的查询如下所示:
use lhsitedb
select t.tax_id, t.description, tr.effective_date, tr.tax_percent
from dbo.tax t
inner join dbo.tax_rate tr on tr.tax_id = t.tax_id
where tr.effective_date = (
select max(tr1.effective_date)
from dbo.tax_rate tr1
where tr1.tax_id = tr.tax_id
我的输出就是这样,我在 tax_tax 百分比列中得到小数位数。我一直在尝试对该列进行限制,以仅显示可以扩展到百分之一的十进制数字。
注意:此查询在 SSMS 中执行时工作得很好,但是,我正在使用sqlcmd
该文件并将其导出到一个.txt
文件中,该文件是产生这些意外结果的地方。
tax_id description effective_date tax_percent
--------------------- ------------------------------- ----------------------- ------------------------
4 AZ-Maricopa/Mesa 2019-03-01 00:00:00.000 2
2 AZ-Maricopa Co 2015-01-01 00:00:00.000 0.69999999999999996
1 AZ State 2015-01-01 00:00:00.000 5.5999999999999996
(17 rows affected)
当该列时,应该更多地沿着这些方向查看:
tax_percent
----------------------------
2
1.75
解决方案
更新了我的答案以使用 CONVERT 函数...
我会尝试使用该CONVERT
功能..试一试!2 将指定百分之一。CONVERT(DECIMAL(10,2),tr.tax_percent) as 'tax_percent'
use lhsitedb
select t.tax_id, t.description, tr.effective_date,
CONVERT(DECIMAL(10,2),tr.tax_percent) AS
'tax_percent'
from dbo.tax t
inner join dbo.tax_rate tr on tr.tax_id = t.tax_id
where tr.effective_date = (
select max(tr1.effective_date)
from dbo.tax_rate tr1
where tr1.tax_id = tr.tax_id
推荐阅读
- php - 登录页面“mysqli_query”
- scheme - 鸡计划和命令行参数
- bash - EOF 命令未运行后
- r - 如何在 data.table 方式 R 中获取 sapply 的结果?
- jquery - 当显示 jquery ajax 的弹出窗口时,是否可以通过刷新(f5)重新加载当前弹出窗口
- tfs - 从 Azure Data Studio 连接到 Team Foundation Server
- c - 我的代码不工作。我做错了吗?
- angular6 - 刷新如何在angular6中保持相同的路线?
- php - 如何使用 PHP 的 PDO 对象发送 SQL 更新查询
- debian - 安装 rsyslog(Debian)时出现问题。存储库未签名