2014-10-01 60 views
1

例如,我有一个包含5行7列的表格,我希望将最后两列移动到前两列中。表中的新格式现在是10行5列将成对列中的现有表格重新排列成行

目前表的格式

+-----+------------+----------+------------+---------------+------------+---------------+ 
| id | VisitDate | fkFamily | child1.DOB | child1.Gender | child2.DOB | child2.Gender | 
+-----+------------+----------+------------+---------------+------------+---------------+ 
| 78 | 19/04/2010 |  277 | 14/03/2009 | 0    | NULL  | NULL   | 
| 79 | 20/04/2010 |  289 | 12/08/2007 | 0    | NULL  | NULL   | 
| 107 | 20/04/2010 |  191 | NULL  | NULL   | NULL  | NULL   | 
| 108 | 20/04/2010 |  259 | NULL  | NULL   | 31/03/2010 | 1    | 
| 109 | 20/04/2010 |  126 | NULL  | NULL   | NULL  | NULL   | 
+-----+------------+----------+------------+---------------+------------+---------------+ 

新表格式

+-----+------------+----------+------------+----------------------+ 
| id | VisitDate | fkFamily | child.DOB | child.Gender   | 
+-----+------------+----------+------------+----------------------+ 
| 78 | 19/04/2010 |  277 | 14/03/2009 | 0     | 
| 79 | 20/04/2010 |  289 | 12/08/2007 | 0     | 
| 107 | 20/04/2010 |  191 | NULL  | NULL     | 
| 108 | 20/04/2010 |  259 | NULL  | NULL     | 
| 109 | 20/04/2010 |  126 | NULL  | NULL     | 
| 78 | 19/04/2010 |  277 | NULL  | NULL     | 
| 79 | 20/04/2010 |  289 | NULL  | NULL     | 
| 107 | 20/04/2010 |  191 | NULL  | NULL     | 
| 108 | 20/04/2010 |  259 | 31/03/2010 | 1     | 
| 109 | 20/04/2010 |  126 | NULL  | NULL     | 
+-----+------------+----------+------------+----------------------+ 
+0

ddl和sample会让这不是只有清晰,但也可以帮助。这是不可能的解读这张贴。 sqlfiddle.com是一个很好的开始。 – 2014-10-01 20:47:37

回答

1

您可以通过unpivoting列Child1_DOBChild1_Gender得到最终结果,等等。从SQL Server 2005开始,unpivot功能可用,但对于您的情况,我实际上使用CROSS APPLY,因此您可以将Child1Child2成对值。

的语法是:

select 
    t.id, 
    t.visitdate, 
    t.fkFamily, 
    c.child_DOB, 
    c.child_Gender 
from yourtable t 
cross apply 
(
    select child1_DOB, child1_Gender union all 
    select child2_DOB, child2_Gender 
) c (child_DOB, child_Gender); 

SQL Fiddle with Demo

然后,你还可以包括每个值的标识,所以你知道,如果它属于孩子一个或两个:

select 
    t.id, 
    t.visitdate, 
    t.fkFamily, 
    c.child, 
    c.child_DOB, 
    c.child_Gender 
from yourtable t 
cross apply 
(
    select 'Child1', child1_DOB, child1_Gender union all 
    select 'Child2', child2_DOB, child2_Gender 
) c (child, child_DOB, child_Gender) 

请参阅SQL Fiddle with Demo。这些给类似于一个结果:

| ID | VISITDATE | FKFAMILY | CHILD_DOB | CHILD_GENDER | 
|-----|------------|----------|------------|--------------| 
| 78 | 19/04/2010 |  277 | 14/03/2009 |   0 | 
| 78 | 19/04/2010 |  277 |  (null) |  (null) | 
| 79 | 20/04/2010 |  289 | 12/08/2007 |   0 | 
| 79 | 20/04/2010 |  289 |  (null) |  (null) | 
| 107 | 20/04/2010 |  191 |  (null) |  (null) | 
| 107 | 20/04/2010 |  191 |  (null) |  (null) | 
| 108 | 20/04/2010 |  259 |  (null) |  (null) | 
| 108 | 20/04/2010 |  259 | 31/03/2010 |   1 | 
| 109 | 20/04/2010 |  126 |  (null) |  (null) | 
| 109 | 20/04/2010 |  126 |  (null) |  (null) | 
1

您可以通过使用UNION格式化表格弄成这个样子: -

SELECT * FROM (
SELECT id, VisitDate, fkFamily, child1_DOB as child_DOB, child1_Gender as child_Gender 
FROM yourtable 
UNION 
SELECT id, VisitDate, fkFamily, child2_DOB, child2_Gender 
FROM yourtable) as temp 

FIDDLE

,如果你想创建你可以使用SELECT INTO例如: -

SELECT * INTO yournewtable FROM (
    SELECT id, VisitDate, fkFamily, child1_DOB as child_DOB, child1_Gender as child_Gender 
    FROM yourtable 
    UNION 
    SELECT id, VisitDate, fkFamily, child2_DOB, child2_Gender 
    FROM yourtable) as temp