2012-09-08 2 views
6

Ho due querys qui sotto, entrambi alimentati dalla stessa tabella "player". Voglio dividere la query 1 con la query 2 per ottenere una percentuale pertinente. Sono relativamente nuovo alle query SQL più dettagliate, così come la pubblicazione sui forum ... ma per favore fatemi sapere se avete qualche suggerimento su come combinare questo per ottenere il risultato percentuale pertinente.SQL - Dividendo due risultati

Select 
    sysdate,sum(Count(init_dtime)) 
From Player p 
Where 
    Trunc(Init_Dtime) > Trunc(Sysdate) - 7 
    And Trunc(Create_Dtime) >= To_Date('2012-mar-01','yyyy-mon-dd') 
    and trunc(create_dtime) < to_date('2015-sep-9','yyyy-mon-dd') 
Group By Trunc(Init_Dtime) 
Order By Trunc(Init_Dtime) Asc 
Select 
    Sum(Count(Create_Dtime)) 
From Player P 
where 
    Trunc(Create_Dtime) >= To_Date('2012-mar-01','yyyy-mon-dd') 
    And Trunc(Create_Dtime) < To_Date('2015-sep-9','yyyy-mon-dd') 
Group By Trunc(create_Dtime) 
Order By Trunc(create_Dtime) Asc 
+1

si dispone già di due risposte, il che mi sorprende, perché io don capisco la domanda Vuoi un numero o un numero per giocatore? Se le risposte non sono quelle che desideri, prova a postare un set di dati originale dalla tabella del giocatore, un set di risultati previsto e una spiegazione della trasformazione. –

+0

@Rob van Wijk - scusa per la mancanza di chiarezza. Sto cercando di ottenere un numero totale per tutti i giocatori messi insieme. ci sono molte colonne nella tabella "player", ma quelle che sono importanti qui sono: 1) "create_dtime" che è la data in cui un giocatore specifico ha creato un account e 2) init d_time che è la data più recente in cui uno specifico il giocatore ha effettuato l'accesso. Per tutti i giocatori entro un determinato intervallo di date, voglio dividere la query 1 (tutti i giocatori attivi negli ultimi 7 giorni, per gli utenti che hanno creato un account dopo il 1 ° gennaio 2012) per la query 2 (utenti totali che hanno creato un account – Americo

risposta

4

Si può solo dire che

select sysdate, 
     count((init_dtime))/sum((Create_Dtime)) * 100 as percentage 
    from Player p 
where Trunc(Init_Dtime) > Trunc(Sysdate) - 7 
    and Trunc(Create_Dtime) >= To_Date('2012-mar-01','yyyy-mon-dd') 
    and trunc(create_dtime) < to_date('2015-sep-9','yyyy-mon-dd') 
    order by percentage asc 

Il group by nei SQLs non sono necessari in quanto non sono realmente raggruppamento da qualcosa. group by è utile quando è necessaria la percentuale per giocatore, ad esempio. Allora si potrebbe dire group by player_id e nel select avrebbe la player_id: EDIT

select player_id, count(…) 
    from … 
where … 
group by player_id 

: Se le clausole dove sono diversi:

select sysdate, 
     (
      (select count((init_dtime)) 
      from player p 
      where trunc(Init_Dtime) > trunc(Sysdate) - 7 
       and Trunc(Create_Dtime) >= To_Date('2012-mar-01','yyyy-mon-dd') 
       and trunc(create_dtime) < to_date('2015-sep-9','yyyy-mon-dd')) 
      /
      (select count((Create_Dtime)) 
       from player P 
      where trunc(Create_Dtime) >= To_Date('2012-mar-01','yyyy-mon-dd') 
       and trunc(Create_Dtime) < To_Date('2015-sep-9','yyyy-mon-dd')) 
     ) * 100 as percentage 
from dual 
+0

Hanno diverse clausole 'WHERE' anche se –

+0

Mike - esattamente ... anche se tutto viene alimentato dalla tabella dei giocatori, i due querys sono strutturati su diversi punti in cui i criteri possono manualmente solo prendere le 2 query e li divido da solo, ma dov'è il divertimento ?! Ho pensato di iniziare a utilizzare questo forum per provare ad espandere la mia automazione. – Americo

+0

@MichaelBerkowski Non l'ho notato. Aggiornerà la risposta. – Nivas