2017-03-30 3 views
1

Bild dieser Tabelle von Interaktionen ein Geschäft hat mit Menschen:Kann ich eine variable Bedingung in einer CASE-Anweisung verwenden?

+-----------+---------------------+-----------------+ 
| user_name | action_timestamp | action   | 
+-----------+---------------------+-----------------+ 
| john  | 2017-01-01 10:00:00 | phone_call  | 
+-----------+---------------------+-----------------+ 
| john  | 2017-01-02 12:00:00 | became_customer | 
+-----------+---------------------+-----------------+ 
| john  | 2017-01-03 14:00:00 | phone_call  | 
+-----------+---------------------+-----------------+ 
| jane  | 2016-08-06 10:00:00 | phone_call  | 
+-----------+---------------------+-----------------+ 
| jane  | 2016-08-06 11:00:00 | phone_call  | 
+-----------+---------------------+-----------------+ 
| jane  | 2016-08-06 12:00:00 | became_customer | 
+-----------+---------------------+-----------------+ 
| tony  | 2016-12-01 15:00:00 | phone_call  | 
+-----------+---------------------+-----------------+ 

ich so etwas bekommen wollen:

+-----------+---------------+-------------+---------------------+------------------------------+-----------------------------+ 
| user_name | total_actions | is_customer | became_customer  | interactions_before_customer | interactions_after_customer | 
+-----------+---------------+-------------+---------------------+------------------------------+-----------------------------+ 
| john  | 3    | TRUE  | 2017-01-02 12:00:00 | 1       | 1       | 
+-----------+---------------+-------------+---------------------+------------------------------+-----------------------------+ 
| jane  | 3    | TRUE  | 2016-08-06 12:00:00 | 2       | 0       | 
+-----------+---------------+-------------+---------------------+------------------------------+-----------------------------+ 
| tony  | 1    | FALSE  | NULL    | 1       | 0       | 
+-----------+---------------+-------------+---------------------+------------------------------+-----------------------------+ 

Die ersten 4 Spalten sind mit einer gewissen Gruppierung und CASEs trivial, aber ich Ich weiß nicht, wie man Spalten 5 und 6 (Interaktionen vor Kunden und Interaktionen nach Kunden) ausführt, da der Fall auf dem Ergebnis einer vorherigen Spalte basiert und zwischen den Zeilen variieren muss.

Ist das einfacher als es scheint? Wenn jemand interessiert, ich arbeite nicht in einem Call-Center, es ist nur ein viel einfacheres Analog zu dem, was ich versuche zu tun;)

Antwort

2

Dies wird Ihnen eine Idee geben. Ich habe dies auf ORACLE getestet und es funktioniert gut,

WITH CUS_DET AS (
    SELECT * 
    FROM INTERACTIONS 
    WHERE ACTION = 'became_customer' 
) 
SELECT INTERACTIONS.USER_NAME 
    , SUM(CASE 
    WHEN (CUS_DET.ACTION_TIMESTAMP IS NULL 
      OR INTERACTIONS.ACTION_TIMESTAMP < CUS_DET.ACTION_TIMESTAMP) 
    THEN 1 
    ELSE 0 
    END) BEF 
    , SUM(CASE 
    WHEN INTERACTIONS.ACTION_TIMESTAMP > CUS_DET.ACTION_TIMESTAMP 
    THEN 1 
    ELSE 0 
    END) AFT 
FROM INTERACTIONS 
    LEFT OUTER JOIN CUS_DET 
    ON INTERACTIONS.USER_NAME = CUS_DET.USER_NAME 
GROUP BY INTERACTIONS.USER_NAME; 
+0

Ehrfürchtig . Vielen Dank! –

2

Bedingte Aggregation, wenn ein Benutzer nur einmal ein Kunde werden kann:

select 
    t.user_name 
    , case when c.action_timestamp is not null then 'Yes' else 'No' end as Is_Customer 
    , c.action_timestamp as became_customer 
    , count(case when t.action_timestamp < coalesce(c.action_timestamp,'2525-01-01') then 1 end) as interactions_before_customer 
    , count(case when t.action_timestamp > c.action_timestamp then 1 end) as interactions_after_customer 
from t 
    left join t as c 
    on t.user_name = c.user_name 
     and c.action = 'became_customer' 
group by t.user_name, c.action_timestamp 

rextester Demo: http://rextester.com/LOUGN53032

+-----------+-------------+---------------------+------------------------------+-----------------------------+ 
| user_name | Is_Customer | became_customer | interactions_before_customer | interactions_after_customer | 
+-----------+-------------+---------------------+------------------------------+-----------------------------+ 
| tony  | No   | NULL    |       1 |       0 | 
| jane  | Yes   | 2016-08-06 12:00:00 |       2 |       0 | 
| john  | Yes   | 2017-01-02 12:00:00 |       1 |       1 | 
+-----------+-------------+---------------------+------------------------------+-----------------------------+ 
+0

Argh tut mir leid, es ist so schwer zwischen diesen Antworten zu wählen! Ich wollte diesen hier wählen, nur weil du ein SQL Prominenter bist und ich etwas über Koaleszenz gelernt habe, aber Pons funktioniert auch und er ist ein paar Minuten früher gekommen. Danke für Ihre Antwort :) –

Verwandte Themen