你的语句好象有问题,上月的最高成本没有和车型关联。
我写了一下:
select a.pmonth,a.vid,a.vc_max,a.vc_min,a.vc_mix,b.poutput,
(a.vc_max-c.vc_max)*b.poutput cbrmaxprice,//降成本额
((a.vc_max-c.vc_max)*b.poutput)/a.vc_max maxprice, //降成本率
((a.vc_max-c.vc_max)*b.poutput)/sum((a.vc_max/c.vc_max*b.poutput)) //降成本权重
from c_monthvc a ,
monthoutput b ,
(select * from c_monthvc where pmonth='200411') c
where a.vid=b.vid and a.vid=c.vid and a.pmonth='200412'
;
我写了一下:
select a.pmonth,a.vid,a.vc_max,a.vc_min,a.vc_mix,b.poutput,
(a.vc_max-c.vc_max)*b.poutput cbrmaxprice,//降成本额
((a.vc_max-c.vc_max)*b.poutput)/a.vc_max maxprice, //降成本率
((a.vc_max-c.vc_max)*b.poutput)/sum((a.vc_max/c.vc_max*b.poutput)) //降成本权重
from c_monthvc a ,
monthoutput b ,
(select * from c_monthvc where pmonth='200411') c
where a.vid=b.vid and a.vid=c.vid and a.pmonth='200412'
;
我想这里只要通过一个子查询算出所有车型总降成本额,问题就差不多解决了。
提醒你一下,你在那里写sum()肯定是不行的。
还有这条语句select vc_max from c_monthvc where pmonth='200411'得出的是多个值,你用一个数字去减它怎么行? Select
A.pmonth,
A.vid,
A.vc_max,
A.vc_min,
A.vc_mix,
A.poutput,
(isnull(A.vc_Max,0)-isnull(B.vc_Max,0))*C.pOutPut cbrmaxprice, --降成本额
(isnull(A.vc_Max,0)-isnull(B.vc_Max,0))*C.pOutPut/A.vc_max cblmaxprice, --降成本率
((isnull(A.vc_Max,0)-isnull(B.vc_Max,0))*C.pOutPut)/ D.Total)--降成本权重
From c_MonthVC A,--当月
c_MonthVC B,--上月
c_MonthOutPut C,
(Select sum((isnull(A.vc_Max,0)-isnull(B.vc_Max,0))*C.pOutPut) Total
From c_MonthVC A,--当月
c_MonthVC B,--上月
c_MonthOutPut C
Where A.VID = B.VID
And A.VID = C.VID
And Datediff(Month,A.pMonth,B.pMonth) = -1
And Datediff(Month,A.pMonth,C.pMonth) = 0
And Datediff(Month,A.pMonth,'2004-12-11') = 0
) D
Where A.VID = B.VID
And A.VID = C.VID
And A.VID = D.VID
And Datediff(Month,A.pMonth,B.pMonth) = -1
And Datediff(Month,A.pMonth,C.pMonth) = 0
And Datediff(Month,A.pMonth,'2004-12-11') =0
select a.pmonth,a.vc_max(日期,部门) from table;vc_max 是你写的函数,返回最大成本
具体怎么写自己想吧