字段拆分,在线等,给分

sid1001 2009-02-16 09:59:38
数据库里有一字段balance,根据他的正负值把它插入到不同的变量,或者select出来的时候根据正负值给它AS不同的别名,
balance
100
-199
188
-234
变成
dibit crd
100 -199
189 -234
...全文
103 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
luckyleaf521 2009-02-16
  • 打赏
  • 举报
回复
DECLARE @ta TABLE (balance INT) 
INSERT INTO @ta
SELECT 100 UNION ALL
SELECT -199 UNION ALL
SELECT 188 UNION ALL
SELECT -234

select t1.dibit,t2.crd from
(select row_number() over (order by getdate()) as r1 ,balance as dibit from @ta where balance >=0) t1
full join
(select row_number() over (order by getdate()) as r2 ,balance as crd from @ta where balance <0) t2
on t1.r1 = t2.r2

/*
dibit crd
----------- -----------
100 -199
188 -234
*/
sid1001 2009-02-16
  • 打赏
  • 举报
回复
能写清楚点吗,如果我想用一个select语句完成呢
-狙击手- 2009-02-16
  • 打赏
  • 举报
回复
;with t
as
(select balance as dibit,row_number() over (order by getdate()) from ta where balance > 0
union all
select balance as crd,row_number() over (order by getdate()) from ta where balance <= 0 )
select * from t;
htl258_Tony 2009-02-16
  • 打赏
  • 举报
回复
SQL2000要用到临时表,一条语句不好实现.
lxf2000104 2009-02-16
  • 打赏
  • 举报
回复
6楼不行,
100 NULL
NULL -199
188 NULL
NULL -234
NULL -234
返回是这样的结果
bushy 2009-02-16
  • 打赏
  • 举报
回复
Select case when balance>=0 then balance end as dibit,
case when balance<0 then balance end as crd
from balance

这种思路试试!
lxf2000104 2009-02-16
  • 打赏
  • 举报
回复
3楼正解
PB菜鸟 2009-02-16
  • 打赏
  • 举报
回复
row_number() OVER?

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