Ⅰ sql更新最小值所在行的数据
--楼主这个需求 不是很难 需要指出的是:已经有Check=1的数据,则不需再更新
--没有必要这么判断 把1更新为1 并没什么影响厅碰 而且如果要排除扮或谈的话 语句会更长 影响性能
--处理思想就是 不用判断 满足条件就更新 你只需把tbname 改为你的表名就可以了
--有什么问题可以随时问我 希望团模采纳
with tmp as(select pn,price,date ,row_number() over (partition by price order by date asc) bz
from (
select pn,price,min(date) date from tbname group by pn,price) a)
update a set a.check=bz
from tbname a join tmp b on a.PN=b.PN and a.price=b.price and a.date=b.date
Ⅱ 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 year month from 表格名
where month=(select Min(month) from 表格名 where year=(select Min(year) from 表格名))
Ⅳ sql 查询一行内几列的平均值,最大值,最小值,怎么写
oracle的话直接用greatest函数,sql server的话照这么写:
select t1.name, max(t1.a) maxnum,min(t1.a) minnum,avg(t1.a) avgnum from
(select name,num1 a from tb1
union all
select name,num2 a from tb1
union all
select name,num3 a from tb1
union all
select name,num4 a from tb1
union all
select name,num5 a from tb1
union all
select name,num6 a from tb1
union all
select name,num7 a from tb1) t1
group by t1.name;
测试数据:
create table tb1(name varchar(50),num1 int,num2 int,num3 int,num4 int,num5 int,num6 int,num7 int)
insert into tb1 select 'Tom',1,2,6,7,9,0,3
insert into tb1 select 'Jacky',7,6,5,4,3,2,1
结果:
name maxnum minnum avgnum
Jacky 7 1 4
Tom 9 0 4
Ⅳ sql 取某些行中某列的最大值和最小值
select top 300 * from 表名 order by desc
select top 300 * from 表名 order by asc
' 为列名
Ⅵ 用SQL语句查询最小值,最大值不能用min,max函数怎么查
1.
--大于等于所有(最大值)
select*fromApo_city
wherecity_id>=all(selectcity_idfromApo_city)
--小于等于所有(最小值)
select*fromApo_city
wherecity_id<=all(selectcity_idfromApo_city)
--2.
--降序取第一个(最大值)
select*fromApo_city
wherecity_id=(selecttop1city_idfromApo_cityorderbycity_iddesc)
--升序取第一个(最小值)
select*fromApo_city
wherecity_id=(selecttop1city_idfromApo_cityorderbycity_idAsc)
--3.
--最大值
selectTop1city_idfromApo_cityorderbycity_iddesc
--最小值
selectTop1city_idfromApo_cityorderbycity_idAsc
--4.
--最大值
WithT
As
(
select*,ROW_NUMBER()over(orderbycity_idDesc)asidfromApo_city
)
select*fromTwhereid=1
--最小值
WithT
As
(
select*,ROW_NUMBER()over(orderbycity_idAsc)asidfromApo_city
)
select*fromTwhereid=1
5.
--不小于任何一个(最大值)
select*fromApo_city
wherenotcity_id<any(selectcity_idfromApo_city)
--不大于任何一个(最小值)
select*fromApo_city
wherenotcity_id>any(selectcity_idfromApo_city)
Ⅶ SQL从查询结果中查最小值
SELECT C.CategoryID, C.CategoryName, SUM(F.CommentNO) AS SumComment
FROM Category AS C, Feedback AS F, Article AS A
WHERE C.CategoryID=A.CategoryID AND A.ArticleID=F.ArticleID
GROUP BY C.CategoryID, C.CategoryName having SUM(F.CommentNO)=
(select min(t1.SumComment1) from
(SELECT SUM(F.CommentNO) AS SumComment1
FROM Category AS C, Feedback AS F, Article AS A
WHERE C.CategoryID=A.CategoryID AND A.ArticleID=F.ArticleID
GROUP BY C.CategoryID, C.CategoryName) as t1)
这样试试
Ⅷ 如何使用sql函数平均值、总数、最小值、最大值、总和、标准差
avg函数:计算查询中某一特定字段资料的算术平均值。
count函数:计算符合查询条件的记录数。
min, max函数:传回指定字段值中符合查询条件的第一条、最末条记录的资料。
first, last函数:传回指定字段值中符合查询条件的最小值、最大值。
stdev函数:计算指定字段值中符合查询条件的标准差。
sum函数:计算指定字段值中符合查询条件的资料总和。
var,函数:计算指定字段值中符合查询条件的变异数估计值。
Ⅸ sql 求最小值 min group by
SELECT yr,min(fp1) as fp1 FROM pmyst where stcd='70221400 'group by yr order by min(fp1) desc
SQL stcd 这个字段是什么类型啊
如果是字符就但单引
如没宽果是数袭困字型的 你的SQL就没有错误拍察念 完全可以执行的
Ⅹ 求sql语句 多列取最小值
请查阅这里:求最小值的方法
里面举三个例子:
1 使用values子句生成临时表
2使用行列转换
3使用union all拼接临时表
createtabletest
(namevarchar(10),time1int,time2int,time3int)
insertintotest(name,time1,time2,time3)
values
('a',1,2,3), ('b',8,9,6), ('c',11,22,8), ('d',101,201,38),
('e',6,7,9), ('f',8,8,13), ('g',2,2,30), ('h',82,56,53)
go
---方法1:使用values子句构建临时表
selectname,(selectmin(timeMin)from(values(time1),(time2),(time3))as#temp(timeMin))astimeMinfromtest
---方法2行转列
selectname,min(timeMin)as[最小数]fromtestunpivot(timeMinfortimeMintin(time1,time2,time3))asugroupbyname
--方法3:使用unionall组合新表
selectname,(selectmin(timeMin)
as[最小数]from(
selecttest.time1astimeMin
unionall
selecttest.time2
unionall
selecttest.time3)ud)
MaxDatefromtest
go
droptabletest
如有疑问,及时沟通!