‘壹’ sql查询取分组最大值
selectt1.分类,t1.商品名称,table.库存,t1.更新时间
from (
select 分类,商品名称,max(更新时间) 更新时间 from table
group by 分类,商品名称
) t1
inner join table on t1.分类=table.分类 and t1.商品名称=table.商品名称 and t1.更新时间
=table.更新时间
(1)sql选取最大值扩展阅读:
关于SQL最大值查询语句的记录
SELECT
bsnum,
STATE AS FILE_STATE
FROM
FILE_RECORD r
WHERE
STATE =(
SELECT
MAX(STATE)
FROM
FILE_RECORD
WHERE
bsnum = r.bsnum
);
小技巧:有时需要测试sql语法但又觉得创建表麻烦,可以使用创建一个临时表,如下:
select id, count(t.id) from
(
select 1 as id, 2 as age, 'F' as sex from al union
select 2 as id, 2 as age, 'M' as sex from al union
select 3 as id, 3 as age, 'F' as sex from al union
select 4 as id, 4 as age, 'M' as sex from al
)t
‘贰’ sql 语句 获取最大值
sql 语句获取一列中的最大值使用MAX() 函数
一、MAX() 函数返回指定列的最大值。
二、SQL MAX() 语法:
SELECT MAX(column_name) FROM table_name;
三、参数解释
column_name:列名
table_name:表名
(2)sql选取最大值扩展阅读:
最小值语句
MIN() 函数
MIN() 函数返回指定列的最小值。
SQL MIN() 语法
SELECT MIN(column_name) FROM table_name;
参考资料:网络-SQL MAX
‘叁’ sql如何对这种情况取最大值
select t.department_id,
max(salary) salary
from (select department_id,
avg(salary) salary
from emloyees
group by department_id
having count(1) > 1) t
group by t.department_id;
聚合函数是不能套用的,只能通过子查询的方式来处理
注:having count 我把 * 改成 1 了,这样效率能高一些
‘肆’ SQL选出一列中最大的数怎么写
在本机测试过了
select * from
(select skb.学号,sum(成绩) as sum_score,avg(成绩) as avg_score from skb group by 学号) tmp left outer join student on student.学号=tmp.学号 where sum_score=(select max(sum_score) from (select skb.学号,sum(成绩) as sum_score,avg(成绩) as avg_score from skb group by 学号) a)
‘伍’ SQL 中取最大值
select
name,address,max(status)
from
表名
group
by
name
‘陆’ SQL中如何选择表中日期最大值
给你个思路:
WITHMessages(ID,ToEmpID,FromEmpID,SendDate,Detail)AS
(
SELECT1,'张三','李四','2014/06/05','a'UNIONALL
SELECT2,'王五','张三','2014/06/03','b'UNIONALL
SELECT3,'刘六','张三','2014/06/02','b'UNIONALL
SELECT4,'张三','李四','2014/05/05','a'UNIONALL
SELECT5,'王五','张三','2014/05/03','b'UNIONALL
SELECT6,'刘六','张三','2014/05/02','b'
)
SELECTID,ToEmpID,FromEmpID,SendDateFROMMessagesa
WHEREIDin
(
SELECTTOP1ID
FROMMessages
whereToEmpID=a.ToEmpIDANDFromEmpID=a.FromEmpID
ORDERBYSendDateDESC
)
‘柒’ sql取最大值和最小值
select
g_table.max_so2
,t_so2.date
,g_table.min_so2
,t_so2_min.date
....
(
select
max(so2) max_so2
,min(so2) min_so2
,max(pm2.5) max_pm25
,min(pm2.5) min_pm25
...
,max(co) max_no2
,min(co) min_co
from table_name
) g_table
,table_name t_so2
,table_name t_so2_min
...
where g_table.max_so2 = t_so2.so2(+)
and g_table.min_so2 = t_so2_min.so2(+)
....
你这个需求有点费劲,这样能实现,但是效率很低。
‘捌’ SQL选择最大值
select
姓名,max(Score)
from
(
select 姓名,语文 Score from 成绩表
union
select 姓名,数学 from 成绩表
union
select 姓名,英语 from 成绩表
union
select 姓名,政治 from 成绩表
union
select 姓名,历史 from 成绩表
) a
group by
姓名
‘玖’ SQL查询问题,取最大值
你这个用一条语句还不太好写
试试这个,不过 First 函数不是 SQL 函数,只有 Access 支持
SELECT First(id) AS ID, [wordID], MAX([price]) AS price, time
FROM [tablename]
GROUP BY [wordID],[time]
ORDER BY [time]
建议你用存储过程来写
‘拾’ SQL怎么提取字段中最大值
1、既然被for xml path('')组合到一起了,那就想办法把这个字符串拆开;
2、可以创建一个Split这样的函数,来拆开这一列为一个表的形式;
3、然后,再查出这个表里的最大值即可
下面是Ufn_Split函数
createfunction[dbo].[Ufn_Split](@Arrayvarchar(max),@StrSepratevarchar(10))
returns@TokenValstable(fjj_idINTIDENTITY(1,1),strValuevarchar(255))
as
begin
declare@strlenint,@curposint,@valvarchar(255),@nextcommaint
set@strlen=datalength(@Array)
set@curpos=0
while@curpos<=@strlen
begin
set@nextcomma=charindex(@StrSeprate,@Array,@curpos)
if@nextcomma=0
set@nextcomma=@strlen+1
set@val=substring(@Array,@curpos,@nextcomma-@curpos)
insert@TokenValsvalues(rtrim(ltrim(@val)))
set@curpos=@nextcomma+1
end
return
end
函数使用方法:
select*fromdbo.Ufn_Split('1+2+3+5+1','+')table1