当前位置:编程学习 > C#/ASP.NET >>

求一条SQL语句,将一个字段显示成两列

有个表的字段FEntryDC的值只能为0或1分别表示借和贷,现在我想通过一条SQL语句,将查询出来的结果显示成下面这样。

    借           贷
   5000
             47850
             32000
  67126


不知道这条语句怎么写,只知道SQL中有个函数可以实现。请高手帮帮小弟。 --------------------编程问答-------------------- ^ō^ 用存储过程吧! 调用时也是一条语句喔.. --------------------编程问答-------------------- ref: 典型的行变列 http://www.cnblogs.com/Ricky81317/archive/2008/03/26/1122165.html#1122273 --------------------编程问答-------------------- 这个就是用到行转列了哦. --------------------编程问答-------------------- 参考一下行列转化的吧

http://zhidao.baidu.com/question/47480836.html --------------------编程问答-------------------- table
money     FEntryDC
500          0
1000         0
2000         1
3000         1

select a.money as 借,b.money as 贷
from
table a,table b
where
a.money=b.money and a.FEntryDC=0 and b.FEntryDC=1

表的子连接 --------------------编程问答--------------------
引用 2 楼 shinaterry 的回复:
ref: 典型的行变列 http://www.cnblogs.com/Ricky81317/archive/2008/03/26/1122165.html#1122273

\
\
学习ing --------------------编程问答-------------------- 还得有别的字段吧?要不借和贷怎么对应? --------------------编程问答--------------------

select id,
借=(select case FEntryDC[借/贷] when 0 then [借/贷] 
                               else null  
           end
    from tableA where id=a.id),
贷=(select case FEntryDC[借/贷] when 1 then [借/贷] 
                               else null  
           end
    from tableA where id=a.id) 
from tableA a
--------------------编程问答-------------------- sorry,FEntryDC[借/贷] 换成 FEntryDC --------------------编程问答-------------------- select (case  when FEntryDC=0 then  借 else "") 别名1,
        (case  when FEntryDC=1 then  贷  else "") 别名2
FROM TABLE
--------------------编程问答-------------------- 顶10楼的 --------------------编程问答-------------------- 10楼的方法不错 --------------------编程问答--------------------

create table # 
(
   FEntryDC int,
   FMoney int
)


insert into # select 1,1200
union
select 1,2400
union
select 0,1000

select (case  when FEntryDC=0 then  FMoney else '' end) as '借', 
        (case  when FEntryDC=1 then  FMoney  else '' end) as '货' 
FROM # 

补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,