date name
------------------------------------------
2008.08.08 07:00:00 A1-1
2008.08.08 08:00:00 A2-1
2008.08.08 09:00:00 A2-2
2008.08.08 10:00:00 A1-2
2008.08.08 11:00:00 A1-3
2008.08.08 12:00:00 B1-1
2008.08.08 13:00:00 B1-2
2008.08.08 14:00:00 B2-2
2008.08.09 07:00:00 A1-1
2008.08.09 08:00:00 A1-2
2008.08.09 09:00:00 A2-1
2008.08.09 10:00:00 A2-2
2008.08.09 11:00:00 A2-3
2008.08.09 12:00:00 B1-1
2008.08.09 13:00:00 B2-1
2008.08.09 14:00:00 B2-2需要得到的结果:
date name
--------------------------------------
2008.08.08 09:00:00 A2-2
2008.08.08 11:00:00 A1-3
2008.08.08 13:00:00 B1-2
2008.08.08 14:00:00 B2-2
2008.08.09 08:00:00 A1-2
2008.08.09 11:00:00 A2-3
2008.08.09 12:00:00 B1-1
2008.08.09 14:00:00 B2-2
按照时间排序,每天都有重复的A?-?,B?-?,要求出每天最大的A1-max,A2-max,A?-max,B1-max,B2-max,B?-max
请高手指教
------------------------------------------
2008.08.08 07:00:00 A1-1
2008.08.08 08:00:00 A2-1
2008.08.08 09:00:00 A2-2
2008.08.08 10:00:00 A1-2
2008.08.08 11:00:00 A1-3
2008.08.08 12:00:00 B1-1
2008.08.08 13:00:00 B1-2
2008.08.08 14:00:00 B2-2
2008.08.09 07:00:00 A1-1
2008.08.09 08:00:00 A1-2
2008.08.09 09:00:00 A2-1
2008.08.09 10:00:00 A2-2
2008.08.09 11:00:00 A2-3
2008.08.09 12:00:00 B1-1
2008.08.09 13:00:00 B2-1
2008.08.09 14:00:00 B2-2需要得到的结果:
date name
--------------------------------------
2008.08.08 09:00:00 A2-2
2008.08.08 11:00:00 A1-3
2008.08.08 13:00:00 B1-2
2008.08.08 14:00:00 B2-2
2008.08.09 08:00:00 A1-2
2008.08.09 11:00:00 A2-3
2008.08.09 12:00:00 B1-1
2008.08.09 14:00:00 B2-2
按照时间排序,每天都有重复的A?-?,B?-?,要求出每天最大的A1-max,A2-max,A?-max,B1-max,B2-max,B?-max
请高手指教
GROUP BY NAME
*
from
tb t
where
not exists(select 1
from tb
where datediff(day,[date],t.[date])=0
and cast(right(name,len(name)-charindex('-',name)) as int)>cast(right(t.name,len(t.name)-charindex('-',t.name)) as int)
)
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([date] datetime,[name] varchar(4))
insert [tb]
select '2008.08.08 07:00:00','A1-1' union all
select '2008.08.08 08:00:00','A2-1' union all
select '2008.08.08 09:00:00','A2-2' union all
select '2008.08.08 10:00:00','A1-2' union all
select '2008.08.08 11:00:00','A1-3' union all
select '2008.08.08 12:00:00','B1-1' union all
select '2008.08.08 13:00:00','B1-2' union all
select '2008.08.08 14:00:00','B2-2' union all
select '2008.08.09 07:00:00','A1-1' union all
select '2008.08.09 08:00:00','A1-2' union all
select '2008.08.09 09:00:00','A2-1' union all
select '2008.08.09 10:00:00','A2-2' union all
select '2008.08.09 11:00:00','A2-3' union all
select '2008.08.09 12:00:00','B1-1' union all
select '2008.08.09 13:00:00','B2-1' union all
select '2008.08.09 14:00:00','B2-2'
---查询---
select
*
from
tb t
where
not exists(select 1
from tb
where datediff(day,[date],t.[date])=0
and left(name,2)=left(t.name,2)
and cast(right(name,len(name)-charindex('-',name)) as int)>cast(right(t.name,len(t.name)-charindex('-',t.name)) as int)
)---结果---
date name
------------------------------------------------------ ----
2008-08-08 09:00:00.000 A2-2
2008-08-08 11:00:00.000 A1-3
2008-08-08 13:00:00.000 B1-2
2008-08-08 14:00:00.000 B2-2
2008-08-09 08:00:00.000 A1-2
2008-08-09 11:00:00.000 A2-3
2008-08-09 12:00:00.000 B1-1
2008-08-09 14:00:00.000 B2-2(所影响的行数为 8 行)
declare @tb table(date datetime,name varchar(5))
insert @tb
select
'2008.08.08 07:00:00' , 'A1-1' union all select
'2008.08.08 08:00:00' , 'A2-1' union all select
'2008.08.08 09:00:00' , 'A2-2' union all select
'2008.08.08 10:00:00' , 'A1-2' union all select
'2008.08.08 11:00:00' , 'A1-3' union all select
'2008.08.08 12:00:00' , 'B1-1' union all select
'2008.08.08 13:00:00' , 'B1-2' union all select
'2008.08.08 14:00:00' , 'B2-2' union all select
'2008.08.09 07:00:00' , 'A1-1' union all select
'2008.08.09 08:00:00' , 'A1-2' union all select
'2008.08.09 09:00:00' , 'A2-1' union all select
'2008.08.09 10:00:00' , 'A2-2' union all select
'2008.08.09 11:00:00' , 'A2-3' union all select
'2008.08.09 12:00:00' , 'B1-1' union all select
'2008.08.09 13:00:00' , 'B2-1' union all select
'2008.08.09 14:00:00' , 'B2-2' select * from @tb t
where not exists(select * from @tb where convert(varchar(10),date,120)=convert(varchar(10),t.date,120) and left(name,2)=left(t.name,2) and cast(right(name,1)as int)>cast(right(t.name,1)as int))date name
----------------------- -----
2008-08-08 09:00:00.000 A2-2
2008-08-08 11:00:00.000 A1-3
2008-08-08 13:00:00.000 B1-2
2008-08-08 14:00:00.000 B2-2
2008-08-09 08:00:00.000 A1-2
2008-08-09 11:00:00.000 A2-3
2008-08-09 12:00:00.000 B1-1
2008-08-09 14:00:00.000 B2-2
不对啊
CREATE TABLE TEST(DATE DATETIME,NAME VARCHAR(10))
INSERT INTO TEST
SELECT '2008.08.08 07:00:00 ' , 'A1-1 ' UNION ALL
SELECT '2008.08.08 07:00:00 ' , 'A2-1 ' UNION ALL
SELECT '2008.08.08 07:00:00 ' , 'A2-2 ' UNION ALL
SELECT '2008.08.08 07:00:00 ' , 'A1-2 ' UNION ALL
SELECT '2008.08.08 07:00:00 ' , 'A1-3 ' UNION ALL
SELECT '2008.08.08 07:00:00 ' , 'B1-1 ' UNION ALL
SELECT '2008.08.08 07:00:00 ' , 'B1-2 ' UNION ALL
SELECT '2008.08.08 07:00:00 ' , 'B2-2 ' UNION ALL
SELECT '2008.08.09 07:00:00 ' , 'A1-1 ' UNION ALL
SELECT '2008.08.09 07:00:00 ' , 'A1-2 ' UNION ALL
SELECT '2008.08.09 07:00:00 ' , 'A2-1 ' UNION ALL
SELECT '2008.08.09 07:00:00 ' , 'A2-2 ' UNION ALL
SELECT '2008.08.09 07:00:00 ' , 'A2-3 ' UNION ALL
SELECT '2008.08.09 07:00:00 ' , 'B1-1 ' UNION ALL
SELECT '2008.08.09 07:00:00 ' , 'B2-1 ' UNION ALL
SELECT '2008.08.09 07:00:00 ' , 'B2-2 ' GO--DROP TABLE TEST
SELECT LEFT(NAME,2),'-',MAX(CAST(RIGHT(NAME,2) AS INT)) FROM TEST
GROUP BY (LEFT(NAME,2)),DATE
---- ---- -----------
A1 - 3
A2 - 2
B1 - 2
B2 - 2
A1 - 2
A2 - 3
B1 - 1
B2 - 2(所影响的行数为 8 行)
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([date] datetime,[name] varchar(10))
insert [tb]
select '2008.08.08 07:00:00','A22-11' union all
select '2008.08.08 08:00:00','A22-1' union all
select '2008.08.08 09:00:00','A22-2' union all
select '2008.08.08 10:00:00','A1-2' union all
select '2008.08.08 11:00:00','A1-3' union all
select '2008.08.08 12:00:00','B1-1' union all
select '2008.08.08 13:00:00','B1-2' union all
select '2008.08.08 14:00:00','B2-2' union all
select '2008.08.09 07:00:00','A1-1' union all
select '2008.08.09 08:00:00','A1-2' union all
select '2008.08.09 09:00:00','A2-1' union all
select '2008.08.09 10:00:00','A2-2' union all
select '2008.08.09 11:00:00','A2-3' union all
select '2008.08.09 12:00:00','B1-1' union all
select '2008.08.09 13:00:00','B2-1' union all
select '2008.08.09 14:00:00','B2-2'
---查询---
select
*
from
tb t
where
not exists(select 1
from tb
where datediff(day,[date],t.[date])=0
and left(name,charindex('-',name)-1)=left(t.name,charindex('-',name)-1)
and cast(right(name,len(name)-charindex('-',name)) as int)>cast(right(t.name,len(t.name)-charindex('-',t.name)) as int)
)---结果---
date name
------------------------------------------------------ ----------
2008-08-08 07:00:00.000 A22-11
2008-08-08 11:00:00.000 A1-3
2008-08-08 13:00:00.000 B1-2
2008-08-08 14:00:00.000 B2-2
2008-08-09 08:00:00.000 A1-2
2008-08-09 11:00:00.000 A2-3
2008-08-09 12:00:00.000 B1-1
2008-08-09 14:00:00.000 B2-2(所影响的行数为 8 行)
CREATE TABLE TEST(DATE DATETIME,NAME VARCHAR(10))
INSERT INTO TEST
SELECT '2008.08.08 07:00:00 ' , 'A1-1 ' UNION ALL
SELECT '2008.08.08 07:00:00 ' , 'A2-1 ' UNION ALL
SELECT '2008.08.08 07:00:00 ' , 'A2-2 ' UNION ALL
SELECT '2008.08.08 07:00:00 ' , 'A1-2 ' UNION ALL
SELECT '2008.08.08 07:00:00 ' , 'A1-3 ' UNION ALL
SELECT '2008.08.08 07:00:00 ' , 'B1-1 ' UNION ALL
SELECT '2008.08.08 07:00:00 ' , 'B1-2 ' UNION ALL
SELECT '2008.08.08 07:00:00 ' , 'B2-2 ' UNION ALL
SELECT '2008.08.09 07:00:00 ' , 'A1-1 ' UNION ALL
SELECT '2008.08.09 07:00:00 ' , 'A1-2 ' UNION ALL
SELECT '2008.08.09 07:00:00 ' , 'A2-1 ' UNION ALL
SELECT '2008.08.09 07:00:00 ' , 'A2-2 ' UNION ALL
SELECT '2008.08.09 07:00:00 ' , 'A2-3 ' UNION ALL
SELECT '2008.08.09 07:00:00 ' , 'B1-1 ' UNION ALL
SELECT '2008.08.09 07:00:00 ' , 'B2-1 ' UNION ALL
SELECT '2008.08.09 07:00:00 ' , 'B2-2 ' GO--DROP TABLE TEST
SELECT LEFT(NAME,CHARINDEX('-',NAME)-1),'-',MAX(CAST(RIGHT(NAME,2) AS INT)) FROM TEST
GROUP BY (LEFT(NAME,CHARINDEX('-',NAME)-1)),DATE
---------- ---- -----------
A1 - 3
A2 - 2
B1 - 2
B2 - 2
A1 - 2
A2 - 3
B1 - 1
B2 - 2(所影响的行数为 8 行)
SELECT *
FROM TB
WHERE
NOT EXISTS(SELECT 1
FROM TB
WHERE NAME=T.NAME --相同名称
AND DATEDIFF(DAY,[TIME],T.[TIME])=0 --同一天
AND [TIME]>T.[TIME] --主查询取时间最大的
)
FROM TB T
WHERE
NOT EXISTS(SELECT 1
FROM TB
WHERE NAME=T.NAME --相同名称
AND DATEDIFF(DAY,[TIME],T.[TIME])=0 --同一天
AND [TIME]>T.[TIME] --主查询取时间最大的
)
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([date] datetime,[name] varchar(10))
insert [tb]
select '2008.08.08 07:00:00','A22-11' union all
select '2008.08.08 08:00:00','A22-1' union all
select '2008.08.08 09:00:00','A22-2' union all
select '2008.08.08 10:00:00','A1-2' union all
select '2008.08.08 11:00:00','A1-3' union all
select '2008.08.08 12:00:00','B1-1' union all
select '2008.08.08 11:00:00','B1-1' union all --add
select '2008.08.08 13:00:00','B1-2' union all
select '2008.08.08 14:00:00','B2-2' union all
select '2008.08.09 07:00:00','A1-1' union all
select '2008.08.09 08:00:00','A1-2' union all
select '2008.08.09 09:00:00','A2-1' union all
select '2008.08.09 10:00:00','A2-2' union all
select '2008.08.09 11:00:00','A2-3' union all
select '2008.08.09 12:00:00','B1-1' union all
select '2008.08.09 13:00:00','B2-1' union all
select '2008.08.09 14:00:00','B2-2'
---查询---
select
*
from
tb t
where
not exists(select 1
from tb
where datediff(day,[date],t.[date])=0
and left(name,charindex('-',name)-1)=left(t.name,charindex('-',name)-1)
and cast(right(name,len(name)-charindex('-',name)) as int)>cast(right(t.name,len(t.name)-charindex('-',t.name)) as int)
)
and
not exists (select 1 from tb where name=t.name and date>t.date) date name
----------------------- ----------
2008-08-08 07:00:00.000 A22-11
2008-08-08 11:00:00.000 A1-3
2008-08-08 13:00:00.000 B1-2
2008-08-09 08:00:00.000 A1-2
2008-08-09 11:00:00.000 A2-3
2008-08-09 12:00:00.000 B1-1
2008-08-09 14:00:00.000 B2-2(7 行受影响)
?
goinsert testtable
select '2008.08.08 07:00:00','A1-1' union all
select '2008.08.08 08:00:00','A2-1' union all
select '2008.08.08 09:00:00','A2-2' union all
select '2008.08.08 10:00:00','A1-2' union all
select '2008.08.08 11:00:00','A1-3' union all
select '2008.08.08 12:00:00','B1-1' union all
select '2008.08.08 13:00:00','B1-2' union all
select '2008.08.08 14:00:00','B2-2' union all
select '2008.08.09 07:00:00','A1-1' union all
select '2008.08.09 08:00:00','A1-2' union all
select '2008.08.09 09:00:00','A2-1' union all
select '2008.08.09 10:00:00','A2-2' union all
select '2008.08.09 11:00:00','A2-3' union all
select '2008.08.09 12:00:00','B1-1' union all
select '2008.08.09 13:00:00','B2-1' union all
select '2008.08.09 14:00:00','B2-2'
go--创建得到日期函数CREATE FUNCTION dayfiled (@inputdate datetime)
RETURNS varchar(2)
AS
begin
declare @redayfild varchar(2)
select @redayfild = case (datename(day,@inputdate)) when '1' then '0' + datename(day,@inputdate)
when '2' then '0' + datename(day,@inputdate)
when '3' then '0' + datename(day,@inputdate)
when '4' then '0' + datename(day,@inputdate)
when '5' then '0' + datename(day,@inputdate)
when '6' then '0' + datename(day,@inputdate)
when '7' then '0' + datename(day,@inputdate)
when '8' then '0' + datename(day,@inputdate)
when '9' then '0' + datename(day,@inputdate)
else datename(day,@inputdate)
end
RETURN(@redayfild)
end
--SQL Code:
select e.[date],e.[name] from (
select datetimefiled,max(namefield) namefield from (
select datetimefiled,namefield,left(namefield,2)namefield2 from(
select datename(year,[date])+'.'+datename(month,[date])+'.'+dbo.dayfiled([date]) datetimefiled,left([name],charindex('-',[name],0)-1) + right([name],len([name])-charindex('-',[name],0)) namefield from testtable
) a
)b
group by datetimefiled,namefield2)d
left join (
select [date] ,[name],datename(year,[date])+'.'+datename(month,[date])+'.'+dbo.dayfiled([date]) datetimefiled,left([name],charindex('-',[name],0)-1) + right([name],len([name])-charindex('-',[name],0)) namefield from testtable
) e
on d.datetimefiled = e.datetimefiled and d.namefield = e.namefield
order by [date]--得到的结果:date name
-------------------------------
2008-08-08 09:00:00.000 A2-2
2008-08-08 11:00:00.000 A1-3
2008-08-08 13:00:00.000 B1-2
2008-08-08 14:00:00.000 B2-2
2008-08-09 08:00:00.000 A1-2
2008-08-09 11:00:00.000 A2-3
2008-08-09 12:00:00.000 B1-1
2008-08-09 14:00:00.000 B2-2