导航:首页 > 贷款资金 > 数据库查询最高价格

数据库查询最高价格

发布时间:2021-03-24 01:51:53

㈠ SQL查询最高价

create table 表 (
日期 varchar(8),
id int,
名称 varchar(10),
进货数量 int,
进货价格 money,
零售数量 int,
零售价格 money)
go

insert into 表
select '20110301',101,'笔芯',500,0.5,0,0 union all
select '20110302',102,'尺子',300,0.8,0,0 union all
select '20110303',103,'记事本',400,1.2,0,0 union all
select '20110304',102,'尺子',0,0,50,1.0 union all
select '20110305',103,'记事本',0,0,60,1.5
go

select e.最新日期,e.id,e.名称,h.平均价格,h.最高价格,h.最低价格,e.最新价格 from

(
select distinct c.id,d.名称,c.最新日期,case when d.进货价格>0 then d.进货价格 else d.零售价格 end as 最新价格
from
(
select a.id,case when a.ma>b.mb or b.mb is null then a.ma else b.mb end as 最新日期
from
(select id,max(convert(datetime,日期))as ma from 表 where 进货价格>0 group by id) a left join
(select id,max(convert(datetime,日期))as mb from 表 where 零售价格>0 group by id) b
on a.id=b.id
)c left join 表 d on c.id=d.id and c.最新日期=convert(datetime,d.日期)
)e left join
(
select f.id,(f.进货总价+case when g.零售总价 is null then 0 else g.零售总价 end)/(f.进货总数量+case when g.零售总数量 is null then 0 else g.零售总数量 end) as 平均价格,
case when f.最高进货价格>g.最高零售价格 or g.最高零售价格 is null then f.最高进货价格 else g.最高零售价格 end as 最高价格,
case when f.最低进货价格<g.最低零售价格 or g.最高零售价格 is null then f.最低进货价格 else g.最低零售价格 end as 最低价格
from
(select id,max(进货价格) as 最高进货价格,min(进货价格) as 最低进货价格,sum(进货价格*进货数量)as 进货总价,sum(进货数量) as 进货总数量 from 表 where 进货价格>0 group by id) f left join
(select id,max(零售价格) as 最高零售价格,min(零售价格) as 最低零售价格,sum(零售价格*零售数量)as 零售总价,sum(零售数量) as 零售总数量 from 表 where 零售价格>0 group by id) g
on f.id=g.id
) h on e.id=h.id

运行结果如下:

(所影响的行数为 5 行)

最新日期 id 名称 平均价格 最高价格 最低价格 最新价格
------------------------------------------------------ ----------- ---------- --------------------- --------------------- --------------------- ---------------------
2011-03-01 00:00:00.000 101 笔芯 .5000 .5000 .5000 .5000
2011-03-04 00:00:00.000 102 尺子 .8285 1.0000 .8000 1.0000
2011-03-05 00:00:00.000 103 记事本 1.2391 1.5000 1.2000 1.5000

(所影响的行数为 3 行)

㈡ mysql数据库查询图书表,输出每一类图书的最高价格、最低价格平均价格的代码怎

1、首先在mysql数据库,创建一张data表,表内插入多条数据,用于测试。

㈢ 如何在SQL中查询在项目表中检索每一种产品的最高价格。

select prooct ,price from table group by proct
系统会自动为你将产品按价格由高到底排序,

㈣ 怎么用SQL语句查询一件物品的最高价和最低价

update x
set x1=(select sum(1) from a where right(x,1)=1),
x2=(select sum(1) from a where right(x,1)=2),
x3=(select sum(1) from a where right(x,1)=3),
x4=(select sum(1) from a where right(x,1)=4),
x5=(select sum(1) from a where right(x,1)=5)

如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!

vaela

㈤ 数据库中有最高价格,最低价格,怎样用sql语言显示最高价格差

SELECT MAX([价格]) - MIN([价格]) AS [差价]
FROM [表名]

SELECT MAX([最高价格]-[最低价格]) AS [最高价格差]
FROM [表名]

不知道你的表什么样, 你看看哪个符合你的需求

㈥ sql数据库:查询单价最高和最低的图书名称

select top 1 book_name from table order by price asc/desc

㈦ sql数据库查询各物品的最大值

这个意思?

㈧ 数据库中有商品的最高单价,最低单价,但是我想查询价格差最最高的前两个商品,

SELECT TOP 2 *, [最高单价]-[最低单价] AS _t
FROM [表]
ORDER BY _t DESC

㈨ 数据库选择查询如何计算单价最贵

按栏目进行分组( group by 栏目 ),然后再取价格最高的( max(价格) )

㈩ sql 查询价格最高的前两条书籍信息

用top关键字选择前两条
祝好运,望采纳

阅读全文

与数据库查询最高价格相关的资料

热点内容
外汇的交易方式是 浏览:393
易方达理财安全吗 浏览:71
行的理财产品 浏览:256
股票后加盟 浏览:630
国家开发银行生源地贷款还款 浏览:641
中金赛富投资集团 浏览:707
期货交易重点环节 浏览:628
股票股東 浏览:83
赛象科技股票怎么样 浏览:278
九鼎新材股票行情 浏览:807
日本1190是多少人民币 浏览:968
二手房银行贷款承诺函 浏览:705
资金结算单 浏览:747
2019年5月8日甲醇市场价格 浏览:447
欧元贷款林业 浏览:628
资金进出靠谱 浏览:153
民转军工股票 浏览:838
小黑妞投资有限公司 浏览:767
石家庄外汇投资公司 浏览:961
温州鼎力投资担保有限公司 浏览:887