sum - SQL Summarizing data in final row -
i have query generates rows of detail information checks issued. in final output, data concatenated , put 1 column (long story short, needs run infomaker , exported text no delimiting).
anyway, @ end of these rows need summary row contains 2 values sums of detail used in previous rows, along other values hard-coded. final row needs concatenated 1 column, , appear after detail rows.
example of how summary row should appear computed values in brackets:
00123456789999999999940[check amount summary][total number of checks (rows)]000 again, no spaces, tabs, or other delimiters allowed.
i'm stumped on how achieve this. have had suggestions of using union i'm not sure how make work situation.
current query:
declare @checkdate date = '08/30/13' select record = ( -- checking account number (record positions 1-9) cast(cna.bank_acct_num varchar(9)) + -- check number (record positions 10-19) -- must 9 characters (case when len(cr.check_num_numeric) = 1 '00000000' when len(cr.check_num_numeric) = 2 '0000000' when len(cr.check_num_numeric) = 3 '000000' when len(cr.check_num_numeric) = 4 '00000' when len(cr.check_num_numeric) = 5 '0000' when len(cr.check_num_numeric) = 6 '000' when len(cr.check_num_numeric) = 7 '00' when len(cr.check_num_numeric) = 8 '0' else '' end + cast(cr.check_num_numeric varchar(9))) + -- record positions 20-21 - determined bank '20' + -- check amount (record positions 22-31) -- must 10 characters (case when len(cr.check_amt) = 1 '000000000' when len(cr.check_amt) = 2 '00000000' when len(cr.check_amt) = 3 '0000000' when len(cr.check_amt) = 4 '000000' when len(cr.check_amt) = 5 '00000' when len(cr.check_amt) = 6 '0000' when len(cr.check_amt) = 7 '000' when len(cr.check_amt) = 8 '00' when len(cr.check_amt) = 9 '0' else '' end + cast(replace(cr.check_amt,'.','') varchar(10))) + -- date issued (mmddyy)(record positions 32-37) cast(replace(convert(char(10),cr.check_dte,101), '/', '') varchar(10)) + -- record positions 38-40 - determined bank '000' + -- payee information line 1 (record positions 41-90) cr.check_name) chk_num_alpha_ctl cna, chk_reconciliation cr ( cr.check_num_alpha = cna.check_num_alpha ) , ( ( cr.check_rtn_void_dte null ) , ( cr.check_dte = @checkdate ) ) , ( cna.bank_acct_num = 'xxxx-xxxx' ) order cr.check_dte asc
-- first, can simplify query using type of 'right-justify-zero-fill' statement (adjust if more or less 9-characters):
select right('000000000' + cast(cr.check_num_numeric varchar(9)),9) -- try (i'm not able test it, there may adjustments):
union select '00123456789999999999940' + right('000000000' + cast(sum(cr.check_amt) varchar(9)),9) + right('000000000' + cast(count(cr.check_amt) varchar(9)),9) + '000' chk_num_alpha_ctl cna, chk_reconciliation cr ( cr.check_num_alpha = cna.check_num_alpha ) , ( ( cr.check_rtn_void_dte null ) , ( cr.check_dte = @checkdate ) ) , ( cna.bank_acct_num = 'xxxx-xxxx' ) group cr.check_dte order cr.check_dte asc
Comments
Post a Comment