时间 产品名称 产品型号   交易价格
2011-7-23  A 2*3*5     0.5
2011-7-25  A 2*3*5     2.3
2011-7-28  B 2*3*7     1.3
2011-8-3  A 2*3*5      4
2011-8-8  C 2*3*9      2
2011-8-23  B 2*3*7   3
2011-9-1  C 2*3*9   6
2011-9-12 C 2*3*9  8
2011-9-21  B 2*3*7  7

找出每种产品在一个月当中最后一次交易的价格,如何写SQL语句



解决方案 »

  1.   


    select *
    from tb t
    where not exists (select 1 from tb where 产品名称=t.产品名称 
           and convert(varchar(6),时间,112)=convert(varchar(6),t.时间,112) and 时间>t.时间)
      

  2.   

    select * from tab a
    where not exists (
      select 1 from tab
      where convert(varchar(7),时间,120) = convert(varchar(7),a.时间,120)
      and 产品名称= a.产品名称 and 产品型号= a.产品型号
      and 时间 > a.时间
      )
      

  3.   

    select *
    from tb t
    where 时间 (select max(时间) from tb where 产品名称=t.产品名称 
           and convert(varchar(6),时间,112)=convert(varchar(6),t.时间,112))
      

  4.   

    if not object_id('Tempdb..#') is null
    drop table #
    Go
    Create table #([时间] Datetime,[产品名称] nvarchar(1),[产品型号] nvarchar(5),[交易价格] decimal(18,1))
    Insert #
    select '2011-7-23',N'A',N'2*3*5',0.5 union all
    select '2011-7-25',N'A',N'2*3*5',2.3 union all
    select '2011-7-28',N'B',N'2*3*7',1.3 union all
    select '2011-8-3',N'A',N'2*3*5',4 union all
    select '2011-8-8',N'C',N'2*3*9',2 union all
    select '2011-8-23',N'B',N'2*3*7',3 union all
    select '2011-9-1',N'C',N'2*3*9',6 union all
    select '2011-9-12',N'C',N'2*3*9',8 union all
    select '2011-9-21',N'B',N'2*3*7',7
    Go
    Select * 
    from # t
    where not exists(select 1
                     from #
                     where datediff(month,[时间],t.[时间])=0 and day([时间])>day(t.[时间]))
    /*
    时间                      产品名称 产品型号  交易价格
    ----------------------- ---- ----- ---------------------------------------
    2011-07-28 00:00:00.000 B    2*3*7 1.3
    2011-08-23 00:00:00.000 B    2*3*7 3.0
    2011-09-21 00:00:00.000 B    2*3*7 7.0(3 row(s) affected)*/
      

  5.   

    select
     *
    from
     tb t
    where
     时间=(select max(时间) from tb where 产品名称=t.产品名称  and convert(varchar(7),时间,120)=convert(varchar(7),t.时间,120))
      

  6.   

    学习,最喜欢关注SQL版面的了
      

  7.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2011-08-10 17:40:53
    -- Verstion:
    --      Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    -- Jul  9 2008 14:43:34 
    -- Copyright (c) 1988-2008 Microsoft Corporation
    -- Enterprise Evaluation Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([时间] datetime,[产品名称] varchar(1),[产品型号] varchar(5),[交易价格] numeric(2,1))
    insert [tb]
    select '2011-7-23','A','2*3*5',0.5 union all
    select '2011-7-25','A','2*3*5',2.3 union all
    select '2011-7-28','B','2*3*7',1.3 union all
    select '2011-8-3','A','2*3*5',4 union all
    select '2011-8-8','C','2*3*9',2 union all
    select '2011-8-23','B','2*3*7',3 union all
    select '2011-9-1','C','2*3*9',6 union all
    select '2011-9-12','C','2*3*9',8 union all
    select '2011-9-21','B','2*3*7',7
    --------------开始查询--------------------------
    select
     *
    from
     tb t
    where
     时间=(select max(时间) from tb where datediff(mm,[时间],t.[时间])=0)
    select
     *
    from
     tb t
    where
     时间=(select max(时间) from tb where convert(varchar(7),时间,120)=convert(varchar(7),t.时间,120))
    ----------------结果----------------------------
    /* 时间                      产品名称 产品型号  交易价格
    ----------------------- ---- ----- ---------------------------------------
    2011-07-28 00:00:00.000 B    2*3*7 1.3
    2011-08-23 00:00:00.000 B    2*3*7 3.0
    2011-09-21 00:00:00.000 B    2*3*7 7.0(3 行受影响)*/