SQL Server 在一个非常嵌套的SQL语句中,我如何确定在哪里可以将一个数字变量转换为一个有2个小数位的小数?

am46iovg  于 2022-12-26  发布在  其他
关注(0)|答案(1)|浏览(107)

在一个非常嵌套的SQL语句中,我想生成一个小数点后只有两位数的十进制值。下面是一个大型SQL程序中的两个段落。我遇到麻烦的语句是:ROUND(((转换(十进制(11,6),求和(pf1.NO_OF_RECONCILED_TRX))/ tot_tab.tot_trx)* 100.0),2)AS“占总帐户的百分比”。我需要“占总帐户的百分比”列中的值为58.82,而不是58.8200000000000000000。
结果的图像也附于

SELECT                                              
a1.ACCOUNT_NUMBER                                           
    , a1.ACCOUNT_NAME                                           
    , ap1.PASS_ID                                           
    , isnull(ap1.PASS_NAME, 'Out') "Pass Name"                                          
----, (convert(decimal(23, 6), sum(pf1.NO_OF_RECONCILED_TRX)) / tot_tab.tot_trx) * 100.0 "% To Total Acct"                                              
, ROUND(((convert(decimal(11, 6), sum(pf1.NO_OF_RECONCILED_TRX)) / tot_tab.tot_trx) * 100.0),2) AS "% To Total Acct"                                            
----, (convert(decimal(23, 6), sum(pf1.NO_OF_RECONCILED_TRX)) / tot_tab.tot_trx) "# Reconciled Items by Pass"                                           
----, SUM (NO_OF_RECONCILED_TRX) as TotalRows                                           
    , SUM (NO_OF_RECONCILED_TRX) as "# Reconciled Items by Pass"                                            
                            
FROM
   BRR_ACCOUNT_HIERARCHIES ah1
   inner join BRR_ACCOUNTS a1 on ah1.ACCOUNT_ID = a1.ACCOUNT_ID
   inner join BRD_PROPERTY prop on a1.ACCOUNT_ID = prop.ACCOUNT_ID and prop.USER_ID = 1952 and        prop.PROPERTY_NAME = 'Allow Access' and prop.PROPERTY_VALUE = 'Yes'
   inner join BRR_PASS_FACT pf1 on ah1.ACCOUNT_ID = pf1.ACCOUNT_ID
   inner join BRD_DAY d1 on pf1.DAY_ID = d1.DAY_ID AND d1.DAY_ID > 15614 and d1.DAY_ID < 15695    --SEPT 30 & DEC 20TH
   left outer join BRR_AUTOREC_PASSES ap1 on pf1.PASS_ID = ap1.PASS_ID
   cross join
(...

结果:

ACCOUNT_NUMBER  ACCOUNT_NAME    PASS_ID Pass Name   % To Total Acct # Reconciled Items by "PASS NAME"
11003   11003 CASH IN TRANSIT-BRANCHES  57  GL (FAST) CO ACCT CENTER FULLREF    58.82000000000000000    24108
11003   11003 CASH IN TRANSIT-BRANCHES  37  GL 1-1 (AR) CO ACCT CENTER REF-NO(Full) 41.15000000000000000    16864
11003   11003 CASH IN TRANSIT-BRANCHES  697 GL 1-1 Co Acct Center Ext Ref not blank o or CIW    0.01000000000000000 6
11003   11003 CASH IN TRANSIT-BRANCHES  58  GL(1-1) AR CO-ACCT-CTR-SRC (CCMWO RESOLVE)  0.01000000000000000 6``

注解掉了我尝试使用的行。最接近我需要的是使用ROUND语句。它用尾随零替换尾随数字,小数点后面的前两个数字除外。我删除了数字,但它们被替换为零。例如,我需要值为58.82而不是58.820000000000。

l7wslrjt

l7wslrjt1#

就像Dale K提到的,只要改变

ROUND(((convert(decimal(11, 6), sum(pf1.NO_OF_RECONCILED_TRX)) / tot_tab.tot_trx) * 100.0),2) AS "% To Total Acct"

CONVERT(DECIMAL(11,2),ROUND(((convert(decimal(11, 6), sum(pf1.NO_OF_RECONCILED_TRX)) / tot_tab.tot_trx) * 100.0),2)) AS "% To Total Acct"

相关问题