1

我有以下选择:

SELECT TOP 1000 [ObjectiveId]
      ,[Name]
      ,[Text]
  FROM [dbo].[Objective]

它给了我

Name    Text
0100    Header1
0101    Detail1
0102    Detail2
0200    Header2
0201    Detail1a
0202    Detail1b

有没有办法可以用 ||| 制作这样的字符串 数据的分隔符。

Header1  |||  Detail1
Header1  |||  Detail2
Header2  |||  Detail1a
Header2  |||  Detail1b etc. 

这里的关键是,当名称的最后两位数字为“00”时,它是后续详细信息行的标题行。

4

4 回答 4

1

询问:

SQLFIDDLE示例

SELECT t1.Text + ' ||| ' + t2.Text AS clm
FROM Objective t1
  LEFT JOIN Objective t2
    ON SUBSTRING(t2.Name, 1, 2) = SUBSTRING(t1.Name, 1, 2)
    AND t2.Name not like '%00'
WHERE t1.Name like '%00'

结果:

|                  CLM |
------------------------
|  Header1 ||| Detail1 |
|  Header1 ||| Detail2 |
| Header2 ||| Detail1a |
| Header2 ||| Detail1b |
于 2013-08-05T11:58:18.350 回答
1
; WITH headers AS (
  SELECT Name
       , Text
  FROM   dbo.Objective
  WHERE  Right(Name, 2) = '00'
)
, details AS (
  SELECT Name
       , Text
  FROM   dbo.Objective
  WHERE  Right(Name, 2) <> '00'
)
SELECT headers.Text + ' ||| ' + details.Text
FROM   headers
 LEFT
  JOIN details
    ON Left(details.name, 2) = Left(headers.name, 2)
于 2013-08-05T10:50:44.767 回答
0

尝试这个:

;WITH Header
AS
(
    SELECT  LEFT([Name], 2) AS HeaderKey,
            [Name], 
            [Text]
    FROM Objective
    WHERE RIGHT([Name], 2) = '00'
),
Detail
As
(
    SELECT  LEFT([Name], 2) AS HeaderKey,
            [Name], 
            [Text]
    FROM Objective
    WHERE RIGHT([Name], 2) <> '00'
)
SELECT Header.[Text] + '|||' + Detail.[Text]
FROM Header
INNER JOIN Detail
    ON Header.HeaderKey = Detail.HeaderKey
于 2013-08-05T10:59:52.727 回答
-1
select top 1000 A.[Text]+' ||| '+B.[Text]
FROM [dbo].[DILL] as A
Inner join [dbo].[DILL] as B
on substring(A.[Name],3,5) = '00'
and A.[Text] != B.[Text] 
and substring(B.[Name],1,3) = substring(A.[Name],1,3)

编辑:忘记添加最后一行代码并使用正确的JOIN

于 2013-08-05T10:51:07.923 回答