让SQL计算整个列的总和 - 我相信分组有问题

时间:2013-03-14 01:16:31

标签: mysql sql join sum

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

我认为这是某种分组问题,虽然我无法在网上找到任何特定于哪些列需要分组的内容。 重要的是所有三行都保留 - 我不想只返回一行。也许这个标准使我想要做的更难?

谢谢你, 埃文

Result Preview

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

╔══════════╦════════════════════════════╦══════════╗
║ 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       topic subject                          view total   reply count
29         The first topic, enjoy it                    4            5
25          school police in the night                  4            4
30          this is a normal title for a topic ...      4            0

2 个答案:

答案 0 :(得分: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

OUTPUT(基于提供的记录

╔══════════╦════════════════════════════╦══════════╦═══════════╦════════════╗
║ 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 ║
╚══════════╩════════════════════════════╩══════════╩═══════════╩════════════╝

答案 1 :(得分: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