我有一张表的结构是:
CREATE TABLE [dbo].[tb_bl_Show] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[UserCode] [int] NOT NULL ,--用户编号
[MobilePhone] [nvarchar] (30) --用户手机号
[RetailStoreID] [nvarchar] (50), --商店编码
[ProductID] [int] NOT NULL ,--商品编码
[StatQuantity] [int] NOT NULL ,--商品数量
         [SMSDate] [datetime] NOT NULL --商品发布日期
}
我现在要写一个存储过程:输入两个参数是 商品发布起始日期和结束日期
输出是满足在这一时间段的表信息要求是:
1)表中有同一用户的相同商品编码信息,则以最后一条为准。
(
 例如:6月11日某SA发了A的数量为3个,6月12日某SA发了A的数量为4个,
  1.如果检索区间为6月1日至6月11日,则只显示数量为3的那条记录
  2.如果检索区间为6月1日至6月12日,则只显示数量为4的那条记录,6月11日的库存不显示)
2)同一用户但不同商品编码信息,则以2条都为统计
3)同一商店中不同用户,但他们的商品编码信息相同,则以2人中的最后一条为准,另一人的舍弃。
4)同一商店中不同用户,但他们的商品编码信息不相同,则以2人中的都统计

解决方案 »

  1.   

    ---创建存储过程
    Create Proc Pro_tb_Bl_Show
           @Bdate Datetime,
           @Edate Datetime
    As 
      Begin
        Select * From tb_bl_Show As A Where SMSDate Between @Bdate And @Edate And
             Not Exists
                (Select 1 From tb_bl_Show Where UserCode=A.tb_bl_Show And 
                          ProductID=A.ProductID And SMSDate>A.SMSDate) And
             Not Exists
                (Select 1 From tb_bl_Show Where RetailStoreID=A.RetailStoreID And 
                          ProductID=A.ProductID And SMSDate>A.SMSDate)
      End
    GO
    ---调用存储过程
    EXEC Pro_tb_Bl_Show '2007-06-01','2007-06-11'
      

  2.   

    问一下:
     Not Exists
                (Select 1 From tb_bl_Show Where UserCode=A.UserCode And 
                          ProductID=A.ProductID And SMSDate>A.SMSDate)起到什么作用呢?
      

  3.   

    Not Exists 作个判断,它并不返回结果集,只返回TRUE 或 FALSE
    上面存储过程是否你要的结果???
      

  4.   

    declare @startdate as datetime
    declare @enddate as datetimeselect a.* from tb_b a,
    (
      select UserCode , max(SMSDate) SMSDate from tb_b where SMSDate >= @startdate and SMSDate <= @enddate group by UserCode
    ) b
    where a.UserCode = b.UserCode and a.SMSDate = b.SMSDate
      

  5.   

    Create Proc Pro_tb_Bl_Show
           @Bdate Datetime,
           @Edate Datetime
    As 
      Begin
        Select * From tb_bl_Show As A Where SMSDate Between @Bdate And @Edate And
             Not Exists
                (Select 1 From tb_bl_Show Where UserCode=A.tb_bl_Show And 
                          ProductID=A.ProductID And SMSDate>A.SMSDate and SMSDate Between @Bdate And @Edate ) And
             Not Exists
                (Select 1 From tb_bl_Show Where RetailStoreID=A.RetailStoreID And 
                          ProductID=A.ProductID And SMSDate>A.SMSDate and SMSDate Between @Bdate And @Edate )
      End
    GO
      

  6.   

    CREATE PROCEDURE my
           @startdate datetime , 
           @enddate datetime
    AS 
      select a.* from tb_b a,
      (
        select UserCode , max(SMSDate) SMSDate from tb_b where SMSDate >= @startdate and SMSDate <= @enddate group by UserCode
      ) b
      where a.UserCode = b.UserCode and a.SMSDate = b.SMSDate
    GO
      

  7.   

    dawugui(潇洒老乌龟)
    那只能判断UserCode 是否重复,如果在判断商场是否重复,还得在对查询的结果在判断一次么?感觉。好像有点烦了
      

  8.   

    binbinfriend() ( ) 信誉:100  2007-08-31 09:15:56  得分: 0  
    运行的结果不太对
    --------------------------------
    把你的数据及要的结果贴出来看看
      

  9.   

    用户编号 用户手机号    商店编码        商品编码    发布日期
    9040076  13146488377  77950005000012   2590        2007-06-13 15:40:59.000
    9040075  13146488377  77950005000013   2590        2007-06-14 15:08:30.850
    9070115  13942772754  77950005000012   2590        2007-06-15 15:39:56.000
    我如果检索 '2007-06-13 00:00:00.000' And '2007-06-14 14:40:59.000'你那结果没有值了
    结果是要显示第一行的值        
      

  10.   

    我后来对你的NOT EXISTS 修改了一下,修改后的存储过程贴了出来,你看看这样修改行么?
      

  11.   

    --把商场代码,商品编码加进去.CREATE PROCEDURE my
           @startdate datetime , 
           @enddate datetime
    AS 
      select a.* from tb_b a,
      (
        select RetailStoreID,ProductID,UserCode , max(SMSDate) SMSDate from tb_b where SMSDate >= @startdate and SMSDate <= @enddate group by RetailStoreID,ProductID,UserCode 
      ) b
      where a.RetailStoreID=b.RetailStoreID and a.ProductID =b.ProductID and a.UserCode = b.UserCode and a.SMSDate = b.SMSDate
    GO
      

  12.   

    谢谢!对了。。我还想问一下,如果我的存储过程有5个参数,两个必填,其它三个可有值,也可以没有值,我的where语句该怎么写呢?
    如果传过来3个A,B,C三个参数
    where ..=A, ..=B, ..=C
    如果传过来4个A,B,C,D四个参数
    where ..=A, ..=B, ..=C,..=DWHERE的条件是根据传个来参数的个数变的,那我这个WHERE子局该怎么写呢?