如何将列转换为每个状态的行

时间:2014-05-20 10:38:10

标签: sql

数据保存在表格中如下。 enter image description here

我需要显示如下数据。 enter image description here

请建议查询

2 个答案:

答案 0 :(得分:0)

这是在SQL Server中执行此操作的方法:

  SELECT Name, 'Joined' AS [ACTION], JOIN_DT AS ACTION_DATE
  FROM SomeTable
UNION ALL
  SELECT Name, 'Started' START_DTTM
  FROM SomeTable
UNION ALL
  SELECT Name, 'ended', END_DT
  FROM SomeTable

答案 1 :(得分:0)

试试这个:

    SELECT Name, Action, Action_Date FROM (
       SELECT Name, 'Joined' as Action, JOIN_DT as ACTION_DATE FROM TableA
       UNION ALL
       SELECT Name, 'Started', START_DT FROM TableA
       UNION ALL
       SELECT Name, 'Ended', END_DT FROM TableA)
    ORDER BY Name;