㈠ 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關鍵字選擇前兩條
祝好運,望採納