0
,如果我有以下數據表SQL獲得最高的價值和串聯
ProductID,StockOnSite,StockOffsite
1 83 81
1 98 85
1 112 101
2 81 85
2 115 83
2 115 101
我需要做的就是讓每個產品ID的最高StockOnSite(計算StockDifference)記錄和concatinate StockOnSite與StockOffsite創建列StockInfo
輸出需要
ProductID,StockOnSite,StockOffsite,StockDifference,StockInfo
1 98 85 13 98/85
2 115 83 32 115/83
SQL我已經想出
select ProductID
,StockOnSite
,StockOffsite
,StockDifference = max(StockOnSite - StockOffsite)
from Product
group by ProductID, StockOnSite, StockOffsite
我不知道該從哪裏出發?
感謝
下面是查詢建表和數據:
CREATE TABLE Products
(
ProductID int NOT NULL,
StockOnSite int NOT NULL,
StockOffsite int NOT NULL
)
insert into table Products(ProductID, StockOnSite, StockOffsite) values(1,83,81)
insert into table Products(ProductID, StockOnSite, StockOffsite) values(1,98,85)
insert into table Products(ProductID, StockOnSite, StockOffsite) values(1,112,101)
insert into table Products(ProductID, StockOnSite, StockOffsite) values(2,81,85)
insert into table Products(ProductID, StockOnSite, StockOffsite) values(2,115,83)
insert into table Products(ProductID, StockOnSite, StockOffsite) values(2,115,101)
你爲什麼選98爲產品1,當有也115?是因爲你在尋找最大的差異嗎? – dasblinkenlight 2014-10-17 09:04:02
你正在使用哪些DBMS? Postgres的?甲骨文? – 2014-10-17 09:12:42
ProductID 1的差異是98-85 = 13 – user142617 2014-10-17 09:14:36