有难度的SQL语句,解决后就给分。

liaoxing 2003-11-05 11:21:06
我的数据库如下
收费记录表:
车道号 车类名称 ......
来一辆车就把 车道号 车类名称和其他信息记录到收费记录表现在就是如何生成如下表
车道号1 车道号2 .........

车类1 数量 数量
车类2 数量 数量
....
....
望高手指教!
...全文
38 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhxr2003 2003-11-07
  • 打赏
  • 举报
回复
你用的什么数据库?可用交叉表或游标解决的.MYQQ57143540
dxy207 2003-11-07
  • 打赏
  • 举报
回复
select 车道号,车类,SUM(车类数量),SUM(费用)
FROM 收费记录表
GROUP BY 车道号,车类
liaoxing 2003-11-07
  • 打赏
  • 举报
回复
怎么没人回答呀是不是太难呀上面的都试过了,都有问题呀。望各位大侠帮帮我吧,在线等。拜托了。
zjcxc 元老 2003-11-05
  • 打赏
  • 举报
回复
--如果车道号很多,要改用下面的方法,防止生成的字符串超过8000,从而出错

declare @sqlhead varchar(8000),@sqlend varchar(8000)
,@sql1 varchar(8000),@sql2 varchar(8000),@sql3 varchar(8000),@sql4 varchar(8000)
,@i int,@ic varchar(20)

--生成数据处理临时表
select id=identity(int,0,1),gid=0
,a=',['+车道号+']=sum(case 车道号 when '''
+车道号+''' then 数量 else 0 end)'
into # from(select distinct 车道号 from 表) a

--判断需要多少个变量来处理
select @i=max(len(a)) from #
set @i=7800/@i

--分组临时表
update # set gid=id/@i
select @i=max(gid) from #

--生成数据处理语句
select @sqlhead='''select 车类名称'''
,@sqlend=''' from 表 group by 车类名称'''
,@sql1='',@sql2='select ',@sql3='',@sql4=''

while @i>=0
select @ic=cast(@i as varchar),@i=@i-1
,@sql1='@'+@ic+' varchar(8000),'+@sql1
,@sql2=@sql2+'@'+@ic+'='''','
,@sql3='select @'+@ic+'=@'+@ic+'+a from # where gid='+@ic
+char(13)+@sql3
,@sql4=@sql4+',@'+@ic

select @sql1='declare '+left(@sql1,len(@sql1)-1)+char(13)
,@sql2=left(@sql2,len(@sql2)-1)+char(13)
,@sql3=left(@sql3,len(@sql3)-1)
,@sql4=substring(@sql4,2,8000)

--执行
exec( @sql1+@sql2+@sql3+'
exec('+@sqlhead+'+'+@sql4+'+'+@sqlend+')'
)

--删除临时表
drop table #
zjcxc 元老 2003-11-05
  • 打赏
  • 举报
回复
--交叉表
declare @sql varchar(8000)
set @sql='select 车类名称'
select @sql=@sql+',['+车道号+']=sum(case 车道号 when '''+车道号+''' then 数量 end)' from (select distinct 车道号 from 表) a
exec(@sql+' from 表 group by 车类名称')
lvltt 2003-11-05
  • 打赏
  • 举报
回复
学习
sunshareforever 2003-11-05
  • 打赏
  • 举报
回复
对不起!忘记一句话

select 车类名称,count(*) as 车道1 from t_表 a group by 车类名称 where 车道号 = 1
left join
select count(*) as 车道2 from t_表 b group by 车类名称 where 车道号 = 2

on a.车类名称 = b.车类名称

......
sunshareforever 2003-11-05
  • 打赏
  • 举报
回复
select 车类名称,count(*) as 车道1 from t_表 group by 车类名称 where 车道号 = 1
left join
select count(*) as 车道2 from t_表 group by 车类名称 where 车道号 = 2
......
pengdali 2003-11-05
  • 打赏
  • 举报
回复
举例:

create table #a(部门 varchar(100),种类 varchar(100),数量 int)
insert #a values('n1','aa',11)
insert #a values('n1','ba',11)
insert #a values('n2','bb',1)
insert #a values('n2','aa',45)
insert #a values('n3','cc',81)
insert #a values('n3','aa',11)
insert #a values('n3','ba',561)
insert #a values('n3','bb',14)

declare @sql varchar(8000)
set @sql = 'select 部门'
select @sql = @sql + ',sum(case 种类 when '''+种类+''' then 数量 else 0 end) ['+种类+'数量]'
from (select distinct 种类 from #a) a

select @sql =@sql+' from #a group by 部门'

exec(@sql)

drop table #a
pengdali 2003-11-05
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql = 'select 车类名称'
select @sql = @sql + ',sum(case 车道号 when '''+cast(车道号 as varchar(10))+''' then 数量 else 0 end) [车道号'+cast(车道号 as varchar(10))+']'
from (select distinct 车道号 from 你的表) as a
select @sql = @sql+' from 你的表 group by 车类名称'

exec(@sql)
go
liaoxing 2003-11-05
  • 打赏
  • 举报
回复
zhangzhaoh(zzh) 没用呀,数量 是什么意思呀。
zhangzhaoh 2003-11-05
  • 打赏
  • 举报
回复
select 车类,
sum(case when 车道号 = 值1 then 数量 else 0 end) 车道号1,
sum(case when 车道号 = 值2 then 数量 else 0 end) 车道号2,
sum(case when 车道号 = 值3 then 数量 else 0 end) 车道号3,
sum(case when 车道号 = 值4 then 数量 else 0 end) 车道号4,
........
from table
liaoxing 2003-11-05
  • 打赏
  • 举报
回复
sunshareforever(阳光)你的程序运行时 提示left join附近有错了谁知道错在哪里吗?
liaoxing 2003-11-05
  • 打赏
  • 举报
回复
这张表就是查询出来的结果请问pengdali(大力 V3.0) 上面的SQL程序看不懂能解释一下吗?对了我是想把查询的结果放在DATAREPORT中去打印,这段程序放在VB程序的哪里了?
liaoxing 2003-11-05
  • 打赏
  • 举报
回复
在VB中要把这个表放到DATAREPORT中去打印请问又该怎么做了.
pengdali 2003-11-05
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql = 'select 车类名称'
select @sql = @sql + ',sum(case 车道号 when '''+cast(车道号 as varchar(10))+''' then 数量 else 0 end) [车道号'+cast(车道号 as varchar(10))+']'
from (select distinct 车道号 from 你的表) as a
select @sql = @sql+' into 你要的新表 from 你的表 group by 车类名称'

exec(@sql)
go

select * from 你要的新表
liaoxing 2003-11-05
  • 打赏
  • 举报
回复
这是个道路收费系统,有6个车道,9种车的种类。要做一张表统计每个车道的每种车类的数量另外还有对应的费用。望各位高手帮帮小弟。先谢谢了。希望讲的详细点,在下水平有限。

34,874

社区成员

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

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