2

附图显示了结果集,我将在下面留下我的代码 - 为什么我无法计算viewTotal每个单元格中整列的总和?我希望viewTotal列下的每个单元格都可以读取4

我想这是某种分组问题,尽管我在网上找不到关于哪些列需要分组的具体信息。保留所有三行很重要 - 我不想只返回一行。也许这个标准使我想做的事情变得更加困难?

谢谢你,埃文

结果预览

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 名校警 4 4
30 这是一个主题的正常标题... 4 0

4

2 回答 2

1
SELECT  x.*, 
        COALESCE(y.viewTotal, 0) viewTotal,
        COALESCE(z.replyCount, 0) replyCount
FROM    topic x
        LEFT JOIN
        (
            SELECT  a.topic_by, COUNT(b.topic_ID) viewTotal
            FROM    topic a
                    LEFT JOIN topic_view_tracker b
                        ON a.topic_ID = b.topic_ID
            GROUP   BY a.topic_by
        ) y ON x.topic_by = y.topic_by
        LEFT JOIN
        (
            SELECT  reply_topic, COUNT(*) replyCount
            FROM    reply
            GROUP   BY reply_topic
        ) z ON  x.topic_ID = z.reply_topic
WHERE   x.topic_by = 10

输出(基于提供的记录

╔══════════╦════════════════════════════╦══════════╦═══════════╦════════════╗
║ TOPIC_ID ║       TOPIC_SUBJECT        ║ TOPIC_BY ║ VIEWTOTAL ║ REPLYCOUNT ║
╠══════════╬════════════════════════════╬══════════╬═══════════╬════════════╣
║       25 ║ School police in the night ║       10 ║         4 ║          1 ║
║       29 ║ The first topic, enjoy it  ║       10 ║         4 ║          1 ║
║       30 ║ This is a normal title...  ║       10 ║         4 ║          0 ║
╚══════════╩════════════════════════════╩══════════╩═══════════╩════════════╝
于 2013-03-14T01:58:42.277 回答
0

如果您需要在每一行上重复总视图,您需要使用这个:

Select topic_id, topic_subject, (SELECT Count(distinct id) FROM topic_view_tracker WHERE topic_id = B.topic_id)) 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

但老实说,我不明白这是为什么。您可以轻松地SELECT Count(distinct id) FROM topic_view_tracker WHERE topic_id = 10单独执行,然后执行其余的查询,topic_view_tracker完全省略

于 2013-03-14T01:43:53.767 回答