SQL - 输出标题行,数据并将数据汇总为页脚行

时间:2016-03-11 06:23:29

标签: sql sql-server tsql

我发现了我想要的变化,但并不完全!

我需要将数据导出为特定的字符串格式,其中包含标题,行和页脚。

我所遇到的问题是在页脚中,我需要在身体中记录一些记录。我见过的每个例子都有字段,然后汇总为字段(即列),而我只需要1列输出

基本上我想要出现的是一个如下所示的表格(请注意 - 这是数据上传的银行要求。我无法控制行的格式,我只需要知道如何总结数据行进入页脚)

TIA

  

====标题行信息=====

     

第1行

     

第2行

     

第3行

     

第4行

     

===上述4行的摘要信息,即数量和行数的总和===

ETA一些真实的数据

感谢。输出本质上是一个进入银行的EFT交易清单(因此我没有在数据中加入太多信息)。

我还有另外一个狡猾的数据。请注意,在代码中,它会被填充并生成文本以对齐列。

  

0 01WBC Trust A / c 000000EFT0000-0000

     

0000-12349876543 500003456.62A& S名称123456 000-001 123456我们的法律Trust00000000   0000-12341234567 500007654.56AM LastName 654321 000-001 654321我们的法律Trust00000000   0000-12348765432 500000315.35M姓486245 000-001 957321我们的法律Trust00000000   7999-9999 0011426.5300000000000011426.53 3

(预告片(以7999开头)包含总值(50后)和记录数量(3)

到目前为止我所拥有的。

SELECT
-----HEADER
'0' + space(17)                                                                         --RecordType (0)
+'01'
--Reel Sequence Number   ***If more than one processed, does this need to increase
+'WBC' + space(7)
--        Financial Institution
+(CAST(Trust' AS CHAR (26)))    --Name of User (provided)
+(CAST('000000' AS CHAR (6)))   --Number of user Supplying File (provided)
+(CAST('EFT0000-0000' AS CHAR (12)))    --Our Reference
    AS ROW1

    UNION ALL

    SELECT  
    -----DATA
    '1'                               --RecordType
    +STUFF(ltrim(rtrim([DIRDEB_BSB])),4,0,'-')          --Bank/State/Branch Number
+ (CAST(ltrim(rtrim([DIRDEB_ACCNUM])) AS CHAR(9)))  --Account Number     (right justified and blank filled)
+' '                --Indicator (blank)
+ '50'               ---Transaction Code
+ right('0000000000' + convert(varchar,[amount]),10)    ---    Amount (right justified zero filled)
+(cast(ltrim(rtrim(DIRDEB_ACCNAME)) as char(32)))                ---Account     (left justified blank filled)
    + (CAST( right([MATTERID],6) AS CHAR(18)))   ---Lodgement Reference
     + (CAST( '000-001 123456'  AS CHAR(32)))   ---Trace Record  (BSB, Account, blank filled)
+ (CAST( 'Trust'  AS CHAR(16)))          --Name of Remitter (left and blank filled)
+ (CAST( '00000000'  AS CHAR(8)))       ---Amount of witholding tax zero filled
AS EFTEXPORT
  FROM AFF_EFT_Activity A
  where A.CHEQUEID NOT LIKE '%RV%' and A.CHEQUEID NOT LIKE '%CN%'  

  UNION ALL

 SELECT
   ----TRAILER
  '7'              --- Record Type (must be 7)
        +(CAST('999-999' AS CHAR (7)))    ---BSB Format Filler must be 999-999)
        +(CAST('' AS CHAR (12)))        ---Blank Filled
        --+(CAST(SUM(X.[amount]) as varchar(50)))       ---Net Total Amount
        +(CAST('0' AS CHAR (10)))                                                                       ---Credit Amount
        --+(CAST(SUM(X.[amount])  AS char (10)))        ---Debit amount
        +(CAST('' AS CHAR (24)))                    ---Blank
        +(CAST(count(*) AS CHAR (6)))           --- count of records
        +(CAST('' AS CHAR (40)))                    ---- Blank
        AS EFTSUMM
          FROM AFF_EFT_Activity X
          where X.CHEQUEID NOT LIKE '%RV%' and X.CHEQUEID NOT LIKE '%CN%'  

1 个答案:

答案 0 :(得分:0)

很难说出你想要做什么。您的示例数据并未真正提供除布局信息之外的任何详细信息。在您的示例输出中放置真实的数据将非常有用。

无论如何,这是一个完全做作的例子,展示了如何创建一个看起来与你的样本类似的数据集:

declare @data table (name char(20), cost int, quantity int)

insert into @data values
('chair'   ,  75, 10),
('desk'    , 250,  4),
('monitor' , 120,  2),
('computer', 850,  1)

select 'ITEM                  COST  QNTY' as HeaderRow

union all

select replicate('-', 20) + '  ' + '----  ----' as HeaderLine

union all

select name + '  ' + cast(cost as char(4)) + '  ' + cast(quantity as char(4)) as Data
from   @data

union all

select replicate('-', 20) + '  ' + '----  ----' as FooterLine

union all

select 'Count: ' + cast(count(*) as char(5)) + replicate(' ', 8) + '  ' + cast(sum(cost) as char(4)) + '  ' + cast(sum(quantity) as char(4)) as FooterRow
from   @data

这是输出的样子:

ITEM                  COST  QNTY
--------------------  ----  ----
chair                 75    10  
desk                  250   4   
monitor               120   2   
computer              850   1   
--------------------  ----  ----
Count: 4              1295  17  

你也可以正确地对齐数字,但这需要更多的努力。