科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道求一个sql语句,实现在查询结果中显示每5行后加一空白行

求一个sql语句,实现在查询结果中显示每5行后加一空白行

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

一个sql语句,实现在查询结果中显示每5行后加一空白行

作者:csdn 来源:csdn 2009年12月18日

关键字: MS-SQL Server 问答

  • 评论
  • 分享微博
  • 分享邮件

一个sql语句,实现在查询结果中显示每5行后加一空白行

SQL code
exec(
select 1 as jh, ... pivot
union all
select 2 as jh, ... pivot
union all
select 3 as jh, ... pivot
union all
select 4 as jh, ... pivot
union all
select 5 as jh, ... pivot
)
查询结果为:
1 ...
2 ...
3 ...
4 ...
5 ...

1 ...
2 ...
3 ...
4 ...
5 ...

1 ...
2 ...
3 ...
4 ...
5 ...
......
其中这个查询有30列

SQL code
union all
select '' as jh ...

30列啊其中还有pivot转化的列

 

SQL codeCREATE TABLE [TB] (
    [ID] [int] IDENTITY (1, 1) NOT NULL ,
    [ITEM] [int] NULL ,
    [NOTE] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
    CONSTRAINT [PK_TB] PRIMARY KEY  CLUSTERED
    (
        [ID]
    )  ON [PRIMARY]
) ON [PRIMARY]
GO

 


INSERT INTO TB (ITEM, NOTE) SELECT 3,'XX'
INSERT INTO TB (ITEM, NOTE) SELECT 3,'XX'
INSERT INTO TB (ITEM, NOTE) SELECT 4,'XX'
INSERT INTO TB (ITEM, NOTE) SELECT 5,'XX'
INSERT INTO TB (ITEM, NOTE) SELECT 5,'XX'
INSERT INTO TB (ITEM, NOTE) SELECT 6,'XX'

SELECT * FROM TB

/*-----------------------
3    XX
3    XX
4    XX
5    XX
5    XX
6    XX
*/
SELECT
    ITEM=CASE WHEN ID=(
                          SELECT MIN(ID)
                          FROM TB
                          WHERE ITEM=A.ITEM
                      )
               THEN RTRIM(ITEM) ELSE '' END,
    NOTE
FROM TB AS A
ORDER BY ID

/*
ITEM         NOTE
------------ --------------------
3            XX
             XX
4            XX
5            XX
             XX
6            XX
SELECT
    ITEM,
    NOTE
FROM (
    SELECT
        RTRIM(ITEM) AS ITEM,NOTE,ITEM AS order1,0 AS order2
    FROM TB
    UNION ALL
    SELECT '','',ITEM,1
    FROM TB
    GROUP BY ITEM
) AS A
ORDER BY order1,order2
3    XX
3    XX

4    XX

5    XX
5    XX

6    XX

SQL codeCREATE TABLE [TB] (
    [ID] [int] IDENTITY (1, 1) NOT NULL ,
    [ITEM] [int] NULL ,
    [NOTE] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
    CONSTRAINT [PK_TB] PRIMARY KEY  CLUSTERED
    (
        [ID]
    )  ON [PRIMARY]
) ON [PRIMARY]
GO

 


INSERT INTO TB (ITEM, NOTE) SELECT 3,'XX'
INSERT INTO TB (ITEM, NOTE) SELECT 3,'XX'
INSERT INTO TB (ITEM, NOTE) SELECT 4,'XX'
INSERT INTO TB (ITEM, NOTE) SELECT 5,'XX'
INSERT INTO TB (ITEM, NOTE) SELECT 5,'XX'
INSERT INTO TB (ITEM, NOTE) SELECT 6,'XX'

SELECT * FROM TB

/*-----------------------
3    XX
3    XX
4    XX
5    XX
5    XX
6    XX
*/
SELECT
    ITEM=CASE WHEN ID=(
                          SELECT MIN(ID)
                          FROM TB
                          WHERE ITEM=A.ITEM
                      )
               THEN RTRIM(ITEM) ELSE '' END,
    NOTE
FROM TB AS A
ORDER BY ID

/*
ITEM         NOTE
------------ --------------------
3            XX
             XX
4            XX
5            XX
             XX
6            XX
SELECT
    ITEM,
    NOTE
FROM (
    SELECT
        RTRIM(ITEM) AS ITEM,NOTE,ITEM AS order1,0 AS order2
    FROM TB
    UNION ALL
    SELECT '','',ITEM,1
    FROM TB
    GROUP BY ITEM
) AS A
ORDER BY order1,order2
3    XX
3    XX

4    XX

5    XX
5    XX

6    XX

 

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章