我的數據與此示例非常相似,因此非常大,因此idealy需要高效的代碼。我想在充電器修理後找到這些交易(最多3個)。SQL查詢可在特定部件修復後查找最多3筆交易
TRANSACTION_ID REPAIR_DATE CUSTOMER_ID COMPONENT LABOR_CODE_DESC ....
28289 6/25/2015 AH123 LAPTOP CHARGER REPAIR
28235 6/29/2015 AH123 LAPTOP CHIP REPLACE
258978 6/27/2013 HW687 PHONE TOUCH SCREEN
28223 6/2/2014 AH123 LAPTOP BATTERY REPAIR
215678 9/7/2014 HW687 PHONE SIM REPAIR
527808 7/30/2016 HW687 LAPTOP BATTERY REPAIR
567976 7/28/2014 HW687 LAPTOP CHARGER REPAIR
7678698 8/68/2015 AH123 LAPTOP BATTERY REPAIR
9987908 5/7/2006 TU890 PHONE SIM REPAIR
.....
OUTPUT
TRANSACTION_ID REPAIR_DATE CUSTOMER_ID COMPONENT LABOR_CODE_DESC ....
28235 6/29/2015 AH123 LAPTOP CHIP REPLACE
7678698 8/68/2015 AH123 LAPTOP BATTERY REPAIR
527808 7/30/2016 HW687 LAPTOP BATTERY REPAIR
215678 9/7/2014 HW687 PHONE SIM REPAIR
.....
因此,所有我想要的是那些客戶ID的交易已經過充電修復和充電器維修日期
SELECT TRANSACTION_ID, REPAIR_DATE,CUSTOMER_ID,LABOR_CODE_DESC from table
where customer_id IN (SELECT CUSTOMER_ID from table where LABOR_CODE_DESC
like '%CHARGER REPAIR%')
不知道我該怎麼擴大,以獲得最大的3後最多3個交易充電修復後交易
只需在t處添加限制3他結束您的查詢 –
用您正在使用的數據庫標記您的問題。 –
我正在使用teradata – viji