,
我想根据最新交易日期和上一交易日期,通过国家和城市获得价格上涨百分比。
我如何查询?我没有得到它。这是我尝试过的:
SELECT Country,City, Price
From tbl
Group by Country,City
增长百分比= [(最新价格 - 前一个价格)/上一个价格] * 100
预计出局: 独特的国家和城市名称+价格增长百分比 国家|城市|百分比
答案 0 :(得分:0)
使用lag()
:
select t.*,
100 * ((price / lag(price) over (partition by country, city order by t_date) - 1) as increase
from t;
答案 1 :(得分:0)
这可能过于复杂。
设置一些随机数据:
IF OBJECT_ID('tempdb..#Cities') IS NOT NULL
BEGIN
DROP TABLE #Cities;
END;
CREATE TABLE #Cities
(
Country VARCHAR(20)
, City VARCHAR(20)
);
IF OBJECT_ID('tempdb..#Data') IS NOT NULL
BEGIN
DROP TABLE #Data;
END;
CREATE TABLE #Data
(
Country VARCHAR(20)
, City VARCHAR(20)
, Price DECIMAL(13, 4)
, Date DATETIME
);
INSERT INTO #Cities
VALUES ('Country 1', 'City 1'), ('Country 1', 'City 2'), ('Country 1', 'City 3'), ('Country 2', 'City 4'), ('Country 2', 'City 5');
INSERT INTO #Data
SELECT Country
, City
, ROUND(RAND(CHECKSUM(NEWID())) * 100, 4) AS Price
, DATEADD(DAY, ROUND(RAND(CHECKSUM(NEWID())) * 10, 0), GETDATE()) AS Date
FROM #Cities
UNION
SELECT Country
, City
, ROUND(RAND(CHECKSUM(NEWID())) * 100, 4)
, DATEADD(DAY, ROUND(RAND(CHECKSUM(NEWID())) * 10, 0), GETDATE())
FROM #Cities;
--Delete duplicate dates
WITH data3 AS
(
SELECT *,ROW_NUMBER() OVER (PARTITION BY Country,City,Date ORDER BY Country,City,Date) AS RN
FROM #Data
)
DELETE FROM data3 WHERE RN<>1
查询数据以获取最新价格,日期和百分比增加:
SELECT Dates.*
, Latest.Price AS Latestprice
, Previous.Price AS Previousprice
, ((Latest.Price - Previous.Price) / Previous.Price) * 100 AS Percentageincrease
FROM
(
SELECT C.*
, Latestdate.Latestdate
, Previousdate.Previousdate
FROM #Cities AS C
LEFT JOIN
(
--Latest Date for each county, city
SELECT Country
, City
, MAX(Date) AS Latestdate
FROM #Data
GROUP BY Country
, City
) AS Latestdate ON Latestdate.Country = C.Country
AND Latestdate.City = C.City
LEFT JOIN
(
--Previous Date for each county, city
SELECT Country
, City
, Date AS Previousdate
FROM
(
SELECT Country
, City
, Date
, RANK() OVER(PARTITION BY Country
, City ORDER BY Date DESC) AS Rank
FROM #Data
) AS A
WHERE Rank = 2
) AS Previousdate ON Previousdate.Country = C.Country
AND Previousdate.City = C.City
) AS Dates
JOIN #Data AS Latest ON Latest.Country = Dates.Country
AND Latest.City = Dates.City
AND Latest.Date = Dates.Latestdate
JOIN #Data AS Previous ON Previous.Country = Dates.Country
AND Previous.City = Dates.City
AND Previous.Date = Dates.Previousdate
为了进行比较,使用lag()来获得每个日期的百分比增长。与戈登的回答类似:
SELECT D.Country
, D.City
, D.Date
, Lag(Date) OVER(PARTITION BY Country
, City ORDER BY Date) AS Previousdate
, D.Price
, Lag(Price) OVER(PARTITION BY Country
, City ORDER BY Date) AS Previousprice
, 100 * (Price / Lag(Price) OVER(PARTITION BY Country
, City ORDER BY Date) - 1) AS PercentageIncrease
FROM #Data AS D;
使用滞后来获得与我的第一个查询相同的结果(每个城市的最新信息):
SELECT *
FROM
(
SELECT D.Country
, D.City
, D.Date
, Lag(Date) OVER(PARTITION BY Country
, City ORDER BY Date) AS Previousdate
, D.Price
, Lag(Price) OVER(PARTITION BY Country
, City ORDER BY Date) AS Previousprice
, 100 * (Price / Lag(Price) OVER(PARTITION BY Country
, City ORDER BY Date) - 1) AS Percentageincrease
, ROW_NUMBER() OVER(PARTITION BY Country
, City ORDER BY Date DESC) AS Rn
FROM #Data AS D
) AS A
WHERE Rn = 1
ORDER BY Country
, City;