![]() ![]() ![]() ![]() |
|||||
|
|||||
樓主 阿龍 ![]()
![]() |
我想要找出今天售價大於昨天售價10%的產品, 也就是說昨天若是賣100元,今天賣111元就篩選出來 請問要怎麼寫,資料表格示如下 日期 商品名稱 售價 20210101 AA 100 20210101 BB 10 20210101 CC 20 20210102 AA 111 20210102 BB 9 20210102 CC 21 ..... |
1樓 |
alter procedure up_cnt
as create table #rep ( 日期 datetime, 商品名稱 nvarchar(50), 售價 int) insert #rep select '2021/01/01','AA',100 insert #rep select '2021/01/01','BB',10 insert #rep select '2021/01/01','CC',20 insert #rep select '2021/01/02','AA',111 insert #rep select '2021/01/02','BB',9 insert #rep select '2021/01/02','CC',21 select 商品名稱 from #rep t1 where (售價-(select 售價 from #rep where 商品名稱=t1.商品名稱 and 日期='2021/01/01'))>10 and 日期='2021/01/02'
本篇文章回覆於2021-01-04 17:04
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
2樓
不錯的參考
真的有點難 ![]() |
是比率 我更正一下
alter procedure up_cnt as create table #rep ( 日期 datetime, 商品名稱 nvarchar(50), 售價 int) insert #rep select '2021/01/01','AA',100 insert #rep select '2021/01/01','BB',10 insert #rep select '2021/01/01','CC',20 insert #rep select '2021/01/02','AA',111 insert #rep select '2021/01/02','BB',12 insert #rep select '2021/01/02','CC',21 select t1.商品名稱,t1.售價,(select 售價 from #rep where 日期='2021/01/01' and 商品名稱=t1.商品名稱) 昨日售價 into #xx from #Rep t1 where 日期='2021/01/02' alter table #xx alter column 昨日售價 numeric(18,2) null select * ,(售價-昨日售價)/昨日售價 比率 from #xx where (售價-昨日售價)/昨日售價>0.1
本篇文章回覆於2021-01-04 17:31
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
3樓
最有價值解答
pilipala ![]() |
用視窗函數 LEAD 的做法,參考看看
本篇文章回覆於2021-01-05 10:11
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
4樓
作者回應
阿龍 ![]() |
感謝兩位大大的協助,
最後我用LEAD的方法做出來了, 這函數以前沒用過, 上網找了一下,也有類似的作法, 提供給未來有需要的人參考, 謝謝。 https://docs.microsoft.com/zh-tw/sql/t-sql/functions/lead-transact-sql?view=sql-server-ver15
本篇文章回覆於2021-01-07 13:21
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
回覆 |
如要回應,請先登入. |