0

tbl両親

parentId | childId
---------+-----
102      |  1
102      |  3
102      |  4
104      |  3
...

tbl人気

Id | popularityScore
---+-----
1  | 4000
2  | 8000
3  | 3000
4  | 2000
...

親 ID を指定して、最も人気のある子 ID を見つけるクエリがあります。複数のクエリを組み合わせて、parentItemId のセットの中で最も人気のあるアイテムを取得したいと考えてい(102, 104, ...)ます。

望ましい出力

parentId   | mostPopularChildId
           | i.e. the childId with the maximum popularityScore for the given parentId
-----------+-----
102        | 1
104        | 3
...
  • どうすればこれを達成できますか?
  • 個々のクエリを実行するよりも大幅に高速になりますか?
4

2 に答える 2

1

SQLite でこのようなクエリを実行するには、最大スコアを計算してから、テーブルに結合して ID を取得できます。

select pp.ParentId, po.Id, pp.maxscore
from (select pa.ParentID, max(PopularityScore) as maxscore
      from tblParent pa join
           tblPopularity po
           on pa.ChildId = po.Id
     ) pp join
     tblParent pa
     on pa.ParentId = pp.ParentId join
     tblPopularity po
     on pa.ChildId = po.Id and
        po.PopularityScore = pp.maxscore
where . . .
于 2013-05-24T10:37:19.930 に答える