请教入门问题

kang1616 2011-10-24 10:21:58
小弟最近在学习SQL2005 ,有个表查询统计不知道如何写了,请各位大侠指点下:
表名:1
区县 时间 销售金额
渝北 2011\08\20 200
渝北 2011\09\25 400
巴南 2011\08\25 400
巴南 2011\09\24 400
渝中 2011\08\24 400
渝中 2011\09\24 400


求各区县2011年8月的销售金额的合计,并按照金额大小排序
谢谢了

...全文
129 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangweitc123 2011-10-26
  • 打赏
  • 举报
回复
我也没转换时间
zhangweitc123 2011-10-26
  • 打赏
  • 举报
回复
select sum(销售金额)as total from 表名:1 where left(时间,7)='2011\08' group by 区县 order by SUM(销售金额) DESC
rmini 2011-10-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xuam 的回复:]
SQL code
select 区县,sum(销售金额) from 表1
where convert(char(7),时间,120) ='2011-08'
group by 区县
order by sum(销售金额) desc
[/Quote]

正解
rubingwoxin 2011-10-26
  • 打赏
  • 举报
回复
楼上的怎么都是在转换时间的格式啊,我觉得没必要那么麻烦
rubingwoxin 2011-10-26
  • 打赏
  • 举报
回复
select 区县,时间,sum(销售金额) from table
where substring(时间,1,7)='2011\08'
group by 区县,时间
order by sum(销售金额) desc
houyajin 2011-10-24
  • 打赏
  • 举报
回复

select 区县, sum(销售金额) from 表1 where convert(varchar(7),时间,120)='2011-08' group by 区县 order by SUM(销售金额) DESC

kang1616 2011-10-24
  • 打赏
  • 举报
回复
谢谢楼上各位大侠,小弟也学会这两招了
q465897859 2011-10-24
  • 打赏
  • 举报
回复
select 区县, convert(varchar(7),时间,23) 时间, sum(销售金额) 销售金额 from table1 group by 区县,convert(varchar(7),时间,23)
中国风 2011-10-24
  • 打赏
  • 举报
回复
還是這樣

use Tempdb
go


if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([区县] nvarchar(50),[时间] DATETIME,[销售金额] int)
Insert #T
select N'渝北',N'2011-08-20',200 union all
select N'渝北',N'2011-09-25',400 union all
--select N'巴南',N'2011-08-25',400 union all --去掉一條記錄
select N'巴南',N'2011-09-24',400 union all
select N'渝中',N'2011-08-24',400 union all
select N'渝中',N'2011-09-24',400
Go
select
区县,
时间=ISNULL(MIN(convert(varchar(7),时间,120)),'2011-08'),
销售金额=isnull(sum(销售金额) ,0)
from #T
WHERE convert(varchar(7),时间,120)='2011-08'
group by all 区县


/*
区县 时间 销售金额
-------------------------------------------------- ------- -----------
巴南 2011-08 0
渝中 2011-08 400
渝北 2011-08 200
*/



在8月沒有的区县也需要顯示為0時
中国风 2011-10-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 kang1616 的回复:]

引用 1 楼 roy_88 的回复:
SQL code
select 区县, 时间=convert(varchar(7),时间,120), 销售金额=sum(销售金额) frp, table1 group by 区县,convert(varchar(7),时间,120)


不好意思,不得行,
[/Quote]

use Tempdb
go


if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([区县] nvarchar(50),[时间] DATETIME,[销售金额] int)
Insert #T
select N'渝北',N'2011-08-20',200 union all
select N'渝北',N'2011-09-25',400 union all
select N'巴南',N'2011-08-25',400 union all
select N'巴南',N'2011-09-24',400 union all
select N'渝中',N'2011-08-24',400 union all
select N'渝中',N'2011-09-24',400
Go
DECLARE @s NVARCHAR(4000)
SELECT @s=ISNULL(@s+',',' select ')+QUOTENAME([区县])+N'=sum(case when [区县]=N'''+[区县]+N''' then [销售金额] else 0 end)' FROM #T GROUP BY [区县]

EXEC(@s+'
from #T
WHERE convert(varchar(7),时间,120)=''2011-08''
'
)
/*
巴南 渝中 渝北
----------- ----------- -----------
400 400 200
*/

中国风 2011-10-24
  • 打赏
  • 举报
回复
use Tempdb
go


if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([区县] nvarchar(50),[时间] DATETIME,[销售金额] int)
Insert #T
select N'渝北',N'2011-08-20',200 union all
select N'渝北',N'2011-09-25',400 union all
select N'巴南',N'2011-08-25',400 union all
select N'巴南',N'2011-09-24',400 union all
select N'渝中',N'2011-08-24',400 union all
select N'渝中',N'2011-09-24',400
Go
DECLARE @s NVARCHAR(4000)
SELECT @s=ISNULL(@s+',',' select ')+QUOTENAME([区县])+N'=sum(case when [区县]=N'''+[区县]+N''' then [销售金额] else 0 end)' FROM #T GROUP BY [区县]

EXEC(@s+'
from #T
WHERE convert(varchar(7),时间,120)=''2011-08''
group by 区县,convert(varchar(7),时间,120)'
)
/*
巴南 渝中 渝北
----------- ----------- -----------
400 0 0
0 400 0
0 0 200
*/

kang1616 2011-10-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 roy_88 的回复:]
SQL code
select 区县, 时间=convert(varchar(7),时间,120), 销售金额=sum(销售金额) frp, table1 group by 区县,convert(varchar(7),时间,120)
[/Quote]

不好意思,不得行,
--小F-- 2011-10-24
  • 打赏
  • 举报
回复
select
区县,sum(销售金额) from 表1
where
datediff(mm, 时间,'2011-08-01')=0
group by
区县
中国风 2011-10-24
  • 打赏
  • 举报
回复
樓主的這個2011\08\20格式是什麼語言環境
xuam 2011-10-24
  • 打赏
  • 举报
回复
select 区县,sum(销售金额)  from 表1  
where convert(char(7),时间,120) ='2011-08'
group by 区县
order by sum(销售金额) desc
xuam 2011-10-24
  • 打赏
  • 举报
回复
select 区县,sum(销售金额)  from 表1  
where convert(char(7),时间,120) ='2011-08'
group by 区县
myhaikuotiankong 2011-10-24
  • 打赏
  • 举报
回复
楼上英明。。。
中国风 2011-10-24
  • 打赏
  • 举报
回复
select 区县, 时间=convert(varchar(7),时间,120), 销售金额=sum(销售金额) from table1 group by 区县,convert(varchar(7),时间,120)
中国风 2011-10-24
  • 打赏
  • 举报
回复
select 区县, 时间=convert(varchar(7),时间,120), 销售金额=sum(销售金额) frp, table1 group by 区县,convert(varchar(7),时间,120)

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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