使用滞后从选择结果中选择

时间:2019-05-15 13:13:02

标签: sql oracle lag

这是给出的场景

SELECT * 
FROM   arcade_machine_history_movement 
WHERE  code_machine = 23502 
ORDER  BY date_movement ASC; 


CODE_MACHINE TYPE          date_movement   CURRENT_COMPANY 
23502        T             01/01/2015      SPAIN GAMES
23502        T             01/03/2016      SPAIN GAMES
23502        T             01/01/2016      SPAIN GAMES
23502        S             01/01/2019      FUN GAMES
23502        T             11/01/2019      FUN GAMES
23502        S             11/03/2019      POKER STAR

表arcade_machine_history_movement代表老虎机的所有历史记录移动。 “ S”表示来自另一家公司的销售。

我想获得每台老虎机的销售动向,并获得当前公司(买方)和上一家公司(销售机器的公司)

这就是我所做的

SELECT code_machine, 
   type_m, 
   current_company, 
   Lag (current_company) 
     OVER ( 
       ORDER BY date_movement ASC) AS previous_company 
       FROM   arcade_machine_history_movement 
       WHERE  code_machine = 23502; 

有了滞后,我得到了每行的前一个公司。

这就是结果。

code_machine type_m  date_movement current_company previous_company
23502        T  01/01/2015   SPAIN GAMES     
23502        T  01/03/2016   SPAIN GAMES     SPAIN GAMES
23502        T  01/01/2016   SPAIN GAMES     SPAIN GAMES 
23502        S  01/01/2019   FUN GAMES       SPAIN GAMES    
23502        T  11/01/2019   FUN GAMES       SPAIN GAMES
23502        S    11/03/2019     POKER GAMES     FUN GAMES

因此,现在我想从先前的选择中创建另一个选择,以仅获得“ S”运动,但是我不知道正确地执行它。

code_machine type_m    date_movement   current_company      previous_company
23502        S        11/03/2019      FUN GAMES           SPAIN GAMES
23502        S        11/03/2019      POKER GAMES         FUN GAMES

这是我尝试过的:

SELECT   code_machine, 
     type_m, 
     date_movement, 
     current_company, 
     previous_company 
FROM     (code_machine,type_m,date_movement, current_company, lag 
(current_company) OVER (ORDER BY date_movement ASC) AS empresa_anterior 
FROM arcade_machine_history_movement WHERE code_machine 23502)
WHERE    type_m ='S';

我运行了这个查询,但是它没有完成,并且一直在思考。

我使用Oracle SQL开发人员

我很感谢任何建议。

2 个答案:

答案 0 :(得分:3)

只需使用子查询:

SELECT amhm.*
FROM (SELECT amhm.*, 
             Lag(current_company) OVER (PARTITION BY code_machine ORDER BY date_movement ASC) AS previous_company 
      FROM arcade_machine_history_movement amhm
      WHERE code_machine = 23502
     ) amhm
WHERE type = 'S';

答案 1 :(得分:2)

您可以在第一个请求中通过添加“ AND”和第二个条件来做到这一点:

Sub WordopenV1()
    Dim objWordApp As Object
    Dim objDoc As Object
    Dim FileName As String

    On Error Resume Next
    Set objWordApp = GetObject(, "Word.Application") 
    On Error GoTo 0

    'If Word was not previously open then object variable will be nothing
    'Therefore Create the Object
    If objWordApp Is Nothing Then
        Set objWordApp = CreateObject("Word.Application")
    End If

   FileName = "C:\Users\nicow\Desktop\Praxis Erik Bernhardt\Word Verknüpfung\Rechnung_mitBriefkopf.docx"

    With objWordApp
        .Visible = True
        Set objDoc = .documents.Add ' .documents.Open(FileName)
    End With           
End Sub