获取最新数据可以参考如下:(不知道你是不是按照PCode分组,如果不是,自己更改字段)
select t.* from Purchase t where StockDate = (select max(StockDate) from Purchase where PCode = t.PCode)
select t.* from Purchase t where not exists = (select 1 from Purchase where PCode = t.PCode and StockDate > t.StockDate)
--更多方法见下:--按某一字段分组取最大(小)值所在行的数据
--(爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 2007-10-23于浙江杭州)
/*
数据如下:
name val memo
a    2   a2(a的第二个值)
a    1   a1--a的第一个值
a    3   a3:a的第三个值
b    1   b1--b的第一个值
b    3   b3:b的第三个值
b    2   b2b2b2b2
b    4   b4b4
b    5   b5b5b5b5b5
*/
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a',    2,   'a2(a的第二个值)')
insert into tb values('a',    1,   'a1--a的第一个值')
insert into tb values('a',    3,   'a3:a的第三个值')
insert into tb values('b',    1,   'b1--b的第一个值')
insert into tb values('b',    3,   'b3:b的第三个值')
insert into tb values('b',    2,   'b2b2b2b2')
insert into tb values('b',    4,   'b4b4')
insert into tb values('b',    5,   'b5b5b5b5b5')
go--一、按name分组取val最大的值所在行的数据。
--方法1:
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
--方法3:
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
/*
name       val         memo                 
---------- ----------- -------------------- 
a          3           a3:a的第三个值
b          5           b5b5b5b5b5
*/--二、按name分组取val最小的值所在行的数据。
--方法1:
select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
--方法3:
select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
/*
name       val         memo                 
---------- ----------- -------------------- 
a          1           a1--a的第一个值
b          1           b1--b的第一个值
*/--三、按name分组取第一次出现的行所在的数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
/*
name       val         memo                 
---------- ----------- -------------------- 
a          2           a2(a的第二个值)
b          1           b1--b的第一个值
*/--四、按name分组随机取一条数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
/*
name       val         memo                 
---------- ----------- -------------------- 
a          1           a1--a的第一个值
b          5           b5b5b5b5b5
*/--五、按name分组取最小的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name,a.val
/*
name       val         memo                 
---------- ----------- -------------------- 
a          1           a1--a的第一个值
a          2           a2(a的第二个值)
b          1           b1--b的第一个值
b          2           b2b2b2b2
*/--六、按name分组取最大的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name , a.val
/*
name       val         memo                 
---------- ----------- -------------------- 
a          2           a2(a的第二个值)
a          3           a3:a的第三个值
b          4           b4b4
b          5           b5b5b5b5b5
*/
--七,如果整行数据有重复,所有的列都相同。
/*
数据如下:
name val memo
a    2   a2(a的第二个值)
a    1   a1--a的第一个值
a    1   a1--a的第一个值
a    3   a3:a的第三个值
a    3   a3:a的第三个值
b    1   b1--b的第一个值
b    3   b3:b的第三个值
b    2   b2b2b2b2
b    4   b4b4
b    5   b5b5b5b5b5
*/
--在sql server 2000中只能用一个临时表来解决,生成一个自增列,先对val取最大或最小,然后再通过自增列来取数据。
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a',    2,   'a2(a的第二个值)')
insert into tb values('a',    1,   'a1--a的第一个值')
insert into tb values('a',    1,   'a1--a的第一个值')
insert into tb values('a',    3,   'a3:a的第三个值')
insert into tb values('a',    3,   'a3:a的第三个值')
insert into tb values('b',    1,   'b1--b的第一个值')
insert into tb values('b',    3,   'b3:b的第三个值')
insert into tb values('b',    2,   'b2b2b2b2')
insert into tb values('b',    4,   'b4b4')
insert into tb values('b',    5,   'b5b5b5b5b5')
goselect * , px = identity(int,1,1) into tmp from tbselect m.name,m.val,m.memo from
(
  select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
) m where px = (select min(px) from
(
  select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
) n where n.name = m.name)drop table tb,tmp/*
name       val         memo
---------- ----------- --------------------
a          1           a1--a的第一个值
b          1           b1--b的第一个值(2 行受影响)
*/
--在sql server 2005中可以使用row_number函数,不需要使用临时表。
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a',    2,   'a2(a的第二个值)')
insert into tb values('a',    1,   'a1--a的第一个值')
insert into tb values('a',    1,   'a1--a的第一个值')
insert into tb values('a',    3,   'a3:a的第三个值')
insert into tb values('a',    3,   'a3:a的第三个值')
insert into tb values('b',    1,   'b1--b的第一个值')
insert into tb values('b',    3,   'b3:b的第三个值')
insert into tb values('b',    2,   'b2b2b2b2')
insert into tb values('b',    4,   'b4b4')
insert into tb values('b',    5,   'b5b5b5b5b5')
goselect m.name,m.val,m.memo from
(
  select * , px = row_number() over(order by name , val) from tb
) m where px = (select min(px) from
(
  select * , px = row_number() over(order by name , val) from tb
) n where n.name = m.name)drop table tb/*
name       val         memo
---------- ----------- --------------------
a          1           a1--a的第一个值
b          1           b1--b的第一个值(2 行受影响)
*/
至于你的结果,我没看懂.

