查最近7次进价,哈哈,办法直接在CSDN找到了!!!!特等高兴!!!

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

楼主ss__328(潇潇) 2004-12-24 15:49:36 在 MS-SQL Server / 基础类 提问

有如下进货表  
   
  商品       单位         进货日期       进货金额      
   
  商品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

1 楼lsxaa(小李铅笔刀)回复于 2004-12-24 15:57:16 得分 0

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

2 楼ss__328(潇潇)回复于 2004-12-24 15:57:54 得分 0

第一步必须实现对进货表进行过滤,只保留最近的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

3 楼zjcxc(邹建)回复于 2004-12-24 15:59:15 得分 0

表中没有主键么?Top

4 楼zjcxc(邹建)回复于 2004-12-24 15:59:45 得分 0

没有主键按1楼的方法得不到正确的结果Top

5 楼ss__328(潇潇)回复于 2004-12-24 16:05:30 得分 0

To     lsxaa(小李铅笔刀)  
   
  好像有点问题,执行后一直查询中。。,很久都没有反应呢Top

6 楼ss__328(潇潇)回复于 2004-12-24 16:07:22 得分 0

表中有一子增长字段主键Top

7 楼lsxaa(小李铅笔刀)回复于 2004-12-24 16:11:05 得分 0

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

8 楼lsxaa(小李铅笔刀)回复于 2004-12-24 16:12:08 得分 30

第一个写的不对,有问题  
   
  呵呵     写的有点臃肿了,   没仔细考虑,一定有更好的解法Top

9 楼zjcxc(邹建)回复于 2004-12-24 16:16:02 得分 50

--如果你的商品+单位+日期可以做主键,则:  
   
  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

10 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2004-12-24 16:18:11 得分 20

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

11 楼lsxaa(小李铅笔刀)回复于 2004-12-24 16:20:19 得分 0

好,楼上的方法好Top

12 楼lsxaa(小李铅笔刀)回复于 2004-12-24 16:20:42 得分 0

邹建的更好     呵呵Top

13 楼ss__328(潇潇)回复于 2004-12-24 16:23:10 得分 0

第一个结果有些不对,更重要的是速度太慢,我只统计进价1就用了1分多种(数据表大,约有20000条数据)  
   
  第二个  
  执行时   (不能对包含聚合或子查询的表达式执行聚合函数。)  
   
  lsxaa(小李铅笔刀)   多谢你  
  请问,怎样实现对进货表进行过滤,只保留最近的7次进货记录(不足7次有多少是多少),我用返回的数据在客户端处理,可以压缩到30秒。Top

14 楼ss__328(潇潇)回复于 2004-12-24 16:29:49 得分 0

谢谢大家帮助,邹建的方法结果如下,  
  可以看到中间有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

15 楼zjcxc(邹建)回复于 2004-12-24 16:31:08 得分 0

--有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

16 楼zjcxc(邹建)回复于 2004-12-24 16:32:06 得分 0

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

17 楼lsxaa(小李铅笔刀)回复于 2004-12-24 16:37:40 得分 0

做过滤,这样      
  select   *        
  from   t   a  
  where   (select   count(*)    
                from   t    
                where   商品=a.商品   and   单位=a.单位  
                            and   日期>=a.日期)<=7  
  Top

18 楼lsxaa(小李铅笔刀)回复于 2004-12-24 16:38:49 得分 0

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

19 楼long0104()回复于 2004-12-24 16:39:53 得分 0

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

20 楼ss__328(潇潇)回复于 2004-12-24 16:43:04 得分 0

 
  [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

21 楼ss__328(潇潇)回复于 2004-12-24 17:03:58 得分 0

zjcxc(邹建)的方法试成功了,真的很感谢大家  
  散分Top

22 楼gimy007(逮猫的耗子)回复于 2004-12-24 17:11:56 得分 0

学习~~~Top

相关问题



http://topic.csdn.net/t/20041224/15/3673923.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值