SQL中一个表中几条数据怎么合成一条!?

wolftomb 2005-03-18 09:35:44
比如 0表示old 1表示new
NO count oldnew
001 1 0
001 2 0
001 2 1
002 1 1
002 2 1
002 2 0

我想要的结果是(首先要以NO分组,然后又要把oldnew分离)
No count oldcount newcount
001 5 3 2
002 5 2 3
...全文
147 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhaozhaozhaozhao 2005-03-18
  • 打赏
  • 举报
回复
select no,count=sum(count),oldcount=sum(case when oldnew=0 then count end),
newcount=sum(case when oldnew=1 then count end) from 表 group by no
xluzhong 2005-03-18
  • 打赏
  • 举报
回复
select No,
[count]=sum([count]),
oldcount=sum(case oldnew when 1 then [count] end),
newcount=sum([count])-sum(case oldnew when 1 then [count] end)
from 表 group by NO
子陌红尘 2005-03-18
  • 打赏
  • 举报
回复
select
No,
count = sum([count]),
oldcount = sum(case when oldnew = 0 then [count] else 0 end),
newcount = sum(case when oldnew = 0 then [count] else 0 end)
from
t
group by
No
order by
No
paoluo 2005-03-18
  • 打赏
  • 举报
回复
修改
select No,
count=SUM(count),
oldcount=SUM(Case oldnew when 0 then [count] End),
newcount=SUM(Case oldnew when 1 then [count] End)
from 表 Group by NO

paoluo 2005-03-18
  • 打赏
  • 举报
回复
select No,
count=SUM(count),
oldcount=SUM(Case oldnew when 1 then [count] end),
newcount=SUM(Case oldnew when 0 then [count] end)
from 表 group by NO
lxysjl 2005-03-18
  • 打赏
  • 举报
回复
接分。
wyb0026 2005-03-18
  • 打赏
  • 举报
回复
我还以为能抢一楼呢?还是大哥们厉害
wyb0026 2005-03-18
  • 打赏
  • 举报
回复
select no sum(count) count ,
sum(case when oldnew=0 then count else 0 end) oldcount
sum(case when oldnew=1 then count else 0 end) newcount
from table
group by no
zjcxc 元老 2005-03-18
  • 打赏
  • 举报
回复
--写反了,改一下:

select No,
[count]=sum([count]),
oldcount=sum(case oldnew when 0 then [count] end),
newcount=sum([count])-sum(case oldnew when 0 then [count] end)
from 表 group by NO
jinjazz 2005-03-18
  • 打赏
  • 举报
回复
列标志写反了,修改

--建立测试环境
Create Table 表(NO varchar(10),count integer,oldnew integer)
--插入数据
insert into 表
select '001','1','0' union
select '001','2','0' union
select '001','2','1' union
select '002','1','1' union
select '002','2','1' union
select '002','2','0'
select * from 表
--测试语句
select [no],sum([count])[count],sum(case when oldnew=1 then [count] else 0 end)[newcount],
sum(case when oldnew=0 then [count] else 0 end)[oldcount]
from 表
group by [no]

--删除测试环境
Drop Table 表
zjcxc 元老 2005-03-18
  • 打赏
  • 举报
回复
select No,
[count]=sum([count]),
oldcount=sum(case oldnew when 1 then [count] end),
newcount=sum([count])-sum(case oldnew when 1 then [count] end)
from 表 group by NO
jinjazz 2005-03-18
  • 打赏
  • 举报
回复
--建立测试环境
Create Table 表(NO varchar(10),count integer,oldnew integer)
--插入数据
insert into 表
select '001','1','0' union
select '001','2','0' union
select '001','2','1' union
select '002','1','1' union
select '002','2','1' union
select '002','2','0'
select * from 表
--测试语句
select [no],sum([count])[count],sum(case when oldnew=1 then [count] else 0 end)[oldcount],
sum(case when oldnew=0 then [count] else 0 end)[newcount]
from 表
group by [no]

--删除测试环境
Drop Table 表

34,838

社区成员

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

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