有难度的查询,请大家帮忙看看

ss__328 2004-12-24 03:49:36
有如下进货表

商品 单位 进货日期 进货金额

商品1 单位1 2004-12-12 50
商品1 单位1 2005-12-12 60
商品2 单位1 2004-12-12 60
商品2 单位1 2005-12-12 60
商品2 单位2 2003-12-12 40
..........

现要实现如下统计,统计出最近的7次进价

商品 单位 进价1 进价2 .....进价7
商品1 单位1
商品2 单位1
商品2 单位2
...................................

处理方式考虑过用游标或客户端计算实现,速度上均未达到要求(大型项目,客户多\数据量大\网络情况复杂)

请问各位是否能通过查询语句实现,请指点一二,谢谢
...全文
236 22 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
gimy007 2004-12-24
  • 打赏
  • 举报
回复
学习~~~
ss__328 2004-12-24
  • 打赏
  • 举报
回复
zjcxc(邹建)的方法试成功了,真的很感谢大家
散分
ss__328 2004-12-24
  • 打赏
  • 举报
回复

[libin_ftsafe(子陌红尘) 的试了么?]

正在试,
select identity(1,1) as id,a.* into #t from 进货表 order by 商品 asc,单位 asc,日期 desc

与 select * from 进货表 order by 商品 asc,单位 asc,日期 desc

排出的序不一样,这是为什么

long0104 2004-12-24
  • 打赏
  • 举报
回复
select 商品,单位
,进价1=max(case sid when 1 then 进货金额 else 0 end)
,进价2=max(case sid when 2 then 进货金额 else 0 end)
,进价3=max(case sid when 3 then 进货金额 else 0 end)
,进价4=max(case sid when 4 then 进货金额 else 0 end)
,进价5=max(case sid when 5 then 进货金额 else 0 end)
,进价6=max(case sid when 6 then 进货金额 else 0 end)
,进价7=max(case sid when 7 then 进货金额 else 0 end)
from(
select 商品,单位,进货金额,sid=(
select count(*) from 进货表 where 商品=a.商品 and 单位=a.单位 and 进货日期<=a.进货日期)
from 进货表 a
)a group by 商品,单位
lsxaa 2004-12-24
  • 打赏
  • 举报
回复
libin_ftsafe(子陌红尘) 的试了么?
lsxaa 2004-12-24
  • 打赏
  • 举报
回复
做过滤,这样
select *
from t a
where (select count(*)
from t
where 商品=a.商品 and 单位=a.单位
and 日期>=a.日期)<=7
zjcxc 元老 2004-12-24
  • 打赏
  • 举报
回复
select 商品,单位
,进价1=max(case sid when 1 then 进货金额 end)
,进价2=max(case sid when 2 then 进货金额 end)
,进价3=max(case sid when 3 then 进货金额 end)
,进价4=max(case sid when 4 then 进货金额 end)
,进价5=max(case sid when 5 then 进货金额 end)
,进价6=max(case sid when 6 then 进货金额 end)
,进价7=max(case sid when 7 then 进货金额 end)
from(
select 商品,单位,进货金额,sid=(
select count(*) from 进货表 where 商品=a.商品 and 单位=a.单位 and id<=a.id)
from 进货表 a
)a where sid<=7 group by 商品,单位
zjcxc 元老 2004-12-24
  • 打赏
  • 举报
回复
--有id做主键这样写:

select 商品,单位
,进价1=max(case sid when 1 then 进货金额 else 0 end)
,进价2=max(case sid when 2 then 进货金额 else 0 end)
,进价3=max(case sid when 3 then 进货金额 else 0 end)
,进价4=max(case sid when 4 then 进货金额 else 0 end)
,进价5=max(case sid when 5 then 进货金额 else 0 end)
,进价6=max(case sid when 6 then 进货金额 else 0 end)
,进价7=max(case sid when 7 then 进货金额 else 0 end)
from(
select 商品,单位,进货金额,sid=(
select count(*) from 进货表 where 商品=a.商品 and 单位=a.单位 and id<=a.id)
from 进货表 a
)a where sid<=7 group by 商品,单位
ss__328 2004-12-24
  • 打赏
  • 举报
回复
谢谢大家帮助,邹建的方法结果如下,
可以看到中间有0存在,正确结果应该没有0数据的。还没完全理解各位的方法,请问怎么去掉0


