一条sql语句,求最大值,最小值,平均值,最新值

saveaswhat 2009-02-16 10:07:44
表 table
表中列
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)来得出,
...全文
838 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
saveaswhat 2009-02-16
  • 打赏
  • 举报
回复
但这样得到的最新单价跟最新数量都是一样的啊
-狙击手- 2009-02-16
  • 打赏
  • 举报
回复
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
ws_hgo 2009-02-16
  • 打赏
  • 举报
回复
这么快
saveaswhat 2009-02-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dawugui 的回复:]
sum(fdj*fsl)/sum(fsl)平均单价2,

上面平均单价2的算法不明白,自己考虑一下.
[/Quote]
这个的意思是想用总的金额除以总的数量来得出总单价
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


这种写法可以

谢谢各位了。
you_tube 2009-02-16
  • 打赏
  • 举报
回复

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
dawugui 2009-02-16
  • 打赏
  • 举报
回复
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
贾桂权 2009-02-16
  • 打赏
  • 举报
回复
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
子陌红尘 2009-02-16
  • 打赏
  • 举报
回复
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
dawugui 2009-02-16
  • 打赏
  • 举报
回复
sum(fdj*fsl)/sum(fsl)平均单价2,

上面平均单价2的算法不明白,自己考虑一下.
dawugui 2009-02-16
  • 打赏
  • 举报
回复
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

34,837

社区成员

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

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