邹建,在问你一种情况?关于union 、Sum问题!

grey_contrail 2004-03-29 02:27:46
表如一张表中,有A、B、C三个字段,
a b 10
b c 20
c b 10


我要 A、B、C
a b 10
b c 20
'' '' 30

Select top 2 A,B,C from ABC
union all
select '','',sum(c) from ABC


而结果是 A、B、C
a b 10
a c 20
'' '' 40

...全文
91 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 2004-03-29
  • 打赏
  • 举报
回复
--测试数据
create table ABC(a char(1),b char(1),c int)
insert ABC select 'a','b',10
union all select 'b','c',20
union all select 'c','b',10
go

--查询
Select top 2 A,B,C from ABC
union all
select '','',sum(c) from(
select top 2 a,b,c from ABC
)a
go

--删除测试
drop table ABC

/*--测试结果
A B C
---- ---- -----------
a b 10
b c 20
30

(所影响的行数为 3 行)
--*/
victorycyz 2004-03-29
  • 打赏
  • 举报
回复
Select top 2 A,B,C from ABC
union all
select '','',sum(c) from (Select top 2 A,B,C from ABC) a
pengdali 2004-03-29
  • 打赏
  • 举报
回复
Select top 2 A,B,C from ABC
union all
select '','',sum(c) from (select top 2 from ABC) a
grey_contrail 2004-03-29
  • 打赏
  • 举报
回复
有人吗?

22,298

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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