如何用sum函数求两个表数据的和

周淡 2012-05-17 05:56:06
//我要把当前表和历史表的数据相加求出来,我用了以下sql语句,但是发现求不出来。
SELECT count(pk1) as count1 FROM ftrans_info where user_id='2012050900000001' and tran_code in ('6110','6120','6130')
and tran_date between '2012-01-01' and '2012-05-17' and chk_status = 1 and status = 1
union
SELECT count(pk1) as count2 FROM ftrans_info_hst where user_id='2012050900000001' and tran_code in ('6110','6120','6130') and tran_date
between '2012-01-01' and '2012-05-17' and chk_status = 1 and status = 1


//以下是上述sql语句的输出结果

COUNT1
23
24

我如何把两个sql语句合并成一个输出,就是我的预想输出是
COUNT1
47
...全文
284 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
liqi_wj 2012-05-21
  • 打赏
  • 举报
回复
那就当楼主 心好 散分吧~~
周淡 2012-05-21
  • 打赏
  • 举报
回复
偶尔那个脑筋短路呗
zdj624489750 2012-05-18
  • 打赏
  • 举报
回复
此贴有意思!!程序员也蒙圈啊!!!!
jaq2006213 2012-05-18
  • 打赏
  • 举报
回复
count 和sum 分清楚了吗?
德薄才浅 2012-05-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

declare @count1,@count2 int

SELECT @count1 = count(pk1) as count1 FROM ftrans_info where user_id='2012050900000001' and tran_code in ('6110','6120','6130')
and tran_date between '2012-01-01' and……
[/Quote]
刚刚写错了!!
declare @count1 int
declare @count2 int
德薄才浅 2012-05-17
  • 打赏
  • 举报
回复
declare int @count1,@count2

SELECT @count1 = count(pk1) as count1 FROM ftrans_info where user_id='2012050900000001' and tran_code in ('6110','6120','6130')
and tran_date between '2012-01-01' and '2012-05-17' and chk_status = 1 and status = 1
union
SELECT @count2 = count(pk1) as count2 FROM ftrans_info_hst where user_id='2012050900000001' and tran_code in ('6110','6120','6130') and tran_date
between '2012-01-01' and '2012-05-17' and chk_status = 1 and status = 1

select @count1 + @count2
周淡 2012-05-17
  • 打赏
  • 举报
回复
是的,发完帖子我就发现自己2了。。。。


select sum(count1) from( SELECT count(pk1) as count1 FROM ftrans_info where user_id='2012050900000001' and tran_code in ('6110','6120','6130')
and tran_date between '2012-01-01' and '2012-05-17' and chk_status = 1 and status = 1
union
SELECT count(pk1) as count2 FROM ftrans_info_hst where user_id='2012050900000001' and tran_code in ('6110','6120','6130') and tran_date
between '2012-01-01' and '2012-05-17' and chk_status = 1 and status = 1)
就好了
forgetsam 2012-05-17
  • 打赏
  • 举报
回复
再套一层select count(*) from ()不就出来了。

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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