[急]请教sql语句,关于分类汇总的(50分)

  • 主题发起人 主题发起人 kofoun
  • 开始时间 开始时间
K

kofoun

Unregistered / Unconfirmed
GUEST, unregistred user!
关于sqlserver2000数据库查询语句的问题:
现时有一个如下的表table1,表结构如下:
-------------------------------------
fid int identity not null primary key
fdate datetime //日期
fline varchar(20) //生产线
fmaster varchar(20) //主任
fproductname varchar(60) //产品名称
fgoodscount int //成品数量
fprojectname varchar(60) //工程名称
fcheckcount int //检查总数
fcheckokcount int //合格数
fcheckproject varchar(60) //检查项目
fcheckng int //不合格数
foutcount int //出库数量
fperson int //人数
fdailytime numeric(5,1) //正常时间
fovertime numeric(5,1) //加班时间
ftotaltime numeric(5,1) //总时间

===============================
现时我按fdate,fline,fproductname分类汇总如下:

select fdate,fline,fproductname,sum(fgoodscount)goods,sum(fcheckng)ng,sum(fdailytime)dailytime,sum(fovertime)overtime,sum(ftotaltime)totaltime from bdailytable group by fdate,fline,fproductname

得出fgoodscount(goods),fcheckng(ng),fdailytime(daily),fovertime(over),ftotaltime(totaltime)总数
---------------------------------------------------------------------------------------
fdate fline fproductname goods ng daily over totaltime
2006-07-03 A5 HDR2150-150 8000 900 416.0 104.0 520.0
2006-07-04 A5 HDR2150-150 9000 829 488.0 122.0 610.0
2006-07-05 A5 HDR2150-150 10000 4 .0 .0 .0
2006-07-06 A5 HDR2150-150 NULL 106 184.0 46.0 230.0
2006-07-12 A5 HDR2150-150 10000 7 .0 .0 .0
2006-07-16 A5 HDR2150-150 7000 527 456.0 .0 456.0
2006-07-20 A1 HDR2150-042 500 30 40.0 10.0 50.0

问题1:我想在此查询结果中最右边增加一,列1: 显示(ng/(ng+goods)*100%)的百分比
fdate fline fproductname goods ng daily over totaltime 列1
2006-07-03 A5 HDR2150-150 8000 900 416.0 104.0 520.0 11.12%
2006-07-04 A5 HDR2150-150 9000 829 488.0 122.0 610.0 8.43%
2006-07-05 A5 HDR2150-150 10000 4 .0 .0 .0 0.03%
2006-07-06 A5 HDR2150-150 NULL 106 184.0 46.0 230.0 100%
2006-07-12 A5 HDR2150-150 10000 7 .0 .0 .0 0.06%
2006-07-16 A5 HDR2150-150 7000 527 456.0 .0 456.0 7.00%
2006-07-20 A1 HDR2150-042 500 30 40.0 10.0 50.0 5.66%

问题2:再在此查询中,再统计每个fproductname的总分类明细,想得出如下结果:
fgoods goods ng (ng/(ng+goods))
HDR2150-150 44000 2373 5.11% (最要小数点后两位)
HDR2150-042 500 30 5.66%
 
方法一,使用视图
将select fdate,fline,fproductname,sum(fgoodscount)goods,sum(fcheckng)ng,sum(fdailytime)dailytime,sum(fovertime)overtime,sum(ftotaltime)totaltime from bdailytable group by fdate,fline,fproductname
结果作为视图的生成条件
那么你的问题1问题2相当于对视图进行操作

方法二,使用临时表
Create一个你用Select语句查询结果的表
然后按照你的Select语句将内容插入表
然后对这个表进行操作
 
select a.fdate,a.fline,a.fproductname,a.goods,a.ng,a.dailytime,a.overtime,
a.totaltime,a.ng/(a.ng+a.goods)*100 bb from
(select fdate,fline,fproductname,sum(fgoodscount)goods,sum(fcheckng)ng,sum(fdailytime)dailytime,sum(fovertime)overtime,sum(ftotaltime)totaltime from bdailytable group by fdate,fline,fproductname) a
 
楼上:
a.ng/(a.ng+a.goods)*100 bb
为何这个计算不出来结果是:0
 
注意,应该是:(1.0*a.ng/(a.ng+a.goods))*100
 
select fdate,fline,fproductname,sum(fgoodscount)goods,sum(fcheckng)ng,sum(fdailytime)dailytime,sum(fovertime)overtime,sum(ftotaltime)totaltime,
sum(fcheckng)/(sum(fcheckng)+sum(fgoodscount))*100) from bdailytable group by fdate,fline,fproductname

楼主能够保证每个字段都没有null的么?
直接写在里面,看有没有结果?
对于保留百分数,自己写个函数就行了
 
想必没有null,否则的话返回的就是null而不是0了,不过加上isnull的判断还是必要的,嘿嘿!
 
select a.fdate,a.fline,a.fproductname,a.goods,a.ng,a.dailytime,a.overtime,
a.totaltime,1.0*isnull(a.ng,0)/(isnull(a.ng,0)+isnull(a.goods,0)))*100 bb from
(select fdate,fline,fproductname,sum(fgoodscount)goods,sum(fcheckng)ng,sum(fdailytime)dailytime,sum(fovertime)overtime,sum(ftotaltime)totaltime from bdailytable group by fdate,fline,fproductname) a
 
我想在列1只要两位小数位,怎么办,并加个'%'号
 
将123.123改为你的字段表达式即可,格式为:left(cast(round(123.123,2) as varchar),CHARINDEX('.',cast(round(123.123,2) as varchar))+2)+'%'
 
create function f_format(@m numeric(8,4))
returns varchar(20)
begin
declare @n numeric(10,2)
declare @s varchar(20)
select @n=@m*100
select @s=convert(varchar,@n)+'%'
return @s
end

上面函数就可以。
 
hityou
这个函数怎么用?如何放在我实现的语句中
 
那个函数f_format,放在我的语句中,本来是11.504424778700
结果变成:1150.44%
哪个地方出错了
 
可不可以解释一下那个自定义函数用法,小弟不是很熟悉函数
 
我写的那个就是一个转换为百分数的自定义函数,比如1转换为100。00%
如果你要想把11.504424778700转为11.50%的话,可以把那个*100去掉就是了。
create function f_format(@m numeric(8,4))
returns varchar(20)
begin
declare @n numeric(10,2)
declare @s varchar(20)
select @n=@m
select @s=convert(varchar,@n)+'%'
return @s
end

自定义函数跟一般的函数没什么区别,只是为了你特定の需要而写的,
 
后退
顶部