求SQL语句哦

yinrongg 2011-10-20 11:53:15
有如下表
编号 车号 货名 净重 计量时间
01 A01 货物A 1000 2011-1-20 11:14:01
02 A03 货物B 4000 2011-2-20 11:14:01
03 A03 货物C 3000 2011-3-20 11:14:01
04 A02 货物D 1000 2011-4-20 11:14:01
.
.
.
【注:每一行数据即为一车货物】

现在要统计成如下格式表
货名 1月份 车数 2月份 车数 3月份 车数 4月份 车数 5月份 车数 6月份 车数 ---至12月
货物A 10000 1 0 0 0 0 0 0 0 0 0 0
货物B 0 0 40000 1
货物C 0 0 0 0 3000 1
货物D 0 0 0 0 0 0 1000 1


目前已经实现了统计每个月重量,但是车数没实现。求解
...全文
79 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
唐诗三百首 2011-10-20
  • 打赏
  • 举报
回复
给1-4月统计的例子,其他月份自行添加喔.

create table tab(编号 varchar(3), 车号 varchar(5), 货名 varchar(6), 净重 int, 计量时间 datetime)

insert into tab
select '01','A01','货物A',1000,'2011-1-20 11:14:01' union all
select '02','A03','货物B',4000,'2011-2-20 11:14:01' union all
select '03','A03','货物C',3000,'2011-3-20 11:14:01' union all
select '04','A02','货物D',1000,'2011-4-20 11:14:01'

select 货名,
sum(case month(计量时间) when 1 then 净重 else 0 end) '1月份',
count(case month(计量时间) when 1 then 车号 else null end) '车数',
sum(case month(计量时间) when 2 then 净重 else 0 end) '2月份',
count(case month(计量时间) when 2 then 车号 else null end) '车数',
sum(case month(计量时间) when 3 then 净重 else 0 end) '3月份',
count(case month(计量时间) when 3 then 车号 else null end) '车数',
sum(case month(计量时间) when 4 then 净重 else 0 end) '4月份',
count(case month(计量时间) when 4 then 车号 else null end) '车数'
from tab
group by 货名

货名 1月份 车数 2月份 车数 3月份 车数 4月份 车数
------ ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
货物A 1000 1 0 0 0 0 0 0
货物B 0 0 4000 1 0 0 0 0
货物C 0 0 0 0 3000 1 0 0
货物D 0 0 0 0 0 0 1000 1
薇薇 2011-10-20
  • 打赏
  • 举报
回复


--> 生成测试数据: @T
DECLARE @T TABLE (日期 VARCHAR(4),工号 VARCHAR(4),加班小时 INT)
INSERT INTO @T
SELECT '周一','A001',6 UNION ALL
SELECT '周二','A001',2 UNION ALL
SELECT '周三','A001',1 UNION ALL
SELECT '周四','A001',5 UNION ALL
SELECT '周五','A001',7

select * from (SELECT 日期,工号,加班小时 FROM @T
UNION ALL
SELECT '总数',工号,SUM(加班小时) FROM @T GROUP BY 工号) as t
pivot(max(加班小时)for 日期 in([周一,[周二],[周三],[周四],[周五],[总数]))AS pvt]
AcHerat 2011-10-20
  • 打赏
  • 举报
回复
很多例子哦!CSDN上搜就有。
SqlServer2008 2011-10-20
  • 打赏
  • 举报
回复
写了一个很蹩脚的:


create table tb
(
编号 int,
车号 nvarchar(50),
货名 nvarchar(50),
净重 int,
计量时间 datetime

)

insert into tb(编号, 车号, 货名, 净重, 计量时间)
select '01','A01', '货物A', 1000, '2011-1-20 11:14:01'
union all
select '01','A01', '货物A', 1000, '2011-1-23 11:14:01'
union all
select '02','A02', '货物B', 4000, '2011-2-20 11:14:01'
union all
select '03','A03', '货物C', 3000, '2011-3-20 11:14:01'
union all
select '04','A04', '货物D', 1000, '2011-4-20 11:14:01'

select 货名,
[1月份]=(case when month(计量时间)=1 then 净重 else 0 end),
[2月份]=(case when month(计量时间)=2 then 净重 else 0 end),
[3月份]=(case when month(计量时间)=3 then 净重 else 0 end),
[4月份]=(case when month(计量时间)=4 then 净重 else 0 end),
[5月份]=(case when month(计量时间)=5 then 净重 else 0 end),
[6月份]=(case when month(计量时间)=6 then 净重 else 0 end),
[7月份]=(case when month(计量时间)=7 then 净重 else 0 end),
[8月份]=(case when month(计量时间)=8 then 净重 else 0 end),
[9月份]=(case when month(计量时间)=9 then 净重 else 0 end),
[10月份]=(case when month(计量时间)=10 then 净重 else 0 end),
[11月份]=(case when month(计量时间)=11 then 净重 else 0 end),
[12月份]=(case when month(计量时间)=12 then 净重 else 0 end)
into #tb
from tb

select 货名,
sum([1月份]) as [1月份],
车数=(select count(1) from #tb where 货名=t.货名 and [1月份]!=0),
sum([2月份]) as [2月份],
车数=(select count(1) from #tb where 货名=t.货名 and [2月份]!=0),
sum([3月份]) as [3月份],
车数=(select count(1) from #tb where 货名=t.货名 and [3月份]!=0),
sum([4月份]) as [4月份],
车数=(select count(1) from #tb where 货名=t.货名 and [4月份]!=0),
sum([5月份]) as [5月份],
车数=(select count(1) from #tb where 货名=t.货名 and [5月份]!=0),
sum([6月份]) as [6月份],
车数=(select count(1) from #tb where 货名=t.货名 and [6月份]!=0),
sum([7月份]) as [7月份],
车数=(select count(1) from #tb where 货名=t.货名 and [7月份]!=0),
sum([8月份]) as [8月份],
车数=(select count(1) from #tb where 货名=t.货名 and [8月份]!=0),
sum([9月份]) as [9月份],
车数=(select count(1) from #tb where 货名=t.货名 and [9月份]!=0),
sum([10月份]) as [10月份],
车数=(select count(1) from #tb where 货名=t.货名 and [10月份]!=0),
sum([11月份]) as [11月份],
车数=(select count(1) from #tb where 货名=t.货名 and [11月份]!=0),
sum([12月份]) as [12月份],
车数=(select count(1) from #tb where 货名=t.货名 and [12月份]!=0)
from #tb t group by 货名

drop table #tb

一缕青烟 2011-10-20
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20110427/14/8cf6ef38-c73f-43c3-aadb-a210c483b25e.html?12155
--小F-- 2011-10-20
  • 打赏
  • 举报
回复
只有12个月 可以穷举出来 毕竟动态SQL的效率不高

select
货名,
sum(case datepart(mm,计量时间) when 1 then 净重 else 0 end) '1月份',
sum(case datepart(mm,计量时间) when 1 then 车号 else null end) '车数',
sum(case datepart(mm,计量时间) when 2 then 净重 else 0 end) '2月份',
sum(case datepart(mm,计量时间) when 2 then 车号 else null end) '车数',
sum(case datepart(mm,计量时间) when 3 then 净重 else 0 end) '3月份',
sum(case datepart(mm,计量时间) when 3 then 车号 else null end) '车数',
sum(case datepart(mm,计量时间) when 4 then 净重 else 0 end) '4月份',
sum(case datepart(mm,计量时间) when 4 then 车号 else null end) '车数'
.....
from
tb
group by 货名

22,209

社区成员

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

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