商品编码 单位编码 1 2 3 4 5
00004000010000100023 000070000200005 0.0 2580.0 0.0 2580.0 2580.0 0.0 0.0
00004000010000100023 00029 2579.0 0.0 0.0 0.0 0.0 0.0 0.0
00004000010000100024 00029 0.0 0.0 0.0 0.0 5097.0 5098.0 5098.0
00004000010000100025 00029 4398.0 4396.0 0.0 0.0 0.0 0.0 0.0
00004000010000100026 000070000200005 0.0 0.0 3449.0 3449.0 0.0 0.0 3449.0
00004000010000100027 000070000200005 2249.0 2249.0 2249.0 2249.0 2249.0 2249.0 0.0
00004000010000100028 000070000200005 4319.0 4319.0 4319.0 4319.0 0.0 4349.0 0.0
00004000010000100028 00029 4299.0 0.0 0.0 0.0 0.0 0.0 0.0
00004000010000100029 000070000200005 2249.0 2249.0 2248.0 2249.0 2249.0 2249.0 0.0
00004000010000100029 00029 2249.0 2249.0 2250.0 0.0 2250.0 2250.0 2250.0
00004000010000100030 000070000200005 3448.0 0.0 0.0 0.0 0.0 0.0 0.0
00004000010000100030 00029 3448.0 0.0 3448.0 0.0 3449.0 3749.0 3749.0
ss__328 2004-12-24
  • 打赏
  • 举报
回复
第一个结果有些不对,更重要的是速度太慢,我只统计进价1就用了1分多种(数据表大,约有20000条数据)

第二个
执行时 (不能对包含聚合或子查询的表达式执行聚合函数。)

lsxaa(小李铅笔刀) 多谢你
请问,怎样实现对进货表进行过滤,只保留最近的7次进货记录(不足7次有多少是多少),我用返回的数据在客户端处理,可以压缩到30秒。
lsxaa 2004-12-24
  • 打赏
  • 举报
回复
邹建的更好 呵呵
lsxaa 2004-12-24
  • 打赏
  • 举报
回复
好,楼上的方法好
子陌红尘 2004-12-24
  • 打赏
  • 举报
回复
select identity(1,1) as id,a.* into #t from 进货表 order by 商品 asc,单位 asc,日期 desc


select a.商品,
a.单位,
sum(case when (a.id-b.id = 0) then a.金额 else 0 end) as 进价1,
sum(case when (a.id-b.id = 1) then a.金额 else 0 end) as 进价2,
sum(case when (a.id-b.id = 2) then a.金额 else 0 end) as 进价3,
sum(case when (a.id-b.id = 3) then a.金额 else 0 end) as 进价4,
sum(case when (a.id-b.id = 4) then a.金额 else 0 end) as 进价5,
sum(case when (a.id-b.id = 5) then a.金额 else 0 end) as 进价6,
sum(case when (a.id-b.id = 6) then a.金额 else 0 end) as 进价7,
from
#t a
inner join
(select 商品,单位,min(id) as id from #t group by 商品,单位) b
on
a.商品 = b.商品 and a.单位 = b.单位
group by
a.商品 , a.单位
order by
a.商品 , a.单位
zjcxc 元老 2004-12-24
  • 打赏
  • 举报
回复
--如果你的商品+单位+日期可以做主键,则:

select 商品,单位
,进价1=max(case sid when 1 then 进货金额 else 0 end)
,进价2=max(case sid when 2 then 进货金额 else 0 end)
,进价3=max(case sid when 3 then 进货金额 else 0 end)
,进价4=max(case sid when 4 then 进货金额 else 0 end)
,进价5=max(case sid when 5 then 进货金额 else 0 end)
,进价6=max(case sid when 6 then 进货金额 else 0 end)
,进价7=max(case sid when 7 then 进货金额 else 0 end)
from(
select 商品,单位,进货金额,sid=(
select count(*) from 进货表 where 商品=a.商品 and 单位=a.单位 and 进货日期<=a.进货日期)
from 进货表 a
)a group by 商品,单位
lsxaa 2004-12-24
  • 打赏
  • 举报
回复
第一个写的不对,有问题

呵呵 写的有点臃肿了, 没仔细考虑,一定有更好的解法
lsxaa 2004-12-24
  • 打赏
  • 举报
回复
select id=identity(int,1,1),* into #t from t order by 商品,单位,日期 desc

select 商品,单位,
sum(case when id=(select top 1 id
from t c
when c.商品=a.商品 and c.单位=a.单位
and (select count(*)
from t b
where b.商品=c.商品 and b.单位=c.单位
and id<=a.id)=1
) then
进货金额 else 0 end) as 进价1,
...
from t a
group by a.商品,a.单位


ss__328 2004-12-24
  • 打赏
  • 举报
回复
表中有一子增长字段主键
ss__328 2004-12-24
  • 打赏
  • 举报
回复
To lsxaa(小李铅笔刀)

好像有点问题,执行后一直查询中。。,很久都没有反应呢
zjcxc 元老 2004-12-24
  • 打赏
  • 举报
回复
没有主键按1楼的方法得不到正确的结果
zjcxc 元老 2004-12-24
  • 打赏
  • 举报
回复
表中没有主键么?
加载更多回复(2)

34,837

社区成员

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

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