解决方案 »

  1.   

    是按PCode分组,并且要将数据插入导LatestPurchasePrice表中
      

  2.   

    declare @Purchase table (
    StockDate datetime ,   GCode varchar(32) , PCode varchar(32), GName  varchar(32)        ,
    Brand varchar(32), Standard varchar(32), Unit varchar(32), BarCode  varchar(32) ,     Price varchar(32),
     Rate  varchar(32),  CooperateType varchar(32))
    insert into @purchase 
    select  '2008-10-12 0:00:00', '316455','联拓','喜力啤酒','喜力', 'dgdf',' dgd ','20090309000004 ','6 ','0.5 ','zczc'
    insert into @purchase 
    select'2008-11-12 0:00:00', '316455','联拓',' 喜力啤酒'  ,'喜力', 'dgdf',' dgd ','20090309000004 ','7 ','0.3 ','zczc'
    insert into @purchase 
    select '2008-11-11 0:00:00', '316455','贵城',' 喜力啤酒'  ,'喜力', 'fgfg',' fff ','20090309000004 ','5 ',' 0.3',' zczc'
    insert into @purchase 
    select '2008-10-10 0:00:00', '000022','东泰',' 元宝大豆油','元宝', 'dgdf',' dgd ','20090309000003 ','78','0.5',' zczc'
    insert into @purchase 
    select '2008-10-19 0:00:00', '000022','东泰',' 元宝大豆油','元宝', 'dgdf','  yy ','20090309000003 ','66',' 0.3',' ghgh' INSERT INTO LatestPurchasePrice
    SELECT  ROW_NUMBER() OVER (ORDER BY GCode) RecId ,T.GCode,T.GName,T.Brand,T.Standard,T.BarCode,
    (SELECT TOP 1 Price FROM @Purchase WHERE GCode = T.GCode AND T.PCode='联拓' ORDER BY StockDate DESC )  PriceOne ,           
    (SELECT TOP 1 StockDate FROM @Purchase WHERE GCode = T.GCode AND PCode='联拓'ORDER BY StockDate DESC ) StockDateOne,   
    (SELECT  TOP 1 Price FROM @Purchase WHERE GCode = T.GCode AND PCode='贵城'ORDER BY StockDate DESC )  PriceTwo,           
    (SELECT  TOP 1 StockDate FROM @Purchase WHERE GCode = T.GCode AND PCode='贵城'ORDER BY StockDate DESC ) StockDateTwo,   
    (SELECT  TOP 1 Price FROM @Purchase WHERE GCode = T.GCode AND PCode='东泰'ORDER BY StockDate DESC ) PriceThree,         
    (SELECT  TOP 1 StockDate FROM @Purchase WHERE GCode = T.GCode AND PCode='东泰'ORDER BY StockDate DESC ) StockDateThree 
    FROM @Purchase T
    GROUP BY GCode,GName,Brand,Standard,BarCode,PCode
    /*
    RecId GCode  GName      Brand  Standard BarCode         PriceOne  StockDateOne            PriceTwo  StockDateTwo            PriceThree StockDateThree
    ----- ------ ---------- ----- --------- --------------- --------- ----------------------- --------- ----------------------- ---------- -----------------------
    1     000022 元宝大豆油元宝    dgdf     20090309000003  NULL      NULL                    NULL      NULL                    66         2008-10-19 00:00:00.000
    2     316455 喜力啤酒  喜力    dgdf     20090309000004  7         2008-11-12 00:00:00.000 5         2008-11-11 00:00:00.000 NULL       NULL
    3     316455 喜力啤酒  喜力    fgfg     20090309000004  NULL      2008-11-12 00:00:00.000 5         2008-11-11 00:00:00.000 NULL       NULL
    4     316455 喜力啤酒  喜力    dgdf     20090309000004  7         2008-11-12 00:00:00.000 5         2008-11-11 00:00:00.000 NULL       NULL */
      

  3.   

    用row_number()over(partition by .. order by ..)
    得到分组排序的序列号,然后去每组第一个就可以了。