2015-05-03 14 views
5

W MySQL Mam tabeli tak:Dla zapytania SQL liczyć z którym błąd klauzula

create table review(
reviewId varchar(12) primary key, 
helpfulness double, 
reviewRating integer) 

staram się liczyć pomocny i nieprzydatny group byreviewRating i helpfulnesswhere> = 0,75 jako unhelpfulness lub gdzie < 0,75 jako przydatność. Jak mogę uzyskać taki wynik?

unhelpfulness helpfulness reviewRating 
5    2   1 
4    2   2 
3    4   3 

I starają się robić tak, ale wydaje się, że licznik nie działa i dołącz jest nieważny w tej pozycji.

SELECT a.count AS HELPFUL, b.count AS UNHELPFUL 
FROM review a where helpfulness>=0.75 group by a.reviewRating 
OUTER JOIN review b where helpfulness<0.75 group by b.reviewRating 
on a.reviewRating = b.reviewRating 
+0

Pytanie: czy nie zostanie policzona żadna wartość przydatności większa niż 0,75? Innymi słowy, dowolna liczba całkowita od 1 do 5? – grill

Odpowiedz

3

W MySQL można zrobić za pomocą sum z warunków lub wyrażenia spowoduje to jako Boolean (0/1) i ten sposób można uzyskać warunkową liczyć

SELECT a.reviewRating, 
SUM(helpfulness>=0.75) AS HELPFUL, 
SUM(helpfulness < 0.75)AS UNHELPFUL 
FROM review a 
GROUP BY a.reviewRating 
0

Ty możliwe, implementacja agregacji warunkowej z wyrażeniem case:

select a.reviewRating 
    , count(case when helpfulness >= 0.75 then helpfulness end) as helpful 
    , count(case when helpfulness < 0.75 then helpfulness end) as unhelpful 
from review a 
group by a.reviewRating 
Powiązane problemy