Ich denke, das ist, was Sie nach:
with sample_data as (select to_date('09/05/2016 23:52:34', 'dd/mm/yyyy hh24:mi:ss') regdate, to_date('09/05/2016 23:55:06', 'dd/mm/yyyy hh24:mi:ss') signupdate, 'Mobile' channel, 1 id, 'apple' product from dual union all
select to_date('09/05/2016 23:52:32', 'dd/mm/yyyy hh24:mi:ss') regdate, to_date('09/05/2016 23:52:45', 'dd/mm/yyyy hh24:mi:ss') signupdate, 'Mobile' channel, 2 id, 'mango' product from dual union all
select to_date('09/05/2016 23:52:16', 'dd/mm/yyyy hh24:mi:ss') regdate, null signupdate, 'Mobile' channel, 3 id, 'mango' product from dual union all
select to_date('09/05/2016 23:52:49', 'dd/mm/yyyy hh24:mi:ss') regdate, null signupdate, 'Mobile' channel, 4 id, 'banana' product from dual union all
select to_date('09/05/2016 23:52:50', 'dd/mm/yyyy hh24:mi:ss') regdate, null signupdate, 'Mobile' channel, 5 id, 'banana' product from dual union all
select to_date('09/05/2016 23:52:50', 'dd/mm/yyyy hh24:mi:ss') regdate, to_date('09/05/2016 15:52:45', 'dd/mm/yyyy hh24:mi:ss') signupdate, 'Mobile' channel, 6 id, 'kiwi' product from dual)
select to_char(regdate, 'dd/mm/yyyy') regdate,
to_char(regdate, 'hh24:mi') min,
count(*) num_of_regs,
count(case when signupdate >= regdate then 1 end) num_signups,
channel,
product
from sample_data
group by to_char(regdate, 'dd/mm/yyyy'),
to_char(regdate, 'hh24:mi'),
channel,
product
order by product;
REGDATE MIN NUM_OF_REGS NUM_SIGNUPS CHANNEL PRODUCT
---------- ----- ----------- ----------- ------- -------
09/05/2016 23:52 1 1 Mobile apple
09/05/2016 23:52 2 0 Mobile banana
09/05/2016 23:52 1 0 Mobile kiwi
09/05/2016 23:52 2 1 Mobile mango
Bitte beachte, dass ich angenommen habe, dass die id
Spalte eindeutig ist und dass die Tatsache, Ihre letzte Reihe hatte id = 5 war ein Tippfehler.
Ich habe auch angenommen, dass der Grund, warum die Ausgabe für das Kiwi-Produkt keine Anmeldeanzahl hatte, ist, weil das Anmeldedatum vor dem Registrierungsdatum lag. Du hast die Logik dahinter nicht erklärt, also weiß ich nicht, ob das ein Tippfehler in deinem erwarteten Output war oder nicht. Sie sollten in der Lage sein, meine Abfrage so zu ändern, dass sie die Ergebnisse ausgibt, die Sie benötigen (zum Beispiel case when signupdate is not null then 1 end
vielleicht?).
Wenn Sie auch Leerzeichen anstelle von 0 anzeigen möchten, ist das eine einfache CASE
/DECODE
, um die Ausgabe zu ändern, um null statt 0 zu setzen; deine Entscheidung.
Als eine komplette beiseite, tun Sie: SUBSTR(TO_CHAR(regdate, 'HH24:MI:SS'),1,5)
... warum? Sie erstellen eine Zeichenfolge vom Datum und verkürzen sie dann. Warum würden Sie nicht einfach die Zeichenfolge aus dem Datum in dem von Ihnen gewünschten Format erstellen? Z.B. to_char(regdate, 'hh24:mi')
Welche rdbms verwenden Sie? –
Orakel verwenden, aber das Problem hier ich bin nicht sicher, wie Sie sowohl regdate & signupdate Anzahl (eindeutige ID), SUBSTR (TO_CHAR (regdate, 'HH24: MI: SS'), 1,5), Quelle, reg_product aus glance_REG_act_Prod_sampl Gruppe von SUBSTR (TO_CHAR (Regdate, 'HH24: MI: SS'), 1,5), Quelle, reg_product – nkalis
Ihre Ausgabe hat Regdate als erste Spalte, aber Ihre Abfrage hat eine Anzahl zuerst. – jarlh