1

我有个疑问。我有一个名为“ID”的列。在该列中,我的值如“FieldName”后跟“FromDate”、“Value”后跟“2012/12/01”、“FieldName”后跟“ToDate”值“2013/12/01”等,

**ID column**    
        FieldName
        FromDt
        Value
        2010/12/01
        FieldName
        ToDt
        Value
        2013/12/21
        FieldName
        CreatedDt
        Value
        2012/10/01
        FieldName
        ModifyDt
        Value
        2013/01/02

现在我想要一张像

**FieldName  Value**
FromDt      2010/12/01
ToDt        2013/12/21
CreatedDt   2012/10/01
ModifyDt    2013/01/02

是否可以在没有聚合函数的情况下使用带有单列的数据透视?请建议我该怎么做?(使用 Pivot 或其他方法)

问候, TNNagasundar

4

2 回答 2

1

Order By您应该按照@Magnus 的建议做另一列。

否则,试试这个。SQLFiddle

WITH cte AS
(
  SELECT ID,ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS row_num
  FROM tbl
 )
SELECT c1.ID AS [field_name], c2.ID AS [value]
FROM cte c1
INNER JOIN cte c2
  ON c2.row_num = c1.row_num + 2
WHERE (c1.row_num % 4) = 2
于 2013-04-11T16:52:25.560 回答
0

我不知道在这种情况下使用枢轴。可以将原始顺序行拆分为 4 组,然后从每个组中选择每个第 2 行和第 4 行,如下所示:

http://sqlfiddle.com/#!3/b2af8/13/0

with t2 as (
  select
    id,
    (row_number() over(order by (select 0)) - 1) / 4 grp,
    (row_number() over(order by (select 0)) - 1) % 4 row
  from t
)
select a.id FieldName,
  b.id Value
from t2 a
join t2 b
  on a.grp = b.grp
  and a.row = 1 -- 2nd row numbering from 0
  and b.row = 3

(灵感来自https://stackoverflow.com/a/6390282/1176601

于 2013-04-11T16:50:50.750 回答