有如下进货表
商品 单位 进货日期 进货金额
商品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
...................................
处理方式考虑过用游标或客户端计算实现,速度上均未达到要求(大型项目,客户多/数据量大/网络情况复杂)
请问各位是否能通过查询语句实现,请指点一二,谢谢 问题点数:100、回复次数:22Top
select 商品,单位,
进价1=(select top 1 进货金额
from t c
where c.商品=a.商品 and c.单位=a.单位
and (select count(*)
from t b
where b.商品=c.商品 and b.单位=c.单位
and b.进货日期>=c.进货日期)=1 --进价n 这里就写n
),
...
from t a
group by a.商品,a.单位Top
第一步必须实现对进货表进行过滤,只保留最近的7次进货记录.这应该怎么实现?
商品编码 单位编码 日期 价格
00004000010000100023 000070000200005 2004-02-09 2580.0
00004000010000100023 000070000200005 2004-02-08 2580.0
00004000010000100023 000070000200005 2004-02-08 2580.0
00004000010000100023 000070000200005 2004-02-07 2580.0
00004000010000100023 000070000200005 2004-02-07 2580.0
00004000010000100023 00029 2004-02-06 2579.0
00004000010000100024 00029 2004-03-14 5098.0
00004000010000100024 00029 2004-03-06 5098.0
00004000010000100024 00029 2004-02-25 5097.0
00004000010000100024 00029 2004-02-25 5097.0
00004000010000100024 00029 2004-02-25 5097.0
00004000010000100024 00029 2004-02-25 5097.0
00004000010000100024 00029 2004-02-25 5097.0
00004000010000100025 00029 2004-03-15 4396.0
00004000010000100025 00029 2004-03-06 4398.0
Top
表中没有主键么?Top
没有主键按1楼的方法得不到正确的结果Top
To lsxaa(小李铅笔刀)
好像有点问题,执行后一直查询中。。,很久都没有反应呢Top
表中有一子增长字段主键Top
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.单位
Top
第一个写的不对,有问题
呵呵 写的有点臃肿了, 没仔细考虑,一定有更好的解法Top
--如果你的商品+单位+日期可以做主键,则:
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 商品,单位Top
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.单位Top
好,楼上的方法好Top
邹建的更好 呵呵Top
第一个结果有些不对,更重要的是速度太慢,我只统计进价1就用了1分多种(数据表大,约有20000条数据)
第二个
执行时 (不能对包含聚合或子查询的表达式执行聚合函数。)
lsxaa(小李铅笔刀) 多谢你
请问,怎样实现对进货表进行过滤,只保留最近的7次进货记录(不足7次有多少是多少),我用返回的数据在客户端处理,可以压缩到30秒。Top
谢谢大家帮助,邹建的方法结果如下,
可以看到中间有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
Top
--有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 商品,单位
Top
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 商品,单位Top
做过滤,这样
select *
from t a
where (select count(*)
from t
where 商品=a.商品 and 单位=a.单位
and 日期>=a.日期)<=7
Top
libin_ftsafe(子陌红尘) 的试了么?Top
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 商品,单位
Top
[libin_ftsafe(子陌红尘) 的试了么?]
正在试,
select identity(1,1) as id,a.* into #t from 进货表 order by 商品 asc,单位 asc,日期 desc
与 select * from 进货表 order by 商品 asc,单位 asc,日期 desc
排出的序不一样,这是为什么
Top
zjcxc(邹建)的方法试成功了,真的很感谢大家
散分Top
学习~~~Top