0

我有一个查询,它生成有关已签发支票的详细信息行。在最终输出中,数据全部连接并放入一列(长话短说,它需要从 Infomaker 运行并导出为不带分隔符的文本)。

无论如何,在这些行的末尾,我需要一个汇总行,其中包含两个值,这些值是前面行中使用的详细信息的总和,以及其他硬编码的值。最后一行也需要连接到一列,并出现在所有详细信息行之后。

汇总行应如何显示的示例以及括号中的计算值:

00123456789999999999940[CHECK AMOUNT SUMMARY][TOTAL NUMBER OF CHECKS (ROWS)]000

同样,不允许使用空格、制表符或任何其他分隔符。

我对如何实现这一目标感到困惑。我有使用 UNION 的建议,但我不确定如何在这种情况下使用它。

当前查询:

declare @checkDate date = '08/30/13'

select 
record = (
-- Checking account number (Record positions 1-9)
cast(cna.BANK_ACCT_NUM as varchar(9)) +    

-- Check number (Record positions 10-19) -- must always be nine characters  
(case 
    when LEN(cr.CHECK_NUM_NUMERIC) = 1 
    then '00000000'
    when LEN(cr.CHECK_NUM_NUMERIC) = 2 
    then '0000000' 
    when LEN(cr.CHECK_NUM_NUMERIC) = 3 
    then '000000'
    when LEN(cr.CHECK_NUM_NUMERIC) = 4 
    then '00000'
    when LEN(cr.CHECK_NUM_NUMERIC) = 5 
    then '0000'
    when LEN(cr.CHECK_NUM_NUMERIC) = 6 
    then '000'
    when LEN(cr.CHECK_NUM_NUMERIC) = 7 
    then '00'
    when LEN(cr.CHECK_NUM_NUMERIC) = 8 
    then '0'
    else ''
    end + cast(cr.CHECK_NUM_NUMERIC as varchar(9))) +

-- Record positions 20-21 - as determined by the bank
'20' +

-- Check amount (Record positions 22-31) -- must always be 10 characters
(case 
    when LEN(cr.CHECK_AMT) = 1 
    then '000000000'
    when LEN(cr.CHECK_AMT) = 2 
    then '00000000' 
    when LEN(cr.CHECK_AMT) = 3 
    then '0000000'
    when LEN(cr.CHECK_AMT) = 4 
    then '000000'
    when LEN(cr.CHECK_AMT) = 5 
    then '00000'
    when LEN(cr.CHECK_AMT) = 6 
    then '0000'
    when LEN(cr.CHECK_AMT) = 7 
    then '000'
    when LEN(cr.CHECK_AMT) = 8 
    then '00'
    when LEN(cr.CHECK_AMT) = 9 
    then '0'
    else ''
    end + cast(REPLACE(cr.CHECK_AMT,'.','') as varchar(10))) +

-- Date issued (MMDDYY)(Record positions 32-37)
cast(REPLACE(convert(char(10),cr.CHECK_DTE,101), '/', '') as varchar(10)) +   

-- Record positions 38-40 - as determined by the bank
'000' +

-- Payee information line 1 (Record positions 41-90)
cr.CHECK_NAME)

from chk_num_alpha_ctl cna,   
chk_reconciliation cr 
where ( cr.check_num_alpha = cna.check_num_alpha ) and  
( ( cr.check_rtn_void_dte is null ) AND  
( cr.check_dte = @checkDate ) ) AND  
( cna.bank_acct_num = 'xxxx-xxxx' )   
order by cr.check_dte ASC   
4

1 回答 1

1

-- 首先,您可以使用这种类型的“右对齐零填充”语句来简化查询(如果多于或少于 9 个字符,请调整):

select right('000000000' + cast(cr.CHECK_NUM_NUMERIC as varchar(9)),9) 

-- 然后尝试这样的事情(我无法测试它,所以可能会有一些调整):

UNION
select '00123456789999999999940' 
+ right('000000000' + cast(sum(cr.CHECK_AMT) as varchar(9)),9) 
+ right('000000000' + cast(count(cr.CHECK_AMT) as varchar(9)),9) 
+ '000'
from chk_num_alpha_ctl cna,   
chk_reconciliation cr 
where ( cr.check_num_alpha = cna.check_num_alpha ) and  
( ( cr.check_rtn_void_dte is null ) AND  
( cr.check_dte = @checkDate ) ) AND  
( cna.bank_acct_num = 'xxxx-xxxx' )   
GROUP BY cr.check_dte 
order by cr.check_dte ASC  
于 2013-09-04T17:59:41.750 回答