行转列 sql

最近做的一个项目刚好要用到行转列的问题.由于以前没用写过这样的SQL语句,没办法只有到网上寻找答案,虽然能搜到相当多的结果,可仔细一看,都是列转行... 肺---气的都要炸了,全骗人..现在好不容易写出行转列的SQL 语句.希望对某些人有所帮助....


SELECT     sss
FROM         (SELECT     TOP (1) 1 AS Expr1, A AS sss
                       FROM          com_jjxx
                       UNION
                       SELECT     TOP (1) 2 AS Expr1,  B AS aaaa
                       FROM         com_jjxx AS com_jjxx_5
                       UNION
                       SELECT     TOP (1) 3 AS Expr1,  C AS bbb
                       FROM         com_jjxx AS com_jjxx_4
                       UNION
                       SELECT     TOP (1) 4 AS Expr1,  D AS ccc
                       FROM         com_jjxx AS com_jjxx_3
                       UNION
                       SELECT     TOP (1) 5 AS Expr1, E AS ddd
                       FROM         com_jjxx AS com_jjxx_2
                       UNION
                       SELECT     TOP (1) 6 AS Expr1, F  AS eeee
                       FROM         com_jjxx AS com_jjxx_1) AS a

 

有必要解释一下... 1,2,3,4,5,6, 就是就要显示的内容的一个排序..  A,B,C,D,E,F 就是要显示的内容.当然, com_jjxx 这个表里面要有这些字段.     com_jjxx  后面也可以加WHERE  条件...

你可能感兴趣的:(sql,C++,c,F#,C#)