SQL Server:使用regexp合并两个值

时间:2018-01-02 09:34:35

标签: sql sql-server

我正在使用SQL Server数据库并拥有以下表格

表"数据"

   --------------------------------------------------------------------------------------------------|
   | Id |col_1_type | col_1_name     | col_2_type | col_2_name    | col_3_type | col_3_name          |
   --------------------------------------------------------------------------------------------------|
   | 1  |KI         | Inflation Rate | KI         | Currency Rate | MI         | Government Spending |
   --------------------------------------------------------------------------------------------------|

我只是想让我的结果像这样:

   +----+------------------------+
   | Id | results                |
   +----+------------------------+
   | 1  | KI-Inflation Rate      |
   | 2  | KI-Currency Rate       |
   | 3  | MI-Government Spending |
   +----+------------------------+

列名是强制性的,那是什么让它变得复杂我猜? 我知道你可以合并2个值或连接它,但我坚持列名称,如col_1_name和col_2_type。我是否需要使用正则表达式?

4 个答案:

答案 0 :(得分:0)

请试试这个 -

select ROW_NUMBER() OVER (ORDER BY (select null)) id , results
from 
(
    SELECT CONCAT(col_1_type,'-',col_1_name) results 
    FROM [Data]
    UNION ALL
    SELECT CONCAT(col_2_type,'-',col_2_name)    
    FROM Data
    UNION ALL
    SELECT CONCAT(col_3_type,'-',col_3_name)   
    FROM Data
)o

或者这也是

SELECT Id,results
         FROM  Data
                CROSS apply 
(VALUES (CONCAT(col_1_type,'-',col_1_name),1),(CONCAT(col_2_type,'-',col_2_name),2)
,(CONCAT(col_3_type,'-',col_3_name) ,3) ) cs (results,Id)

答案 1 :(得分:0)

我会在CTEstuff()

的帮助下使用交叉申请
;with cte as
(
    select a.* from table t
    cross apply (
          values 
                (t.col_1_type, 'col_1'),
                (t.col_1_name, 'col_1'),
                (t.col_2_type, 'col_2'),
                (t.col_2_name, 'col_2'),
                (t.col_3_type, 'col_3'),
                (t.col_3_name, 'col_3')
    ) a(name, id)
) 
select distinct stuff(
               (select '-'+name from cte where id= c.id for xml path('')),
             1,1, '' 
          ) [Results],
from cte c

编辑:

不确定Id列,但我的猜测可以通过使用排名功能

来解决
select row_number() over (order by (select 1)) Id, 
      cc.Results from
(
    select distinct stuff(
               (select '-'+id from cte where name = c.name for xml path('')),
             1,1, '' 
          ) [Results]
from cte c
) cc

结果:

Id  Results
1   KI-Currency Rate
2   KI-Inflation Rate
3   MI-Government Spending

答案 2 :(得分:0)

示例数据

IF OBJECT_ID('tempdb..#t') iS NOT NULL
DROP TABLE #t
IF OBJECT_ID('dbo.temp','U') iS NOT NULL
DROP TABLE temp

 ;With CTe(
  Id ,col_1_type , col_1_name, col_2_type , col_2_name, col_3_type , col_3_name )
  AS
  (   
  SELECT 1,'KI','Inflation Rate','KI','Currency Rate','MI','Government Spending'
  )
  SELECT * INTO  temp FROM CTe

使用Dynamic sql

DECLARE @Sqlstring nvarchar(max)
        ,@SQlQuery nvarchar(max)

;WITH cte
AS
(
SELECT COLUMN_NAME ,
      ((ROW_NUMBER()OVER(ORDER BY (SELECT NULL))-1)/2 )+1 AS BatchSeq
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='temp' AND COLUMN_NAME<>'Id'
)
SELECT @Sqlstring=STUFF((SELECT ', '+COLUMN_NAME FROM
(
    SELECT  DISTINCT '('+STUFF((SELECT   ', '+COLUMN_NAME
    FROM cte i
    WHERE i.BatchSeq=o.BatchSeq FOR XML PATH ('')),1,1,'') +')' AS COLUMN_NAME
    FROM cte o
)dt
FOR XML PATH ('')),1,1,'')


SET @SQlQuery=' 
            SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS ID,
                   CONCAT(col_type,''-'',Col_names) AS Result 
            FROM Temp 
                CROSS APPLY ( VALUES '+@Sqlstring+') dt(col_type,Col_names)'

PRINT @SQlQuery
EXEC(@SQlQuery)

结果

   ID   Result
  -----------------------
    1   KI-Inflation Rate
    2   KI-Currency Rate
    3   MI-Government Spending 

答案 3 :(得分:-2)

试试这个

select (column1 || ' '|| column2) from table;

SELECT tablename.col_1_type + ' ' + tablename.col_1_name AS results;