with tb(名字,科目,成绩) as( select '小明','語文',90 union all select '小明','數學',85 union all select '小明','英語',80 union all select '小紅','語文',80 union all select '小紅','數學',85 union all select '小紅','英語',70 ) select 名字,max(科目),min(成绩) from tb tb1 group by 名字
select * from tb t where exists(select 1 from tb where t.分数=min(分数) or t.分数=max(分数) )
select * from tb t where exists(select top 1 WITH TIES 1 from tb where 科目=t.科目 order by 分数 asc) 修改如上。
7樓的語句: with tb(名字,科目,成绩) as( select '小明','語文',90 union all select '小明','數學',85 union all select '小明','英語',80 union all select '小紅','語文',80 union all select '小紅','數學',85 union all select '小紅','英語',70 ) select * from tb t where exists(select top 1 WITH TIES 1 from tb where 科目=t.科目 order by 成绩 asc) 結果為: 名字 科目 成绩 小明 語文 90 小明 數學 85 小明 英語 80 小紅 語文 80 小紅 數學 85 小紅 英語 70
汗。明白楼主的意思了。 上面大致改改也就行了。 SELECT * FROM TB T WHERE 分数=(SELECT MIN(分数) FROM TB WHERE 姓名=T.姓名)
謝謝SQL77,,大致解決了,現在需要消化一下,下次有問題還需你幫忙!!!!
按照名字與科目來算出每一門的最低成績SELECT 名字,科目,MIN(成績) FROM TABLE1 GROUP BY 名字,科目
先按每个人的分数排序:然后求最低分 with tb(name,kemu,score) as( select '小明','語文',90 union all select '小明','數學',85 union all select '小明','英語',80 union all select '小紅','語文',80 union all select '小紅','數學',85 union all select '小紅','英語',70 ) SELECT name,kemu,score FROM ( SELECT *,scoreIndex=(SELECT COUNT(DISTINCT i.score) FROM tb i WHERE i.name=tb.name AND i.score<tb.score) FROM tb )a WHERE a.scoreIndex=0
这个问题一分析其实就是分组求前几名的问题. 咱也提供一种方案吧:使用apply,有个好处就是除了能查最小那个,还能查最小的那二个. with tb(名字,科目,成绩) as( select '小明','語文',90 union all select '小明','數學',85 union all select '小明','英語',80 union all select '小紅','語文',80 union all select '小紅','數學',85 union all select '小紅','英語',70 )
select k.* from (select distinct 名字 from tb) as a cross apply (select top 1 * from tb as b where a.名字 = b.名字 order by b.成绩) as k
select name,kemu,score from (select name ,kemu,score ,ROW_NUMBER() over(partition by name order by score ) as fz from tb) t where t.fz=1 /* name kemu score 小紅 英語 70 小明 英語 80 */
SELECT * FROM TB a WHERE NOT EXISTS(SELECT 1 FROM TB b WHERE a.科目=b.科目 AND b.成绩<a.成绩)
as(
select '小明','語文',90 union all
select '小明','數學',85 union all
select '小明','英語',80 union all
select '小紅','語文',80 union all
select '小紅','數學',85 union all
select '小紅','英語',70
)
select 名字,max(科目),min(成绩) from tb tb1 group by 名字
where exists(select 1 from tb where t.分数=min(分数) or t.分数=max(分数) )
where exists(select top 1 WITH TIES 1 from tb where 科目=t.科目 order by 分数 asc) 修改如上。
with tb(名字,科目,成绩)
as(
select '小明','語文',90 union all
select '小明','數學',85 union all
select '小明','英語',80 union all
select '小紅','語文',80 union all
select '小紅','數學',85 union all
select '小紅','英語',70
)
select * from tb t
where exists(select top 1 WITH TIES 1 from tb where 科目=t.科目 order by 成绩 asc)
結果為:
名字 科目 成绩
小明 語文 90
小明 數學 85
小明 英語 80
小紅 語文 80
小紅 數學 85
小紅 英語 70
上面大致改改也就行了。
SELECT * FROM TB T WHERE 分数=(SELECT MIN(分数) FROM TB WHERE 姓名=T.姓名)
FROM TABLE1
GROUP BY 名字,科目
with tb(name,kemu,score)
as(
select '小明','語文',90 union all
select '小明','數學',85 union all
select '小明','英語',80 union all
select '小紅','語文',80 union all
select '小紅','數學',85 union all
select '小紅','英語',70
)
SELECT name,kemu,score FROM (
SELECT *,scoreIndex=(SELECT COUNT(DISTINCT i.score) FROM tb i WHERE i.name=tb.name AND i.score<tb.score) FROM tb
)a
WHERE a.scoreIndex=0
咱也提供一种方案吧:使用apply,有个好处就是除了能查最小那个,还能查最小的那二个.
with tb(名字,科目,成绩)
as(
select '小明','語文',90 union all
select '小明','數學',85 union all
select '小明','英語',80 union all
select '小紅','語文',80 union all
select '小紅','數學',85 union all
select '小紅','英語',70
)
select k.*
from (select distinct 名字 from tb) as a
cross apply (select top 1 * from tb as b where a.名字 = b.名字 order by b.成绩) as k
select name,kemu,score from
(select name ,kemu,score ,ROW_NUMBER() over(partition by name order by score ) as fz from tb) t where t.fz=1
/*
name kemu score
小紅 英語 70
小明 英語 80
*/
SELECT * FROM TB a WHERE NOT EXISTS(SELECT 1 FROM TB b WHERE a.科目=b.科目 AND b.成绩<a.成绩)