2010-07-26 9 views
7

Sto creando un sistema di cache della pagina web. Volevo creare un semplice sistema di page rank e output. Il problema è che voglio visualizzare il recordset con il punteggio di rilevanza più alto per dominio univoco. Un dominio può avere più record ma con titoli, descrizioni differenti, ecc. Il problema è che, invece di ottenere 1 recordset contenente un dominio univoco, raggruppa tutti i recordset di quel dominio univoco e li emette tutti. Voglio solo il set di record con il più alto punteggio di rilevanza per dominio unico per gruppo prima che emette il (dominio e diverso con la massima rilevanza per il gruppo) accantoSeleziona il limite massimo 1 dal gruppo

SELECT title, html, sum(relevance) FROM 
    (
    SELECT title, html, 10 AS relevance FROM page WHERE title like ‘%about%’ UNION 
    SELECT title, html, 7 AS relevance FROM page WHERE html like ‘%about%’ UNION 
    SELECT title, html, 5 AS relevance FROM page WHERE keywords like ‘%about%’ UNION 
    SELECT title, html, 2 AS relevance FROM page WHERE description like ‘%about%’ 
) results 
GROUP BY title, html 
ORDER BY relevance desc; 

sto ottenendo:

domain1 title html 
domain1 title html 
domain1 title html 
domain2 title html 
domain2 title html 
domain2 title html 

Quello che voglio è

domain1 title html 
domain2 title html 
domain3 title html 
domain4 title html 
domain5 title html 

risposta

2

io non so perché il codice funziona anche, dal momento che penso che si dovrebbe avere

 
ORDER BY Sum(relevance) DESC 

invece di

 
ORDER BY relevance DESC 

Forse è questo il problema?

Oltre a ciò, che dire di questo. È brutto, ma funzionerà. Sarebbe meglio se SQL Server comprendesse come fare riferimento agli alias più tardi nella query. Ma ahimè.

 
SELECT title, html, 
Case When title LIKE '%about%' Then 10 Else 0 End + 
Case When html LIKE '%about%' Then 7 Else 0 End + 
Case When keywords LIKE '%about%' Then 5 Else 0 End + 
Case When description LIKE '%about%' Then 2 Else 0 End AS relevance 
FROM page 
WHERE Case When title LIKE '%about%' Then 10 Else 0 End + 
Case When html LIKE '%about%' Then 7 Else 0 End + 
Case When keywords LIKE '%about%' Then 5 Else 0 End + 
Case When description LIKE '%about%' Then 2 Else 0 End > 0 
ORDER BY Case When title LIKE '%about%' Then 10 Else 0 End + 
Case When html LIKE '%about%' Then 7 Else 0 End + 
Case When keywords LIKE '%about%' Then 5 Else 0 End + 
Case When description LIKE '%about%' Then 2 Else 0 End DESC; 

O forse solo un leggero riarrangiamento:

 
SELECT title, html, relevance 
FROM (SELECT title, html, 
Case When title LIKE '%about%' Then 10 Else 0 End + 
Case When html LIKE '%about%' Then 7 Else 0 End + 
Case When keywords LIKE '%about%' Then 5 Else 0 End + 
Case When description LIKE '%about%' Then 2 Else 0 End AS relevance 
FROM page) 
WHERE relevance > 0 
ORDER BY relevance DESC; 
+0

Indipendentemente dal modo in cui lo faccio, continuo a ricevere la "pertinenza" della colonna non valida – Patriotec

+1

@kinjite: È perché "pertinenza" è un alias di colonna, a cui fa riferimento la clausola WHERE. SQL Server supporta l'alias di colonne di riferimento in GROUP BY, ma non i ponies di WHOME –

+0

@OMG o ORDER BY, se ricordo correttamente. Ho modificato la risposta per risolvere questo problema. È molto brutto, ma funzionerà. Ho usato questo hack prima. –

0

Ordina per importanza sta causando la vostra richiesta a comportarsi come se rilevanza (non aggregata) è nella clausola SELECT. Erick ha ragione - ORDER BY sum (pertinenza) dovrebbe correggere il tuo errore.