viewTotal
添付の画像は結果セットを示しています。以下にコードを残します-各セル内の列全体の合計を計算できないのはなぜですか?viewTotal
列の下の各セルに読み取りを依頼します4
。
どの列をグループ化する必要があるかに関して、Web上で具体的なことは何も見つかりませんが、これはある種のグループ化の問題だと思います。3行すべてを残すことが重要です。1行だけを返したくありません。おそらく、この基準は私がやろうとしていることをより難しくしますか?
ありがとう、エヴァン
Select topic_id, topic_subject, SUM(topicViews) as viewTotal, replyCount From
(
Select
T.topic_id, T.topic_subject, Count(distinct Tvt.id) as topicViews, Count(Distinct R.reply_id) as replyCount, R.reply_id, R.reply_topic
From topic T
LEFT JOIN topic_view_tracker Tvt ON
T.topic_id = Tvt.topic_id
LEFT Join reply R ON
T.topic_id = R.reply_topic
Where
T.topic_by = 10
Group By T.topic_id) B
Group By topic_id
Order by replyCount DESC
サンプルレコード:
トピックテーブル
╔══════════╦════════════════════════════╦══════════╗
║ TOPIC_ID ║ TOPIC_SUBJECT ║ TOPIC_BY ║
╠══════════╬════════════════════════════╬══════════╣
║ 25 ║ School police in the night ║ 10 ║
║ 29 ║ The first topic, enjoy it ║ 10 ║
║ 30 ║ This is a normal title... ║ 10 ║
╚══════════╩════════════════════════════╩══════════╝
TOPIC_VIEW_TRACKERテーブル
╔════╦════════════╦══════════╗
║ ID ║ USER_IP ║ TOPIC_ID ║
╠════╬════════════╬══════════╣
║ 1 ║ xx.xx.xx.x ║ 25 ║
║ 2 ║ xx.xx.xx.x ║ 25 ║
║ 3 ║ xx.xxx.xxx ║ 29 ║
║ 4 ║ xxx.xx.xx ║ 30 ║
╚════╩════════════╩══════════╝
返信テーブル
╔══════════╦═════════════╗
║ REPLY_ID ║ REPLY_TOPIC ║
╠══════════╬═════════════╣
║ 1 ║ 25 ║
║ 2 ║ 29 ║
╚══════════╩═════════════╝
期待される出力(例):
topic_idトピックサブジェクトビュー合計返信数 29最初のトピック、楽しんでください4 5 夜の25校の警察44 30これはトピックの通常のタイトルです...40