SQL 查询输出 - 小数点位置,整数太多
SQL Query output - decimal placment, too many integers
我有一个正常运行的查询,我正在合并两个表,删除重复项并使数据保持最新的生效日期。
表格如下所示:
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
首先,您可以使用 apply
来简化您的查询。
其次,您似乎将值存储为 float
而不是 decimal
/numeric
。好吧,你可以在输出上解决这个问题:
select t.tax_id, t.description, tr.effective_date,
round(tr.tax_percent, 2)
from dbo.tax t cross apply
(select top (1) tr.*
from dbo.tax_rate tr
where tr.tax_id = t.tax_id
order by tr.effective_date desc
) tr
你可以使用 str()
str(your_value, 3, 2);
select t.tax_id, t.description, tr.effective_date, str(tr.tax_percent,3,2)
更新了我的答案以使用 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
来自文档:"Floating point data is approximate; therefore, not all values in the data type range can be represented exactly." float
和 real
都存在一些显示异常。
我会尝试显式 CAST
。类似于:
use lhsitedb
select
t.tax_id,
t.description,
tr.effective_date,
CAST(tr.tax_percent AS decimal(18,2)) AS tax_percent --<-- The only change is here.
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
我有一个正常运行的查询,我正在合并两个表,删除重复项并使数据保持最新的生效日期。
表格如下所示:
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
首先,您可以使用 apply
来简化您的查询。
其次,您似乎将值存储为 float
而不是 decimal
/numeric
。好吧,你可以在输出上解决这个问题:
select t.tax_id, t.description, tr.effective_date,
round(tr.tax_percent, 2)
from dbo.tax t cross apply
(select top (1) tr.*
from dbo.tax_rate tr
where tr.tax_id = t.tax_id
order by tr.effective_date desc
) tr
你可以使用 str()
str(your_value, 3, 2);
select t.tax_id, t.description, tr.effective_date, str(tr.tax_percent,3,2)
更新了我的答案以使用 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
来自文档:"Floating point data is approximate; therefore, not all values in the data type range can be represented exactly." float
和 real
都存在一些显示异常。
我会尝试显式 CAST
。类似于:
use lhsitedb
select
t.tax_id,
t.description,
tr.effective_date,
CAST(tr.tax_percent AS decimal(18,2)) AS tax_percent --<-- The only change is here.
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