3

次のコードがあります

select count(*)
from (select Annotations.user_id
from Annotations, Users
where Users.gender =  'Female'
and Users.user_id = Annotations.user_id
and image_id = 1
group by Annotations.user_id
having sum(case when stem = 'taxi' then 1 else 0 end) > 0 and
       sum(case when stem = 'zebra crossing' then 1 else 0 end) > 0
) Annotations

画像 1 の語幹「タクシー」と「横断歩道」を与えた女性の数をカウントします。

サンプルデータ

 user id, image id, stem
 1           1      image
 1           1      taxi
 1           1      zebra crossing
 2           1      person
 2           1      zebra crossing
 2           1      taxi
 3           1      person
 3           1      zebra crossing

期待される結果 (または類似)

stem1,  stem2,            count
taxi ,  zebra crossing      2
person, zebra crossing      2

ただし、2000以上のステムがあるため、すべてを特定することはできません。

ステム文字列を指定するのではなく、image_id = 1 および性別 = female でステム行をループする方法を教えてください。

ありがとうございました

4

3 に答える 3

0

あなたの問題を解決するための私の試みは次のとおりです。

SELECT COUNT(*) AS Count, a1.stem AS Stem1, a2.Stem AS Stem2 
FROM Annotations AS a1
INNER JOIN Annotations AS a2 ON a1.user_id = a2.user_id AND a1.image_id = a2.image_id
                          AND a1.stem < a2.stem 
WHERE a1.image_id = 1
GROUP BY a1.stem, a2.Stem
HAVING COUNT(*) > 1; 

image_idロジックは入れていません。

ここで私の SQL Fiddle を参照してください: http://sqlfiddle.com/#!2/4ee69/33

次のデータ (あなたのものからコピー) に基づいて、その下に投稿された結果を取得します。

CREATE TABLE Annotations
    (`user_id` int, `image_id` int, `stem` varchar(14))
;

INSERT INTO Annotations
    (`user_id`, `image_id`, `stem`)
VALUES
    (1, 1, 'image'),
    (1, 1, 'taxi'),
    (1, 1, 'zebra crossing'),
    (2, 1, 'person'),
    (2, 1, 'zebra crossing'),
    (2, 1, 'taxi'),
    (3, 1, 'person'),
    (3, 1, 'zebra crossing')
;

  COUNT STEM1   STEM2
    2   person  zebra crossing
    2   taxi    zebra crossing
于 2013-04-02T20:08:55.280 に答える