create table test (sid nvarchar(5),sdate datetime,svol int)
insert into test 
select 's1','2013-12-10',20 union all
select 's1','2013-12-12',24 union all
select 's1','2013-12-13',23 union all
select 's1','2013-12-14',23 union all
select 's2','2013-12-16',25 union all
select 's2','2013-12-17',26 union all
select 's2','2013-12-19',24 union all
select 's3','2013-12-23',18 union all
select 's3','2013-12-24',20 union all
select 's3','2013-12-25',19 union all
select 's3','2013-12-26',21 union all
select 's3','2013-12-27',20select a.sid,
sdate=MIN(a.sdate),
svol=( select svol 
from test 
where sid=a.sid 
and sdate =(
select MIN(sdate) 
from test 
where sid=a.sid
)
)
from test a
group by a.sid
union 
select a.sid,
sdate=MAX(a.sdate),
svol=( select svol 
from test 
where sid=a.sid 
and sdate =(
select MAX(sdate) 
from test 
where sid=a.sid
)
)
from test a
group by a.sid
 
/*
s1 2013-12-10 00:00:00.000 20
s1 2013-12-14 00:00:00.000 23
s2 2013-12-16 00:00:00.000 25
s2 2013-12-19 00:00:00.000 24
s3 2013-12-23 00:00:00.000 18
s3 2013-12-27 00:00:00.000 20
*/

解决方案 »

  1.   


    --> 测试数据:[a]
    if object_id('[a]') is not null drop table [a]
    go 
    create table [a](sid  char(2),sdate datetime,svol int)
    insert [a]
    select 's1', '2013-12-10', 20 union all
    select 's1', '2013-12-12', 24 union all
    select 's1', '2013-12-13', 23 union all
    select 's1', '2013-12-14', 23 union all
    select 's2', '2013-12-16', 25 union all
    select 's2', '2013-12-17', 26 union all
    select 's2', '2013-12-19', 24 union all
    select 's3', '2013-12-23', 18 union all
    select 's3', '2013-12-24', 20 union all
    select 's3', '2013-12-25', 19 union all
    select 's3', '2013-12-26', 21 union all
    select 's3', '2013-12-27', 20 
    select a.* from a ,
    (
    select sid ,min(sdate) as sdate from a group  by sid
    union all
    select sid ,max(sdate) as sdate from a group  by sid
    ) b 
    where a.sid=b.sid and a.sdate=b.sdate
    order by a.sid
      

  2.   

    select * from tb a
     where not exists(select 1 from tb where sid=a.sid and datepart(week,sdate)=datepart(week,a.sdate) and a.sdate>sdate)
    or 
    not exists(select 1 from tb where sid=a.sid and datepart(week,sdate)=datepart(week,a.sdate) and a.sdate<sdate)
      

  3.   


    select * from
    (
    select *,ROW_NUMBER() over(partition by DATEPART(week,sdate) order by sdate) as rn from test 
    ) a where rn=1
    union
    select * from
    (
     select *,ROW_NUMBER() over(partition by DATEPART(week,sdate) order by sdate desc) as rn from test
    ) b where rn=1
      

  4.   

    这个其实很简单,
    sql应该有根据日期 获取 星期几的函数,
    where datepart(weekday, sdate)= ‘星期一’or datepart(weekday, sdate)= ‘星期日’
    就OK了,当可能datepart(weekday, sdate)是英文的,看你是什么语言的。