■■求sql的对列的统计■■

l285791700 2009-05-05 05:04:16
如下table表

id price(价格) name
1 20 一部
2 25 一部
3 10 二部
4 5 三部
。。。。。。

想实现如下查询结果:

name total(总计)
一部 45
二部 10
三部 5
。。。。。

就是说查询table表里有关于"一部"的所有价格(price)的总和 和 "二部"的所有价格(price)的总和等等这些数值的列表
如何用SQL语言表示。。
谢谢!

...全文
68 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
SQL77 2009-05-05
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 SQL77 的回复:]
select t.name,max(case when total>22 then total else 'null' end) as total
from
(select [name],total=sum(price) from tb group by [name]) as t
group by t.name
[/Quote]


declare @t table(id int,price decimal(4,2),name varchar(20))

insert into @t
select '1','20','一部'
union all select '2','25','一部'
union all select '3','10','二部'
union all select '4','5','三部'

--select [name],total=sum(price) from @t group by [name]

select t.[name],max(case when total>22 then total else null end) as total
from
(select [name],total=sum(price) from @t group by [name]) as t
group by t.[name]
order by t.[name] desc


一部 45.00
三部 NULL
二部 NULL
you_tube 2009-05-05
  • 打赏
  • 举报
回复
select [name],total=sum(price) from tb group by [name]
SQL77 2009-05-05
  • 打赏
  • 举报
回复
select t.name,max(case when total>22 then total else 'null' end) as total
from
(select [name],total=sum(price) from tb group by [name]) as t
group by t.name
震区 2009-05-05
  • 打赏
  • 举报
回复

declare @t table(id int,price decimal(4,2),name varchar(20))

insert into @t
select '1','20','一部'
union all select '2','25','一部'
union all select '3','10','二部'
union all select '4','5','三部'

select name,
case
when sum(isnull(price,0)) > 22 then sum(isnull(price,0))
else null
end
from @t
group by name
order by sum(isnull(price,0)) desc
l285791700 2009-05-05
  • 打赏
  • 举报
回复
如下table表

id price(价格) name
1 20 一部
2 25 一部
3 30 二部
4 5 三部
。。。。。。



如果加入判断呢?比如总计大于22以上的所有结果呢?

想实现如下查询结果:

name total(总计)
一部 25
二部 30
三部 null
。。。。。


谢谢!
震区 2009-05-05
  • 打赏
  • 举报
回复
declare @t table(id int,price decimal(4,2),name varchar(20))

insert into @t
select '1','20','一部'
union all select '2','25','一部'
union all select '3','10','二部'
union all select '4','5','三部'

select name,
sum(isnull(price,0))
from @t
group by name
order by sum(isnull(price,0)) desc
wxg22526451 2009-05-05
  • 打赏
  • 举报
回复
select [name],total=sum(price) from tb group by [name]
zuolina257 2009-05-05
  • 打赏
  • 举报
回复
select name,sum(price) as 统计 total from th group by name
jia_guijun 2009-05-05
  • 打赏
  • 举报
回复
select name,sum(price) total from tb group by name
lgx0914 2009-05-05
  • 打赏
  • 举报
回复
select name ,sum(price) as 总计 from table group by name 
水族杰纶 2009-05-05
  • 打赏
  • 举报
回复
SELECT  name ,SUM(price)price FROM TB GROUP BY NAME

22,210

社区成员

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

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