postgresql group by

时间:2018-02-22 03:54:54

标签: mysql sql postgresql group-by

这是我在Postgresql中的查询:

{
    "result": [
    {
        "id":1,
        "original_title":"title 1",
        "release_date":"date",
        "production_countries":"contries"

    },
    {
        "id":2,
        "original_title":"title 2",
        "release_date":"date",
        "production_countries":"contries"

    },
    {
        "id":3,
        "original_title":"title 3",
        "release_date":"date",
        "production_countries":"contries"

    },
    .
    .
    .

    ]

}

我得到了这个结果:

enter image description here

现在我正在尝试按名称(最后一列)对此结果进行分组,以获得如下结果:

SELECT 
    C.id, S.domen, B.name, C.source_id, ST.name 
FROM "calls" C 
    INNER JOIN "site" S ON S.id=C.site_id 
    INNER JOIN "sources" ON sources.id=C.source_id 
    INNER JOIN "brand" B ON B.id = S.id_brand
    INNER JOIN "source_types" ST ON ST.id = "sources".type_id 
WHERE 
    ("calltime" >= '2017-12-01') AND 
    ("calltime" <= '2017-12-03') AND 
    (S."id_brand"='6') 
ORDER BY "calltime" LIMIT 50

为此,我使用此查询:

Контекстная реклама - 17
SEO-10
.... 

但我得错了结果:

enter image description here

好像从SELECT ST.name, count(ST.name) FROM "calls" C INNER JOIN "site" S ON S.id=C.site_id INNER JOIN "sources" ON sources.id=C.source_id INNER JOIN "brand" B ON B.id = S.id_brand INNER JOIN "source_types" ST ON ST.id = "sources".type_id WHERE ("calltime" >= '2017-12-01') AND ("calltime" <= '2017-12-03') AND (S."id_brand"='6') GROUP BY ST.name ORDER BY count(ST.name) DESC LIMIT 50 列中获取值。 我做错了什么?

3 个答案:

答案 0 :(得分:1)

试试这个,如果您在限制结果之前调用group by,则为整个记录分组。因此,首先过滤然后执行分组。

    SELECT 
        name, count(name) 
    FROM(
    SELECT 
        ST.name 
    FROM "calls" C 
        INNER JOIN "site" S ON S.id=C.site_id 
        INNER JOIN "sources" ON sources.id=C.source_id 
        INNER JOIN "brand" B ON B.id = S.id_brand
        INNER JOIN "source_types" ST ON ST.id = "sources".type_id 
    WHERE 
        ("calltime" >= '2017-12-01') AND 
        ("calltime" <= '2017-12-03') AND 
        (S."id_brand"='6') 
    ORDER BY "calltime" LIMIT 50
    ) T 
    GROUP BY name 

答案 1 :(得分:0)

49可能只是巧合。尝试:

SELECT 
    ST.name, count(*) 
FROM "calls" C 
    INNER JOIN "site" S ON S.id=C.site_id 
    INNER JOIN "sources" ON sources.id=C.source_id 
    INNER JOIN "brand" B ON B.id = S.id_brand
    INNER JOIN "source_types" ST ON ST.id = "sources".type_id 
WHERE 
    ("calltime" >= '2017-12-01') AND 
    ("calltime" <= '2017-12-03') AND 
    (S."id_brand"='6') AND 
    C.source_id > 50
GROUP BY ST.name 
ORDER BY count(*) DESC LIMIT 50

我所做的就是将其更改为count(*)并将AND C.source_id > 50添加到Where子句中。让我们看看这是否会改变计数。

你也可以这样运行:

SELECT 
    count(*) 
FROM "calls" C 
    INNER JOIN "site" S ON S.id=C.site_id 
    INNER JOIN "sources" ON sources.id=C.source_id 
    INNER JOIN "brand" B ON B.id = S.id_brand
    INNER JOIN "source_types" ST ON ST.id = "sources".type_id 
WHERE 
    ("calltime" >= '2017-12-01') AND 
    ("calltime" <= '2017-12-03') AND 
    (S."id_brand"='6') 

这为您提供了所有行的总计数(我删除了GROUP BY)。如果此数字等于您的分组行数的总和,则它们实际上是计数。我们正在寻找127

49 + 30 + 21 + 13 + 9 + 4 + 1

我希望这会有所帮助。

答案 2 :(得分:0)

第二个查询中的LIMIT 50GROUP BY后执行

如果您只想汇总前50行,请将LIMIT 50写入子选择,然后在外GROUP BY内执行SELECT