Domanda

Ragazzi, ho una query in cui fondamentalmente selezionare l'ultima versione del browser che il nostro utente utilizzato.

Questa è la nostra (semplificato) struttura della tabella

HITS_TABLE
----------
USERID
BROWSER
HITSDATE

USER_TABLE
----------
USERID
USERNAME

Ed ecco come interrogo l'ultima versione del browser che il nostro utente ha utilizzato

SELECT U.*, H.BROWSER

FROM USER_TABLE U

CROSS APPLY 
  (SELECT TOP 1 BROWSER 
   FROM HITS_TABLE 
   WHERE HITS_TABLE.USERID = U.USERID
   ORDER BY HITS_TABLE.HITSDATE DESC
  )as H

Il HITS_TABLE è appena aggiunto alcuni giorni fa.

Quindi, quella query è solo traduce gli utenti che hanno visitato il nostro sito web dopo abbiamo aggiunto la HITS_TABLE, ed eliminare gli altri.

Qui è il caso del campione

USER_TABLE
-------------------
USERID     USERNAME
-------------------
1          'Spolski'
2          'Atwoord
3          'Dixon'


HITS_TABLE
------------------------------
USERID     HITSDATE     BROWSER
------------------------------
2          15/8/2009    'Firefox 3.5'
1          16/8/2009    'IE 6'
2          16/8/2009    'Chrome'

Ecco il risultato del campione

------------------------------
USERID     USERNAME     BROWSER
------------------------------
1          'Spolsky'    'IE 6'
2          'Atwoord'    'Chrome'

Ma, voglio aggiungere altri utenti con il browser 'sconosciuto'. Ecco il mio risultato desiderato

------------------------------
USERID     USERNAME     BROWSER
------------------------------
1          'Spolsky'    'IE 6'
2          'Atwoord'    'Chrome'
3          'Dixon'      'Unknown'

Credo che potrebbe essere raggiunto attraverso LEFT OUTER JOIN. Ma ho sempre avuto questa: (I non voglio che questo risultato)

------------------------------
USERID     USERNAME     BROWSER
------------------------------
1          'Spolsky'    'IE 6'
2          'Atwoord'    'Chrome'
2          'Atwoord'    'Firefox 3.5'
3          'Dixon'      'Unknown'

Spero che la mia domanda è chiara.

È stato utile?

Soluzione

con un gruppo da l'userid contro il hits_table consente di ottenere il massimo hitsdate () per ogni userid. Ho chiamato questo ultimi successi nel codice qui sotto.

La selezione sulla tabella utente con un sinistro join ultimi successi ti permette di estrarre record per ogni utente.

unendo di nuovo sul HITS TABELLA poi allwos di tirare il record del browser associato a tale data, o un null per gli utenti con la fedina in là.

select
   user_table.userid,
   user_table.username,
   isnull(hitstable.browser, 'unknown') as browser
from
  user_table
left join
(
  select
    userid,
    max(hitsdate) hitsdate
  from
    hits_table
  group by  
    userid
) latest_hits
on
  user_table.userid = latest_hits.userid    
left join
  hits_table
on hits.table.userid = latest_hits.userid
and hits_table.hitsdate = latest_hits.hitsdate

Altri suggerimenti

Non potresti sub select, non bella, ma dovrebbe funzionare ..

SELECT U.*,

ISNULL((SELECT TOP 1 BROWSER 
   FROM HITS_TABLE 
   WHERE HITS_TABLE.USERID = U.USERID
   ORDER BY HITS_TABLE.HITSDATE DESC),'UnKnown') AS Browser

FROM USER_TABLE U
SELECT U.*,'BROWSER' = 
    case 
     when (SELECT TOP 1 BROWSER FROM HITS_TABLE WHERE HITS_TABLE.USERID = U.USERID ORDER BY HITS_TABLE.HITSDATE DESC) is  null then 'Unknown'
else (SELECT TOP 1 BROWSER FROM HITS_TABLE WHERE HITS_TABLE.USERID = U.USERID ORDER BY HITS_TABLE.HITSDATE DESC)
    end
FROM USER_TABLE U
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top