34,592
社区成员
发帖
与我相关
我的任务
分享
select old.id,t.公司名数量,old.主题,t.录入时间,t.录入人
from test as old
inner join (select 公司名, 公司名+'('+convert(nvarchar(5),count(id))+')' as 公司名数量
,max(录入时间) as 录入时间,录入人
from test
group by 公司名,录入人) as t
on old.公司名 = t.公司名 and old.录入时间 = t.录入时间
and old.录入人 = t.录入人
order by old.id
declare @time datetime
set @time=(select max(录入时间) from tbname)
select * from test where 录入时间=@time
select count(distinct(录入人)) from tb where 公司名='A公司'
---------------------------------
-- Author: liangCK 小梁
-- Date : 2008-12-03 14:55:21
---------------------------------
--> 生成测试数据: @T
DECLARE @T TABLE (ID INT,公司名 VARCHAR(5),主题 VARCHAR(3),录入时间 DATETIME,录入人 VARCHAR(4))
INSERT INTO @T
SELECT 1,'A公司','aaa','2008-10-1','张三' UNION ALL
SELECT 2,'A公司','bbb','2008-10-3','张三' UNION ALL
SELECT 3,'B公司','ccc','2008-10-3','张三' UNION ALL
SELECT 4,'A公司','eee','2008-10-1','李四' UNION ALL
SELECT 5,'A公司','fff','2008-10-2','李四' UNION ALL
SELECT 6,'B公司','ggg','2008-10-2','李四'
--SQL查询如下:
SELECT
ID,
公司名=公司名+'('+
RTRIM((SELECT COUNT(*)
FROM @T
WHERE 公司名=A.公司名
AND 录入人=A.录入人))+')',
主题,
录入时间,
录入人
FROM @T AS A
WHERE NOT EXISTS(
SELECT *
FROM @T
WHERE 公司名=A.公司名
AND 录入人=A.录入人
AND 录入时间>A.录入时间
)
ORDER BY ID
/*
ID 公司名 主题 录入时间 录入人
----------- ------------------- ---- ----------------------- ----
2 A公司(2) bbb 2008-10-03 00:00:00.000 张三
3 B公司(1) ccc 2008-10-03 00:00:00.000 张三
5 A公司(2) fff 2008-10-02 00:00:00.000 李四
6 B公司(1) ggg 2008-10-02 00:00:00.000 李四
(4 行受影响)
*/
if object_id('tb')is not null drop table tb
go
create table tb(ID int, 公司名 nvarchar(10), 主题 varchar(5), 录入时间 datetime, 录入人 nvarchar(5))
insert tb select 1 , N'A公司', 'aaa' , '2008-10-1' , N'张三'
insert tb select 2 , N'A公司' , 'bbb' , '2008-10-3' , N'张三'
insert tb select 3 , N'B公司' ,'ccc' , '2008-10-3' , N'张三'
insert tb select 4 , N'A公司' ,'eee' , '2008-10-1' , N'李四'
insert tb select 5 , N'A公司' ,'fff ' , '2008-10-2' , N'李四'
insert tb select 6, N'B公司' ,'ggg', '2008-10-2' , N'李四'
select * from tb t where not exists(select 1 from tb where 公司名=t.公司名 and 录入人=t.录入人 and 录入时间>t.录入时间 )
/*
ID 公司名 主题 录入时间 录入人
----------- ---------- ----- ------------------------------------------------------ -----
2 A公司 bbb 2008-10-03 00:00:00.000 张三
3 B公司 ccc 2008-10-03 00:00:00.000 张三
5 A公司 fff 2008-10-02 00:00:00.000 李四
6 B公司 ggg 2008-10-02 00:00:00.000 李四
*/
create table tb(ID int, 公司名 varchar(10), 主题 varchar(10), 录入时间 datetime, 录入人 varchar(10))
insert into tb values(1 , 'A公司' , 'aaa' , '2008-10-1' , '张三')
insert into tb values(2 , 'A公司' , 'bbb' , '2008-10-3' , '张三')
insert into tb values(3 , 'B公司' , 'ccc' , '2008-10-3' , '张三')
insert into tb values(4 , 'A公司' , 'eee' , '2008-10-1' , '李四')
insert into tb values(5 , 'A公司' , 'fff' , '2008-10-2' , '李四')
insert into tb values(6 , 'B公司' , 'ggg' , '2008-10-2' , '李四')
go
select * , cnt = (select count(*) from tb where 录入人 = t.录入人 and 公司名 = t.公司名) from tb t where 录入时间 = (select max(录入时间) from tb where 录入人 = t.录入人 and 公司名 = t.公司名) order by t.id
drop table tb
/*
ID 公司名 主题 录入时间 录入人 cnt
----------- ---------- ---------- ------------------------------------------------------ ---------- -----------
2 A公司 bbb 2008-10-03 00:00:00.000 张三 2
3 B公司 ccc 2008-10-03 00:00:00.000 张三 1
5 A公司 fff 2008-10-02 00:00:00.000 李四 2
6 B公司 ggg 2008-10-02 00:00:00.000 李四 1
(所影响的行数为 4 行)
*/
--建立测试环境
set nocount on
create table test(ID varchar(20),公司名 varchar(20),主题 varchar(20),录入时间 varchar(20),录入人 varchar(20))
insert into test select '1','A公司','aaa','2008-10-1','张三'
insert into test select '2','A公司','bbb','2008-10-3','张三'
insert into test select '3','B公司','ccc','2008-10-3','张三'
insert into test select '4','A公司','eee','2008-10-1','李四'
insert into test select '5','A公司','fff','2008-10-2','李四'
insert into test select '6','B公司','ggg','2008-10-2','李四'
go
--测试
select *,(select count(*) from test where 公司名=a.公司名
and 录入人=a.录入人) as 次数 from test a
where not exists(select 1 from test where 公司名=a.公司名
and 录入人=a.录入人 and 录入时间>a.录入时间)
--删除测试环境
drop table test
set nocount off
/*
2 A公司 bbb 2008-10-3 张三 2
3 B公司 ccc 2008-10-3 张三 1
5 A公司 fff 2008-10-2 李四 2
6 B公司 ggg 2008-10-2 李四 1
*/
select T.*,T1.num
from (select 公司名,录入人,count(1) as num,Max(录入时间) as 录入时间 from 表 group by 公司名,录入人) T1
inner join 表 T on T.公司名=T1.公司名 and T.录入人=T1.录入人 and T.录入时间=T1.录入时间
select * from tb t where not exists(select 1 from tb where 公司名=t.公司名 and 录入人=t.录入人 and 录入时间>t.录入时间 )
--按某一字段分组取最大(小)值所在行的数据
--(爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 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')
go
select * , px = identity(int,1,1) into tmp from tb
select 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')
go
select 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 行受影响)
*/
select * from tb t where 录入时间 = (select max(录入时间) from tb where 录入人 = t.录入人 and 公司名 = t.公司名)