SQL行重复列

时间:2015-05-20 10:55:29

标签: sql sql-server-2012

我有这些数据:

aTextBox3.onchange = function() { 
    alert(prueba); 
    for (var i=0;i<myLength;i++) { 
        //form.precio.value = valor_total; 
        form.precio[i].value = form.cantidad[i].value * form.preciounitario[i].value; 
    } 
};

我想这样:

col1|col2|col3|col4|col5|col6|col7
0062|date|501|text1|text1|text1|text1
0062|date|502|text2|text2|text2|text2       
0062|date|503|text3|text3|text3|text3       
0063|date|501|text1|text1|text1|text1       
0063|date|502|text2|text2|text2|text2
0063|date|503|text3|text3|text3|text3

如何在带有SQL Server 2008 R2数据库的Microsoft SQL Server 2012中执行此操作?

我想我必须用枢轴做点什么,但我不知道如何开始。

1 个答案:

答案 0 :(得分:1)

Double self FULL OUTER JOIN

select coalesce(t1.col1, t2.col1, t3.col1),
       t1.col2, t1.col3, t1.col4, t1.col5, t1.col6, t1.col7,
       t2.col2, t2.col3, t2.col4, t2.col5, t2.col6, t2.col7,
       t3.col2, t3.col3, t3.col4, t3.col5, t3.col6, t3.col7
from (select * from mytable where col3 = 501) as t1
full outer join (select * from mytable where col3 = 502) as t2 on t1.col1 = t2.col1
full outer join (select * from mytable where col3 = 503) as t3 on t2.col1 = t3.col1

ANSI SQL语法。不知道SQL Server是否支持它。