我有一个查询,它会生成有关已发布检查的详细信息行。在最终输出中,数据全部连接并放入一列(长话短说,它需要从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
答案 0 :(得分:1)
- 首先,您可以使用此类型的“right-justify-zero-fill”语句简化查询(如果多于或少于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