求SQL 语句

yinrongg 2011-10-20 11:18:33
有如下表
编号 车号 货名 净重 计量时间
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
求SQL语句
...全文
136 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
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
yinrongg 2011-10-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 sqlserver2008 的回复:]

不知道楼主车数是怎么算的
[/Quote] 车数就是count(编号),每一行就是一车
SqlServer2008 2011-10-20
  • 打赏
  • 举报
回复
不知道楼主车数是怎么算的
SqlServer2008 2011-10-20
  • 打赏
  • 举报
回复

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)
from tb


yinrongg 2011-10-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 dongxinxi 的回复:]

经典的行列转换,去SQL区找下推荐已结的帖子
[/Quote] 我貌似发错地方了
  • 打赏
  • 举报
回复
经典的行列转换,去SQL区找下推荐已结的帖子
yinrongg 2011-10-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wknight_it 的回复:]

行列转换
[/Quote] 本人不才,只能实现数据统计,车数统计没实现
bdmh 2011-10-20
  • 打赏
  • 举报
回复
自己写代码(或存储过程)转换
SQL777 2011-10-20
  • 打赏
  • 举报
回复
[Quote=引用楼主 ling3wei 的回复:]
有如下表
编号 车号 货名 净重 计量时间
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
.
.
.


现在要统计成如下……
[/Quote]
车数也可以SUM(CASE WHEN 月分=1 THEN 1 ELSE 0) AS 车数
buzhidao945 2011-10-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 sqlserver2008 的回复:]
SQL code


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 ……
[/Quote]
1101框架 2011-10-20
  • 打赏
  • 举报
回复
类型:
不转变
栏目标题
栏目数据
1101框架 2011-10-20
  • 打赏
  • 举报
回复
我在C#里面做的,设置参数:
行转成栏目
栏目|类型|分组汇总|顺序
xiaoyaoxiaowei 2011-10-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 sqlserver2008 的回复:]
SQL code


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 ……
[/Quote]
nikolaichow 2011-10-20
  • 打赏
  • 举报
回复
上次写了一个行列转换,用的SQL封装,用CASE WHEN要知道列的值,搞12个月份可以的,若要变成其他值,就难了。封装SQL语句,则可以,给你看给原来写的方法,你修改修改即可:
set nocount on --不显示执行消息

--方法一:使用Case When 直接通过SQL查询
declare @AreaCode varchar(30),@Year int,@i int,@count int,@Add int,@sql varchar(3000),@FieldName varchar(30),@FinsId int,
@sqlEntry varchar(300),@sqlHeader varchar(300)
set @AreaCode='320602' --条件值
set @Year=2010 --条件值

select @count=count(distinct id) from XIANZHONG --查询险种的总数量
set @i=len(@AreaCode)+2 --设置下级区域code长度
set @Add=1 --用于While中的动态值

--封装主体SQL,将,@sqlHeader和,@sqlEntry预留位置等待替换
set @sql='select 区域,@sqlHeader
from (select b.NAME 区域,@sqlEntry from
(select substring(D.IDCode,1,'+cast(@i as varchar)+') FAreaCode,X.Id AS FInsID ,X.Name AS FinsName,sum(Sl) FQTY,sum(JE) FAmount
from CANBAOXINXI C left join NONGHU N on C.Nonghu_id=N.Id
left join DIQU D on N.DQID=D.ID
left join XIANZHONG X ON X.Id=C.xianzhong_id
where D.IDCODE like '''+@AreaCode+'%'' and C.create_time in ('+cast(@Year as varchar) +')
group by substring(D.IDCode,1,'+cast(@i as varchar)+'),X.Id,X.Name) a inner join DIQU b on a.FAreaCode=b.IDCODE
group by b.NAME,a.FInsID) a
group by 区域'

while (@Add<=@count)
begin
select @FinsId=id,@FieldName=name
from (select id ,NAME,ROW_NUMBER() OVER (ORDER BY id) AS FOrderID from XIANZHONG) a
where FOrderID=@Add
set @Add=@Add+1
--拼装@sqlEntry与@sqlHeader的语句
set @sqlEntry=',sum(case when finsid='+cast(@FinsId as varchar)+' then FQty end) AS '+@FieldName+'数量,
sum(case when finsid='+cast(@FinsId as varchar)+' then FAmount end) AS '+@FieldName+'金额,@sqlEntry'
set @sqlHeader=',sum('+@FieldName+'数量) '+@FieldName+'数量,sum('+@FieldName+'金额) '+@FieldName+'金额,@sqlHeader'
--替换主体@sql中@sqlEntry与@sqlHeader位置的语句
select @sql=REPLACE(@sql,',@sqlEntry',@sqlEntry)
select @sql=REPLACE(@sql,',@sqlHeader',@sqlHeader)
end
--去除最后主体@sql中@sqlEntry与@sqlHeader位置的语句
select @sql=REPLACE(@sql,',@sqlEntry','')
select @sql=REPLACE(@sql,',@sqlHeader','')
--想查看@SQL语句的请直接使用
--select @sql
exec(@sql) --执行SQL语句
set nocount Off

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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