sql-查找最大值

sql-查找最大值,sql,sql-server,greatest-n-per-group,Sql,Sql Server,Greatest N Per Group,我有一个表,有3列:代码、年份、百分比 我需要返回2009年最低(最低)百分比的代码。在此之后,我还需要代码的名称,它存在于我制作的其他表中 我只想使用createview,但我不喜欢这样做 Select table.code, table.year, table.percentage, othertable.name from table inner join othertable on table.FKId = othertable.PKid where year = 2009 an

我有一个表,有3列:代码、年份、百分比

我需要返回2009年最低(最低)百分比的代码。在此之后,我还需要代码的名称,它存在于我制作的其他表中

我只想使用createview,但我不喜欢这样做

Select table.code, table.year, table.percentage, othertable.name
from table
inner join othertable  on table.FKId = othertable.PKid
where year = 2009 
  and percentage = 
  (select min(percentage) 
   from table 
   where year = 2009)
已更新以包含othertable。。。因为我们没有名字

更新 现在我们有了表名。。。第三次更新,因为我知道年份是字符串

Select E.Code, C.Name 
From dbo.Exam E
inner join dbo.Course C
  ON E.Code = C.Code  
Where E.Year = '2009' and --<-- PROBLEM LIKELY HERE year was string not int.
 E.Fail = (select MIN(E2.Fail) 
                  from dbo.Exam E2 where E2.Year = '2009') --<--Don't forget here too.
假设是结果应该是 姓名2,5

已更新以包含othertable。。。因为我们没有名字

更新 现在我们有了表名。。。第三次更新,因为我知道年份是字符串

Select E.Code, C.Name 
From dbo.Exam E
inner join dbo.Course C
  ON E.Code = C.Code  
Where E.Year = '2009' and --<-- PROBLEM LIKELY HERE year was string not int.
 E.Fail = (select MIN(E2.Fail) 
                  from dbo.Exam E2 where E2.Year = '2009') --<--Don't forget here too.
假设是结果应该是
Name2,5

您可以使用
分组依据
查找一年中的最低百分比,然后返回主表以查找相应的其他列:

select  *
from    CodeYearPercTbl cyp
join    CodeTbl c
on      c.Code = cyp.Code
join    (
        select  Year
        ,       min(Percentage) as MinPerc
        from    CodeYearPercTbl
        group by
                Year
        ) as filter
on      filter.Year = cyp.Year
        and filter.MinPerc = cyp.Percentage
where   cyp.Year = 2009

您可以使用
分组依据
查找一年中的最低百分比,然后连接回主表以查找相应的其他列:

select  *
from    CodeYearPercTbl cyp
join    CodeTbl c
on      c.Code = cyp.Code
join    (
        select  Year
        ,       min(Percentage) as MinPerc
        from    CodeYearPercTbl
        group by
                Year
        ) as filter
on      filter.Year = cyp.Year
        and filter.MinPerc = cyp.Percentage
where   cyp.Year = 2009

表1:代码、年份、百分比 表2:代码、代号

select T1.Code,T2.CodeName,T1.Percentage from
(
     select TOP 1 Code,Percentage 
     from Table-1 
     where Year = '2009' 
     order by Percentage asc
) T1  inner join Table-2 T2 on T1.Code = T2.Code

表1:代码、年份、百分比 表2:代码、代号

select T1.Code,T2.CodeName,T1.Percentage from
(
     select TOP 1 Code,Percentage 
     from Table-1 
     where Year = '2009' 
     order by Percentage asc
) T1  inner join Table-2 T2 on T1.Code = T2.Code
试试这个:

select a.Code, c.Name 
from  YourTable a inner join AnotherTable c on a.Code = c.Code 
where a.Percentage = (select MIN(Percentage) 
                      from YourTable  b where b.Year = '2009'
                     )
试试这个:

select a.Code, c.Name 
from  YourTable a inner join AnotherTable c on a.Code = c.Code 
where a.Percentage = (select MIN(Percentage) 
                      from YourTable  b where b.Year = '2009'
                     )


从您的表中选择排名前1的领带代码,其中year=2009 order by percentage
@Oded它总是给我带来视图在my DB中已经定义的问题。我读到这是网络,我解决了它。但是,这个问题时不时地重复。有可能吗?一年中的两个代码可以有相同的百分比吗?既然您要求的是
MIN
,为什么Title
Finding MAX
从您的表中选择带有领带代码的前1名,其中year=2009按百分比排序
@Oded它总是给我视图在我的数据库中定义的问题。我读到这是网络,我解决了它。但是,这个问题时不时地重复。有可能吗?一年中的两个代码可以有相同的百分比吗?既然你要求的是
MIN
,为什么标题
Finding MAX
?这正是我要发布的内容+1对于狙击我:P仍然应该查找代码的名称,但这是一个简单的方法join@xQbert
有什么问题?从表中选择code,min(百分比),其中year=2009按代码分组
@RoyiNamir-它不起作用。它将找到每个代码的最小百分比。nods。。只能返回1个值。不是3。如果2009年,2010年,2011年存在,我只想要2009年;因此不需要分组,这正是我要发布的内容+1对于狙击我:P仍然应该查找代码的名称,但这是一个简单的方法join@xQbert
有什么问题?从表中选择code,min(百分比),其中year=2009按代码分组
@RoyiNamir-它不起作用。它将找到每个代码的最小百分比。nods。。只能返回1个值。不是3。如果2009年,2010年,2011年存在,我只想要2009年;因此,不需要分组。