【问题标题】:SQL Query output - decimal placment, too many integersSQL 查询输出 - 小数位置,整数太多
【发布时间】:2020-04-21 19:17:20
【问题描述】:

我有一个正常运行的查询,我正在合并两个表,删除重复项并将数据保留为最近的生效日期。

表格如下所示:

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 

【问题讨论】:

  • tax_percent 的数据类型是什么?
  • 设置为“浮动”。

标签: sql sql-server sql-server-2008


【解决方案1】:

更新了我的答案以使用 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

【讨论】:

  • 运气不好,令人惊讶的是仍然收到相同的输出。
  • 所以,这个查询在执行 Server Management Studio 时工作得很好,但是,我正在使用 sqlcmd 并将文件导出到 .txt 文件中。无论出于何种原因,我仍然看到多余的小数点。
  • 啊……我知道了。我更新了我的答案以使用 CONVERT 函数......让我知道这是否有效
  • 另外,如果这不起作用@JonnyDavy,您可以尝试将此数据放入临时表中,然后将其导出。
  • 介意将其标记为答案伴侣吗?我是这个游戏的新手
【解决方案2】:

首先,您可以使用apply 简化查询。

其次,您似乎将值存储为floats 而不是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

【讨论】:

  • 看起来不错,但出于某种原因:Msg 4104, Level 16, State 1, Line 8 无法绑定多部分标识符“tr1.effective_date”。
  • @JonnyDavy。 . .那是一个错字。
  • 所以,这个查询在执行 Server Management Studio 时工作得很好,但是,我正在使用 sqlcmd 并将文件导出到 .txt 文件中。无论出于何种原因,我仍然看到多余的小数点。
【解决方案3】:

来自文档:"Floating point data is approximate; therefore, not all values in the data type range can be represented exactly." floatreal 都存在一些显示异常。

我会尝试明确的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

【讨论】:

  • 非常感谢,埃里克,这也很有效。结果与@BadBoyBI 相同
【解决方案4】:

你可以使用 str()

https://docs.microsoft.com/en-us/sql/t-sql/functions/str-transact-sql?redirectedfrom=MSDN&view=sql-server-ver15

str(your_value, 3, 2);  


select t.tax_id, t.description, tr.effective_date, str(tr.tax_percent,3,2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 2015-05-04
    相关资源
    最近更新 更多