将行转换为列

时间:2017-08-16 06:40:19

标签: sql-server

我遇到了SQL Server中的问题。

我的查询是这样的:

select Format(new_qtrdate1,'dd-MMM') as MTBURDATE1,
       Format(new_qtrdate2,'dd-MMM') as MTBURDATE2,
       Format(new_qtrdate3,'dd-MMM') as MTBURDATE3,
       new_mtbur1 as MTBUR1,
       new_mtbur2 as MTBUR2,
       new_mtbur3 as MTBUR3
  from Filterednew_craform
 where

我的查询的当前结果如下所示:

(MTBURDATE1) (MTBURDATE2) (MTBURDATE3) MTBUR1 MTBUR2 MTBUR3
------------------------------------------------------------------------
01-May         01-Jun        01-Jul    34214  10756   9879

我想得到的是:

Date    MTBUR
------------
01-May  34214
01-Jun  10756
01-Jul  9879

我该怎么做?

2 个答案:

答案 0 :(得分:3)

如果您有固定数量的列,请尝试以下:

UPDATE t1 
SET t1.value = t2.value 
FROM table1 t1 
JOIN table2 t2 ON t1.start = t2.start AND t1.end = t2.end 
               AND t2.value NOT LIKE 'pending'
               AND t1.value LIKE 'pending' 
WHERE t1.main_name = 'abc' 
  AND t2.main_name = 'abc'

答案 1 :(得分:1)

我认为UNION ALL应该做你需要的事情:

SELECT (MTBURDATE1)
       , MTBUR1 
  FROM Filterednew_cra
 UNION ALL
SELECT (MTBURDATE2)
       , MTBUR2 
  FROM Filterednew_cra
 UNION ALL
SELECT (MTBURDATE3)
       , MTBUR3 
  FROM Filterednew_cra