SQL - 从列中逐行检索并构建一个包含两列的表

时间:2016-06-10 19:34:09

标签: sql-server sql-server-2008

我必须从Column中逐行获取记录并构建一个包含两列的新表。

在原始表列中将有一个9位字符,我必须将其选为Bill Id。对于每个账单ID,都会有一些消息。所以在我的新表中,它将是Bill Id和Message两列。

我必须从原始表格中选择最多5条消息,或者如果我在5条消息之前点击新的帐单ID。

create table #t 
(col2 nvarchar(4000))

insert into #t values
('No Bills Picked up for Processing'),
('Total 5 Bills picked up for Processing'),
('AB358399B'),
('XML Validation Failed'),
('CN290550T'),
('service Call Done'),
('Status : SUCCESSFULL'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('CN290570T'),
('service Call Done'),
('Status : SUCCESSFULL'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing')


select * into #billid from #t
where  len(ltrim(rtrim(col2))) = 9

select * from #billid

select * from #t




-- Expected Results
--Bill          Message
--------------------------
-- AB358399B    XML Validation Failed 
-- CN290550T    service Call Done
--              Status : SUCCESSFULL
--              No Bills Picked up for Processing
--              No Bills Picked up for Processing
--              No Bills Picked up for Processing
-- CN290570T    service Call Done
--              Status : SUCCESSFULL
--              No Bills Picked up for Processing
--              No Bills Picked up for Processing
--              No Bills Picked up for Processing

1 个答案:

答案 0 :(得分:1)

请注意没有隐式排序顺序!!! 结果集的排序是随机的!

因此我引入了IDENTITY列......

使用SQL Server 2012+,你可以使用LEADLAG,而2008年你必须再多一点......

顺便说一下:我会避免只有第一个写的方法。这是您的表示层应该做的事情......

--Your table with an IDENTITY column
create table #t 
(ID INT IDENTITY,col2 nvarchar(4000))

insert into #t(col2) values
('No Bills Picked up for Processing'),
('Total 5 Bills picked up for Processing'),
('AB358399B'),
('XML Validation Failed'),
('CN290550T'),
('service Call Done'),
('Status : SUCCESSFULL'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('CN290570T'),
('service Call Done'),
('Status : SUCCESSFULL'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing'),
('No Bills Picked up for Processing');

- 这是查询

WITH Only9Chars AS
(
    SELECT ID,col2 
    FROM #t 
    WHERE len(ltrim(rtrim(col2))) = 9
)
,StartAndEnd AS
(
    SELECT o9c.ID+1 AS startIndex
          ,o9c.col2 AS Bill 
          ,nxt.ID 
          ,ISNULL(nxt.ID,999999)-1 AS endIndex
    FROM Only9Chars AS o9c
    OUTER APPLY(SELECT TOP 1 nxt.ID FROM Only9Chars AS nxt WHERE nxt.ID>o9c.ID) AS nxt
)
SELECT CASE WHEN ROW_NUMBER() OVER(PARTITION BY Bill ORDER BY ID)=1 THEN Bill ELSE '' END AS Bill 
      ,Details.*
FROM StartAndEnd
OUTER APPLY(SELECT x.col2 
            FROM #t AS x 
            WHERE x.ID BETWEEN startIndex AND endIndex) AS Details
GO
DROP TABLE #t;

这是结果

+-----------+-----------------------------------+
| Bill      | col2                              |
+-----------+-----------------------------------+
| AB358399B | XML Validation Failed             |
+-----------+-----------------------------------+
| CN290550T | service Call Done                 |
+-----------+-----------------------------------+
|           | Status : SUCCESSFULL              |
+-----------+-----------------------------------+
|           | No Bills Picked up for Processing |
+-----------+-----------------------------------+
|           | No Bills Picked up for Processing |
+-----------+-----------------------------------+
|           | No Bills Picked up for Processing |
+-----------+-----------------------------------+
| CN290570T | service Call Done                 |
+-----------+-----------------------------------+
|           | Status : SUCCESSFULL              |
+-----------+-----------------------------------+
|           | No Bills Picked up for Processing |
+-----------+-----------------------------------+
|           | No Bills Picked up for Processing |
+-----------+-----------------------------------+
|           | No Bills Picked up for Processing |
+-----------+-----------------------------------+
|           | No Bills Picked up for Processing |
+-----------+-----------------------------------+
|           | No Bills Picked up for Processing |
+-----------+-----------------------------------+
|           | No Bills Picked up for Processing |
+-----------+-----------------------------------+
|           | No Bills Picked up for Processing |
+-----------+-----------------------------------+
|           | No Bills Picked up for Processing |
+-----------+-----------------------------------+
|           | No Bills Picked up for Processing |
+-----------+-----------------------------------+
|           | No Bills Picked up for Processing |
+-----------+-----------------------------------+
|           | No Bills Picked up for Processing |
+-----------+-----------------------------------+