表className中有如下分类:classID className
1 衣服
2 裤子
5 帽子
10 鞋子表productInfo有如下记录:productID productName parentID clickNum1 男士衣服 1 90 --衣服类别中这条记录的点击率最高
2 女士衣服 1 80
3 男士裤子 2 70
4 女士裤子 2 90 --裤子类别中这条记录点击率最高
5 男士帽子 5 15
6 女士帽子 5 30 --帽子类别中这条点击率最高
7 男士鞋子 10 65 --鞋子类别中这条点击率最高
8 女士鞋子 10 52
9 女士鞋子1 10 54现在要求分别把衣服,裤子,帽子,鞋子这些类别中点击率最高的一条记录找出来,然后再降序排列,结果应如下:productID productName clickNum
1 男士衣服 90
4 女士裤子 90
7 男士鞋子 65
6 女士帽子 30
1 衣服
2 裤子
5 帽子
10 鞋子表productInfo有如下记录:productID productName parentID clickNum1 男士衣服 1 90 --衣服类别中这条记录的点击率最高
2 女士衣服 1 80
3 男士裤子 2 70
4 女士裤子 2 90 --裤子类别中这条记录点击率最高
5 男士帽子 5 15
6 女士帽子 5 30 --帽子类别中这条点击率最高
7 男士鞋子 10 65 --鞋子类别中这条点击率最高
8 女士鞋子 10 52
9 女士鞋子1 10 54现在要求分别把衣服,裤子,帽子,鞋子这些类别中点击率最高的一条记录找出来,然后再降序排列,结果应如下:productID productName clickNum
1 男士衣服 90
4 女士裤子 90
7 男士鞋子 65
6 女士帽子 30
from productinfo a
where not exists(select 1 from productinfo where a.parentid = parentid and clicknum > a.clicknum)
from classname c
join
(select * from productinfo p where clicknum=(select max(clicknum) from productinfo where parentid=p.parentid)) p
on c.classid=p.parentid
order by p.clicknum desc--这样子吗?
If object_id('ta') is not null
Drop table ta
Go
Create table ta(productID int,productName varchar(14),parentID int,clickNum int)
Go
Insert into ta
select 1,'男士衣服',1,90 union all
select 2,'女士衣服',1,80 union all
select 3,'男士裤子',2,70 union all
select 4,'女士裤子',2,90 union all
select 5,'男士帽子',5,15 union all
select 6,'女士帽子',5,30 union all
select 7,'男士鞋子',10,65 union all
select 8,'女士鞋子',10,52 union all
select 9,'女士鞋子1',10,54
Go
--Start
select *
from ta a
where not exists(select 1 from ta where a.parentid = parentid and clicknum > a.clicknum)
--Result:
/*
productID productName parentID clickNum
----------- -------------- ----------- -----------
1 男士衣服 1 90
4 女士裤子 2 90
6 女士帽子 5 30
7 男士鞋子 10 65(所影响的行数为 4 行)*/
--End
if object_id('tempdb.dbo.#className') is not null drop table #className
create table #className (classID int,className varchar(4))
insert into #className
select 1,'衣服' union all
select 2,'裤子' union all
select 5,'帽子' union all
select 10,'鞋子'
--> 测试数据: #productInfo
if object_id('tempdb.dbo.#productInfo') is not null drop table #productInfo
create table #productInfo (productID int,productName nvarchar(9),parentID int,clickNum int)
insert into #productInfo
select 1,'男士衣服',1,90 union all
select 2,'女士衣服',1,80 union all
select 3,'男士裤子',2,70 union all
select 4,'女士裤子',2,90 union all
select 5,'男士帽子',5,15 union all
select 6,'女士帽子',5,30 union all
select 7,'男士鞋子',10,65 union all
select 8,'女士鞋子',10,52 union all
select 9,'女士鞋子1',10,54select p.productID,p.productName,p.clickNum
from #classname c
join
(select * from #productInfo p where clickNum=(select max(clickNum) from #productInfo where parentID=p.parentID)) p
on c.classID=p.parentID
order by p.clickNum desc/*
productID productName clickNum
----------- ----------- -----------
4 女士裤子 90
1 男士衣服 90
7 男士鞋子 65
6 女士帽子 30(4 行受影响)
*/
我想请问happyflystone 这是什么思路啊 ,能讲讲么 我对exist最头痛了 请详细讲讲 谢谢
select *
from ta a
where not exists(select 1 from ta where a.parentid = parentid and clicknum > a.clicknum)
(爱新觉罗.毓华 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
/*
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
/*
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 行受影响)
*/
insert into ta select 1,1
insert into ta select 2,1
insert into ta select 3,1
insert into ta select 4,1
insert into ta select 5,2
insert into ta select 4,2
insert into ta select 3,2
insert into ta select 2,2
go
select *
from ta a
where not exists(select 1 from ta where col = a.col and id > a.id)
from ta a
where not exists(select 1 from ta where col = a.col and id < a.id)
select *
from ta a
where not exists(select 1 from ta where col = a.col and id < a.id)
这个对的
我这个为什么错 select * from productinfo a where a.parentid = productinfo.parentid and productinfo.clicknum > a.clicknum
列前缀 'productinfo' 与查询中所用的表名或别名不匹配。
列前缀 'productinfo' 与查询中所用的表名或别名不匹配。哪里不匹配了????
If object_id('test') is not null
Drop table test
Go
Create table test(productID int,productName varchar(14),parentID int,clickNum int)
Go
Insert into test
select 1,'男士衣服',1,90 union all
select 2,'女士衣服',1,80 union all
select 3,'男士裤子',2,70 union all
select 4,'女士裤子',2,90 union all
select 5,'男士帽子',5,15 union all
select 6,'女士帽子',5,30 union all
select 7,'男士鞋子',10,65 union all
select 8,'女士鞋子',10,52 union all
select 9,'女士鞋子1',10,54
Go
SELECT * FROM test WHERE clickNum IN(SELECT max(clicknum) AS num FROM test GROUP BY parentID) ORDER BY clickNum DESC
/*(所影响的行数为 4 行)
1 男士衣服 1 90
4 女士裤子 2 90
7 男士鞋子 10 65
6 女士帽子 5 30*/
Drop table test