SQL Server-在每个组中添加页眉和页脚字段

时间:2018-10-19 12:33:33

标签: sql sql-server tsql

我从SQL表中获取以下格式的数据

spring:
  profiles:
    active: prod
  datasource:
    name: dataSource
    url: xxx
    username: xxx
    password: xxx
    #type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: XXXX
mybatis:
   typeAliasesPackage: xxx
   mapperLocations: classpath:mybatis/mapper/*Mapper.xml

我需要在每个组的开头添加页眉,并在每个组的末尾添加页脚。

我需要使用SQL查询获得以下输出。

Code       Name
-----------------------------
Finance    Bank Charges
Finance    Interest Charges
Finance    Other Charges
Insurance  Premium Charges
Insurance  Other Charges
-----------------------------

等待您的回复。 谢谢。

4 个答案:

答案 0 :(得分:3)

也许是简单的UNION ALL

示例

Declare @YourTable Table ([Code] varchar(50),[Name] varchar(50))
Insert Into @YourTable Values 
 ('Finance','Bank Charges')
,('Finance','Interest Charges')
,('Finance','Other Charges')
,('Insurance','Premium Charges')
,('Insurance','Other Charges')


Select Seq=2,* from @YourTable
Union All
Select Distinct 1,[Code],[Code]+' Header' From @YourTable
Union All
Select Distinct 3,[Code],[Code]+' Footer' From @YourTable
Order by [Code],Seq

返回

Seq Code        Name
1   Finance     Finance Header
2   Finance     Bank Charges
2   Finance     Interest Charges
2   Finance     Other Charges
3   Finance     Finance Footer
1   Insurance   Insurance Header
2   Insurance   Premium Charges
2   Insurance   Other Charges
3   Insurance   Insurance Footer

答案 1 :(得分:2)

您需要rollup

原始问题答案:

select code, sum(name)
from table t
group by rollup (code);

编辑:假设您还需要单独的行,则可以这样做:

select code, name
from (select distinct 1 as sort, Code, concat(code, ' header') as name
      from table t
      union all
      select 2, Code, cast(name as varchar(255)) -- This assumes name has numeric value so, casting is needed.
      from table t
      union all
      select distinct 3, Code, concat(code, ' footer')
      from table t
     ) t
order by code, sort;

答案 2 :(得分:1)

我认为这不可能通过SQL进行,但这是一种您可能会发现有用的方法

WITH K AS (
SELECT *, ROW_NUMBER () OVER (PARTITION BY Code ORDER BY Code) RN
FROM T
)
SELECT Code, 
       Name, 
       CASE WHEN RN = 1 
                THEN Code + ' Header'
            WHEN RN = (SELECT MAX(RN) FROM K WHERE K.Code = KK.Code GROUP BY Code)  
                THEN Code + ' Footer'
            ELSE
            '' END AS Title
FROM K KK;

结果:

+-----------+------------------+------------------+
|   Code    |       Name       |      Title       |
+-----------+------------------+------------------+
| Finance   | Bank Charges     | Finance Header   |
| Finance   | Interest Charges |                  |
| Finance   | Other Charges    | Finance Footer   |
| Insurance | Premium Charges  | Insurance Header |
| Insurance | Other Charges    | Insurance Footer |
+-----------+------------------+------------------+

答案 3 :(得分:1)

您也可以使用CROSS APPLY

DECLARE @TempTable TABLE (Code VARCHAR(20),      Name VARCHAR(20))
INSERT INTO @TempTable VALUES
('Finance', 'Bank Charges'),
('Finance', 'Interest Charges'),
('Finance', 'Other Charges'),
('Insurance', 'Premium Charges '),
('Insurance', 'Other Charges')

SELECT X.* FROM 
    ( SELECT *, 
        ROW_NUMBER() OVER(PARTITION BY Code ORDER BY (SELECT NULL)) AS RN 
        FROM @TempTable ) T
    CROSS APPLY (
        SELECT Seq, Code, Code + ' '+ Title AS Name  FROM ( VALUES(1,'Header'),(3, 'Footer')) AS HF (Seq, Title)
        WHERE T.RN = 1
        UNION ALL
        SELECT 2 Seq, T.Code, T.Name
    ) X
ORDER BY Code, Seq, RN

结果:

Seq         Code                 Name
----------- -------------------- ---------------------------
1           Finance              Finance Header
2           Finance              Bank Charges
2           Finance              Interest Charges
2           Finance              Other Charges
3           Finance              Finance Footer
1           Insurance            Insurance Header
2           Insurance            Premium Charges 
2           Insurance            Other Charges
3           Insurance            Insurance Footer