SQL查询未找到SUM和COUNT中的所有行

卡利姆斯
select coalesce(ratings.positive,0) as positive,coalesce(ratings.negative,0) as negative,articles.id,x.username,commentnumb, 
    articles.category,
    articles."createdAt",
    articles.id,
    articles.title,
    articles."updatedAt"
    FROM articles
    LEFT JOIN (SELECT id AS userId,username,about FROM users) x ON articles.user_id = x.userId
    LEFT JOIN (SELECT id,
        article_id,
        sum(case when rating = '1' then 1 else 0 end) as positive,
        sum(case when rating = '0' then 1 else 0 end) as negative
        from article_ratings
        GROUP by id
        ) as ratings ON ratings.article_id = articles.id
    LEFT JOIN (SELECT article_id,id,
       count(article_id) as commentNumb
       from article_comments
       GROUP by id
       ) as comments ON comments.article_id = articles.id
    WHERE articles."createdAt" <= :date
    group by ratings.positive,ratings.negative,articles.id,x.username,commentnumb
    order by articles."createdAt" desc
    LIMIT 10

该代码正在运行,但是与SUM和COUNT函数相比,我有更多的注释和更多的评级。

如何解决此查询?

这是使用postgres。

我做了一些实验,似乎第三个加入评论的人是引起问题的人。

文卡塔拉曼R

在派生表中,理想情况下,应该使用article_id进行分组。但是,您正在根据ID进行分组。因此,您获得的不仅仅是派生表中的必要行。我已修改查询以满足您的需求。

    SELECT COALESCE(ratings.positive,0) AS positive,COALESCE(ratings.negative,0) AS negative,articles.id,x.username,commentnumb, 
        articles.category,
        articles."createdAt",
        articles.id,
        articles.title,
        articles."updatedAt"
        FROM articles
        LEFT OUTER JOIN (SELECT id AS userId,username,about FROM users) x ON articles.user_id = x.userId
        LEFT OUTER JOIN (SELECT article_id,
            SUM(case when rating = '1' then 1 else 0 end) as positive,
            SUM(case when rating = '0' then 1 else 0 end) as negative
            FROM article_ratings
            GROUP by article_id 
            ) AS ratings ON ratings.article_id = articles.id
        LEFT OUTER JOIN (SELECT article_id,
           count(article_id) as commentNumb
           FROM article_comments
           GROUP by article_id
           ) AS comments ON comments.article_id = articles.id
        WHERE articles."createdAt" <= :date
        ORDER BY articles."createdAt" desc
        LIMIT 10;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何编写在组中未找到任何记录时返回count = 0的SQL查询

删除所有未找到的对象,即删除集合中未找到的所有键/值

Python PANDAS df.duplicated和df.drop_duplicated未找到所有重复项

“在LSA中未找到TGT的所有可能原因”

SQL搜索未找到所有值

选择所有行和组关系表,即使未找到结果

从JAVA中的SQL查询获取JSON的所有行

HTML :: ELEMENT未找到所有元素

替换未找到所有匹配项

未找到列:1054“ where子句”中的未知列“ id”(SQL:从“ item”中选择count(*)作为汇总,其中“ barcode” = A002和`id` <> 12)

是否可以对组中除返回的类别之外的所有元素使用SQL进行Sum()和/或count()?

BeautifulSoup并未找到所有标签

SUM和COUNT在复杂的SQL查询中不起作用

BeautifulSoup抓取未找到所有'a'标签

查找未找到所有文件

SQL JOIN查询以返回在联接表中未找到匹配项的行

需要T-SQL查询找到所有可能的方法

无法在具有COUNT和3个JOIN(4个表)的SQL查询中获取零值的行

同一查询中COUNT和SUM的SQL难度

sh:所有命令“未找到”

单个查询中的SQL SUM和计数

返回找到和未找到的行

SQL Server:SUM() 和 Row_number() - 行 #1 的所有记录的总和

SQL Server 中的递归查询和所有子项

异步和 LINQ 查询:检索目录树中所有文件中的所有行

使用 Sum() 和 count() 函数的 SQL 查询

用于过滤至少一行中具有特定值的所有行的 SQL 查询

未找到 Hazelcast Predicate/SQL 查询类

有没有办法找到在 SQL Server 中运行查询的代码行?