求一SQL语句????~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

HELLO_JIM 2010-05-17 04:01:10
表内容 table1:
号码 件数 金额
a 2 1
a 2 2
a 2 5
b 1 2
b 1 1

查询要显示结果:
票数 件数 金额
----------- ---------- --------------
2 3 11


即相同的号码算一票 ,件数根据票数来,如上,a号码算2件,b号码算1件,加起来为3件,而金额全部相加,为11
...全文
115 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
HELLO_JIM 2010-05-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 chuifengde 的回复:]
SQL code
还是错了,
select count(distinct 号码) 号码,sum(distinct 件数) 件数,sum(金额) 金额
from [Table]
[/Quote]


当件数都相同时,例如都为1,则查询结果就不对
youyou2404 2010-05-17
  • 打赏
  • 举报
回复
--> 测试数据: #table1
if object_id('tempdb.dbo.#table1') is not null drop table #table1
create table #table1 (号码 varchar(1),件数 int,金额 int)
insert into #table1
select 'a',2,1 union all
select 'a',2,2 union all
select 'a',2,5 union all
select 'b',1,2 union all
select 'b',1,1

select count(distinct 号码),sum(件数),Sum(金额)
from (
select 号码,sum(distinct 件数) 件数,sum(金额) 金额
from #table1
group by 号码
) t

(5 行受影响)

----------- ----------- -----------
2 3 11

(1 行受影响)
youyou2404 2010-05-17
  • 打赏
  • 举报
回复
--> 测试数据: #table1
if object_id('tempdb.dbo.#table1') is not null drop table #table1
create table #table1 (号码 varchar(1),件数 int,金额 int)
insert into #table1
select 'a',1,1 union all
select 'a',1,2 union all
select 'a',1,5 union all
select 'b',1,2 union all
select 'b',1,1

select count(distinct 号码),sum(件数),Sum(金额)
from (
select 号码,sum(distinct 件数) 件数,sum(金额) 金额
from #table1
group by 号码
) t

----------- ----------- -----------
2 2 11

(1 行受影响)
HELLO_JIM 2010-05-17
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 youyou2404 的回复:]
SQL code
--> 测试数据: #table1
if object_id('tempdb.dbo.#table1') is not null drop table #table1
create table #table1 (号码 varchar(1),件数 int,金额 int)
insert into #table1
select 'a',2,1 union all
selec……
[/Quote]
芬雨无阻,假如我的件数都为1呢,那查询出来的结果就不对了,应该是2,但这样的话查询出来就为1
zhengtw 2010-05-17
  • 打赏
  • 举报
回复
SELECT COUNT(DISTINCT(col1)),SUM(DISTINCT(col2)),SUM(col3)
FROM table1
youyou2404 2010-05-17
  • 打赏
  • 举报
回复
--> 测试数据: #table1
if object_id('tempdb.dbo.#table1') is not null drop table #table1
create table #table1 (号码 varchar(1),件数 int,金额 int)
insert into #table1
select 'a',2,1 union all
select 'a',2,2 union all
select 'a',2,5 union all
select 'b',1,2 union all
select 'b',1,1

select count(distinct 号码) 号码,sum(distinct 件数) 件数,sum(金额) 金额
from #table1
号码 件数 金额
----------- ----------- -----------
2 3 11

(1 行受影响)
htl258_Tony 2010-05-17
  • 打赏
  • 举报
回复
a号码算2件,b号码算1件,加起来为3件

怎么算的
chuifengde 2010-05-17
  • 打赏
  • 举报
回复
还是错了,
select count(distinct 号码) 号码,sum(distinct 件数) 件数,sum(金额) 金额
from [Table]
htl258_Tony 2010-05-17
  • 打赏
  • 举报
回复
select count(distinct 号码) 票数,count(distinct 金额) 件数,sum(金额) 金额
from tb
?
youyou2404 2010-05-17
  • 打赏
  • 举报
回复
数根据票数来,如上,a号码算2件,b号码算1件, 啥意思
chuifengde 2010-05-17
  • 打赏
  • 举报
回复
select count(distinct 号码) 号码,count(distinct 件数) 件数,sum(金额) 金额
from [Table]
chuifengde 2010-05-17
  • 打赏
  • 举报
回复
select sum(distinct 号码) 号码,sum(distinct 件数) 件数,sum(金额) 金额
from [Table]

34,576

社区成员

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

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