表中列
fgysbm(供应商编码),fsj(时间用来进行排序),fcpbm(产品编码),fdj(单价),fsl(数量)
可能的数据
001 2008-05-02 010101 0.12 126
001 2008-05-03 010101 0.11 212
001 2008-05-04 010101 0.10 526
002 ....................
希望得到的结果
最大单价 最小单价 平均单价1 平均单价2 最新单价 最大数量 最小数量 最新数量
001 010101 0.12 0.10 0.11 0.10 0.10 526 126 526
上面的平均单价1的算法是直接用avg来得出的
还有一种平均单价2应该是 sum(fdj*fsl)/sum(fsl)来得出,
10 个解决方案
#1
select fgysbm,fcpbm,
max(fdj) 最大单价,
min(fdj) 最小单价,
avg(fdj) 平均单价1,
sum(fdj*fsl)/sum(fsl)平均单价2,
(select top 1 fdj from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc) 最新单价,
max(fsl) 最大数量,
min(fsl) 最小数量,
(select fsl from table where fsj = (select top 1 fdj from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc)) 最新数量
from table m
group by fgysbm,fcpbm
#2
sum(fdj*fsl)/sum(fsl)平均单价2,
上面平均单价2的算法不明白,自己考虑一下.
上面平均单价2的算法不明白,自己考虑一下.
#3
select
fgysbm,
max(fdj),
min(fdj),
avg(fdj),
sum(fdj*fsl)/sum(fsl),
(select top 1 fdj from 表 where fgysbm=t.fgysbm order by fsj desc),
max(fsl),
min(fsl),
(select top 1 fsl from 表 where fgysbm=t.fgysbm order by fsj desc)
from
表 t
group by
fgysbm
#4
select fgysbm,fcpbm,
max(fdj) 最大单价,
min(fdj) 最小单价,
avg(fdj) 平均单价1,
sum(fdj*fsl)/sum(fsl)平均单价2,
(select top 1 fdj from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc) 最新单价,
max(fsl) 最大数量,
min(fsl) 最小数量,
(select fsl from table where fsj = (select top 1 fdj from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc)) 最新数量
from table m
#5
select fgysbm,fcpbm,
max(fdj) 最大单价,
min(fdj) 最小单价,
avg(fdj) 平均单价1,
sum(fdj*fsl)/sum(fsl)平均单价2,
(select top 1 fdj from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc) 最新单价,
max(fsl) 最大数量,
min(fsl) 最小数量,
(select top 1 fsl from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc) 最新数量
from table m
group by fgysbm,fcpbm
#6
select fgysbm,fcpbm,
max(fdj) 最大单价,
min(fdj) 最小单价,
avg(fdj) 平均单价1,
sum(fdj*fsl)/sum(fsl)平均单价2,
(select top 1 fdj from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc) 最新单价,
max(fsl) 最大数量,
min(fsl) 最小数量,
(select top 1 fsl from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc) 最新数量
from table m
group by fgysbm,fcpbm
#7
这个的意思是想用总的金额除以总的数量来得出总单价
select fgysbm,fcpbm,
max(fdj) 最大单价,
min(fdj) 最小单价,
avg(fdj) 平均单价1,
sum(fdj*fsl)/sum(fsl)平均单价2,
(select top 1 fdj from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc) 最新单价,
max(fsl) 最大数量,
min(fsl) 最小数量,
(select top 1 fsl from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc) 最新数量
from table m
group by fgysbm,fcpbm
这种写法可以
谢谢各位了。
#8
这么快
#9
select *
from (
select fgysbm,fcpbm,最新单价 as fdj, 最新数量 as fsl
from table a
where exists(select 1 from table where fgysbm = a.fgysbm and fcpbm = a.fcpbm and fsj > a.fsj)
) a
join (
select fgysbm,fcpbm,max(fdj) as maxfdj,min(fdj) as minfdj,avg(fdj) as avgfdj1,sum(fdj*fsl)/sum(fsl) as avgfdj2,max(fsl) as maxfsl,min(fsl) as minfsl
from table
group by fgysbm,fcpbm) b
on a.fgysbm = b.fgysbm and a.fcpbm = b.fcpbm
#10
但这样得到的最新单价跟最新数量都是一样的啊
#1
select fgysbm,fcpbm,
max(fdj) 最大单价,
min(fdj) 最小单价,
avg(fdj) 平均单价1,
sum(fdj*fsl)/sum(fsl)平均单价2,
(select top 1 fdj from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc) 最新单价,
max(fsl) 最大数量,
min(fsl) 最小数量,
(select fsl from table where fsj = (select top 1 fdj from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc)) 最新数量
from table m
group by fgysbm,fcpbm
#2
sum(fdj*fsl)/sum(fsl)平均单价2,
上面平均单价2的算法不明白,自己考虑一下.
上面平均单价2的算法不明白,自己考虑一下.
#3
select
fgysbm,
max(fdj),
min(fdj),
avg(fdj),
sum(fdj*fsl)/sum(fsl),
(select top 1 fdj from 表 where fgysbm=t.fgysbm order by fsj desc),
max(fsl),
min(fsl),
(select top 1 fsl from 表 where fgysbm=t.fgysbm order by fsj desc)
from
表 t
group by
fgysbm
#4
select fgysbm,fcpbm,
max(fdj) 最大单价,
min(fdj) 最小单价,
avg(fdj) 平均单价1,
sum(fdj*fsl)/sum(fsl)平均单价2,
(select top 1 fdj from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc) 最新单价,
max(fsl) 最大数量,
min(fsl) 最小数量,
(select fsl from table where fsj = (select top 1 fdj from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc)) 最新数量
from table m
#5
select fgysbm,fcpbm,
max(fdj) 最大单价,
min(fdj) 最小单价,
avg(fdj) 平均单价1,
sum(fdj*fsl)/sum(fsl)平均单价2,
(select top 1 fdj from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc) 最新单价,
max(fsl) 最大数量,
min(fsl) 最小数量,
(select top 1 fsl from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc) 最新数量
from table m
group by fgysbm,fcpbm
#6
select fgysbm,fcpbm,
max(fdj) 最大单价,
min(fdj) 最小单价,
avg(fdj) 平均单价1,
sum(fdj*fsl)/sum(fsl)平均单价2,
(select top 1 fdj from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc) 最新单价,
max(fsl) 最大数量,
min(fsl) 最小数量,
(select top 1 fsl from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc) 最新数量
from table m
group by fgysbm,fcpbm
#7
这个的意思是想用总的金额除以总的数量来得出总单价
select fgysbm,fcpbm,
max(fdj) 最大单价,
min(fdj) 最小单价,
avg(fdj) 平均单价1,
sum(fdj*fsl)/sum(fsl)平均单价2,
(select top 1 fdj from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc) 最新单价,
max(fsl) 最大数量,
min(fsl) 最小数量,
(select top 1 fsl from table where fgysbm = m.fgysbm and fcpbm = n.fcpbm order by fsj desc) 最新数量
from table m
group by fgysbm,fcpbm
这种写法可以
谢谢各位了。
#8
这么快
#9
select *
from (
select fgysbm,fcpbm,最新单价 as fdj, 最新数量 as fsl
from table a
where exists(select 1 from table where fgysbm = a.fgysbm and fcpbm = a.fcpbm and fsj > a.fsj)
) a
join (
select fgysbm,fcpbm,max(fdj) as maxfdj,min(fdj) as minfdj,avg(fdj) as avgfdj1,sum(fdj*fsl)/sum(fsl) as avgfdj2,max(fsl) as maxfsl,min(fsl) as minfsl
from table
group by fgysbm,fcpbm) b
on a.fgysbm = b.fgysbm and a.fcpbm = b.fcpbm
#10
但这样得到的最新单价跟最新数量都是一样的啊