2017-04-05 45 views
1

我的數據與此示例非常相似,因此非常大,因此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個交易充電修復後交易

+0

只需在t處添加限制3他結束您的查詢 –

+0

用您正在使用的數據庫標記您的問題。 –

+0

我正在使用teradata – viji

回答

2

這是一個充電修復日期後返回三行:

SELECT * 
FROM tab 
QUALIFY 
    -- check if any of the three previous rows contains 'CHARGER REPAIR' 
    Max(CASE WHEN LABOR_CODE_DESC = 'CHARGER REPAIR' THEN 1 ELSE 0 END) 
    Over (PARTITION BY CUSTOMER_ID 
     ORDER BY REPAIR_DATE 
     ROWS BETWEEN 3 Preceding AND 1 Preceding) = 1 

編輯:

要在未來10天內僅獲得具有修理日期的行:

-- check if any of the previous rows contains 'CHARGER REPAIR' 
    -- and the current REPAIR_DATE is within 10 days after that repair 
    Max(CASE WHEN LABOR_CODE_DESC = 'CHARGER REPAIR' THEN REPAIR_DATE END) 
    Over (PARTITION BY CUSTOMER_ID 
     ORDER BY REPAIR_DATE 
     ROWS BETWEEN Unbounded Preceding AND 1 Preceding) >= REPAIR_DATE - 10 
+0

如果我只需要在'Charger Repair'的repair_date 10天后才需要交易,我該如何添加。想法使用+ INTERVAL'10'日,但不知道如何以及在哪裏包括條件 – viji

+0

@viji:請參閱我的編輯 – dnoeth

+0

:如果我在CHARGER REPAIR的同一天有多個交易。根據REPAIR_DATE,我們幾乎沒有出現,也沒有出現,因爲我們正在訂購它們。如何更改爲不在CHARGER REPAIR的同一天顯示任何交易以避免此情況(可能會在CHARGER REPAIR日期之後添加一天)? – viji

-1

您可以通過執行獲得所有此類交易:

select t.* 
from table t 
where t.repair_date > (select t2.repair_date 
         from table t2 
         where t2.customer_id = t.customer_id and 
          labor_code_desc like '%CHARGER REPAIR%' 
        ); 

限制這3%的客戶是小tricker,但您可以使用窗口函數:

select t.* 
from (select t.*, 
      row_number() over (partition by customer_id order by repair_date) as seqnum 
     from (select t.*, 
        max(case when labor_code_desc like '%CHARGER REPAIR%' then repair_date end) over (partition by customer_id) as max_crd 
      from table t 
      ) t 
     where t.repair_date > max_crd 
    ) t 
where seqnum <= 3; 
+0

以單一語句執行。失敗[3707:42000]語法錯誤,預期類似於'repair_date'和''之間的'SUCCEEDS'關鍵字或'MEETS'關鍵字或'PRECEDES'關鍵字或'IN'關鍵字或'CONTAINS'關鍵字」。 已用時間= 00:00:00.020 聲明1:未知失敗。 – viji

+0

@viji。 。 。我假設這是爲第二個查詢。我沒有完成'where'條款。 –

+0

現在收到此錯誤 - 作爲單個語句執行。失敗[5479:HY000]在WHERE子句中不允許排序分析函數。 已用時間= 00:00:00.026 聲明1:選擇語句失敗。 – viji