Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Postgres:字符串聚合和连接_Sql_Postgresql - Fatal编程技术网

Sql Postgres:字符串聚合和连接

Sql Postgres:字符串聚合和连接,sql,postgresql,Sql,Postgresql,如果我有这样一个问题: SELECT u.client_code, max(d.created_at) as last_document_created_at, u.brand_id, t.name as template_name, count(d) FROM users u INNER JOIN documents d ON u.id = d.user_id INNER JOIN templates t ON t.id = d

如果我有这样一个问题:

SELECT 
    u.client_code, 
    max(d.created_at) as last_document_created_at, 
    u.brand_id, 
    t.name as template_name, 
    count(d)
FROM users u
    INNER JOIN documents d ON u.id = d.user_id
    INNER JOIN templates t ON t.id = d.template_id
GROUP BY 1, 3, 4
client_code last_document_created_at    brand_id template_name  count
---------------------------------------------------------------------
client1     2017-12-06 10:03:47 +1100   39       newsletter_r   1
client1     2017-12-05 15:23:24 +1100   39       Other media    5
client2     2017-12-21 17:07:11 +1100   39       newsletter_r   4
client3     2018-01-11 12:10:43 +1100   39       newsletter_r   2
client3     2017-12-06 11:45:21 +1100   39       Other media    1
SELECT u.client_code, 
       max(d.created_at) as last_document_created_at, 
       u.brand_id, 
       string_agg(t.name, ',') as template_name, 
       count(distinct d.id)
FROM users u INNER JOIN
     documents d
     ON u.id = d.user_id INNER JOIN
     templates t
     ON t.id = d.template_id
GROUP BY 1, 3;
SELECT client_code, STRING_AGG(template_name || template_count, ',') 
FROM (
    SELECT 
        u.client_code, 
        MAX(d.created_at) AS last_document_created_at, 
        u.brand_id, 
        t.name AS template_name, 
        COUNT(d) AS template_count
    FROM users u
    INNER JOIN documents d ON u.id = d.user_id
    INNER JOIN templates t ON t.id = d.template_id
    GROUP BY 1, 3, 4
) src
GROUP BY client_code
返回如下信息:

SELECT 
    u.client_code, 
    max(d.created_at) as last_document_created_at, 
    u.brand_id, 
    t.name as template_name, 
    count(d)
FROM users u
    INNER JOIN documents d ON u.id = d.user_id
    INNER JOIN templates t ON t.id = d.template_id
GROUP BY 1, 3, 4
client_code last_document_created_at    brand_id template_name  count
---------------------------------------------------------------------
client1     2017-12-06 10:03:47 +1100   39       newsletter_r   1
client1     2017-12-05 15:23:24 +1100   39       Other media    5
client2     2017-12-21 17:07:11 +1100   39       newsletter_r   4
client3     2018-01-11 12:10:43 +1100   39       newsletter_r   2
client3     2017-12-06 11:45:21 +1100   39       Other media    1
SELECT u.client_code, 
       max(d.created_at) as last_document_created_at, 
       u.brand_id, 
       string_agg(t.name, ',') as template_name, 
       count(distinct d.id)
FROM users u INNER JOIN
     documents d
     ON u.id = d.user_id INNER JOIN
     templates t
     ON t.id = d.template_id
GROUP BY 1, 3;
SELECT client_code, STRING_AGG(template_name || template_count, ',') 
FROM (
    SELECT 
        u.client_code, 
        MAX(d.created_at) AS last_document_created_at, 
        u.brand_id, 
        t.name AS template_name, 
        COUNT(d) AS template_count
    FROM users u
    INNER JOIN documents d ON u.id = d.user_id
    INNER JOIN templates t ON t.id = d.template_id
    GROUP BY 1, 3, 4
) src
GROUP BY client_code
我有哪些选项可以连接
template\u name
count
字段,以便每个用户(在
u.client\u code
中表示)都在一行上?我知道我可以像这样调用
string\u agg
列:

...
string_agg(distinct t.name, ', ') as template_name, 
...
但这当然破坏了各自的计算:

newsletter_r, Other media   6
更新

我可以这样做:

string_agg(concat_ws(': ', t.name::text, count(d)::text), ', ') as template_count
但这给了我一个错误:

aggregate function calls cannot be nested LINE 5: string_agg(concat_ws(': ', t.name::text, count(d)::text)... ^ : SELECT u.client_code,

我想你想要这样的东西:

SELECT 
    u.client_code, 
    max(d.created_at) as last_document_created_at, 
    u.brand_id, 
    t.name as template_name, 
    count(d)
FROM users u
    INNER JOIN documents d ON u.id = d.user_id
    INNER JOIN templates t ON t.id = d.template_id
GROUP BY 1, 3, 4
client_code last_document_created_at    brand_id template_name  count
---------------------------------------------------------------------
client1     2017-12-06 10:03:47 +1100   39       newsletter_r   1
client1     2017-12-05 15:23:24 +1100   39       Other media    5
client2     2017-12-21 17:07:11 +1100   39       newsletter_r   4
client3     2018-01-11 12:10:43 +1100   39       newsletter_r   2
client3     2017-12-06 11:45:21 +1100   39       Other media    1
SELECT u.client_code, 
       max(d.created_at) as last_document_created_at, 
       u.brand_id, 
       string_agg(t.name, ',') as template_name, 
       count(distinct d.id)
FROM users u INNER JOIN
     documents d
     ON u.id = d.user_id INNER JOIN
     templates t
     ON t.id = d.template_id
GROUP BY 1, 3;
SELECT client_code, STRING_AGG(template_name || template_count, ',') 
FROM (
    SELECT 
        u.client_code, 
        MAX(d.created_at) AS last_document_created_at, 
        u.brand_id, 
        t.name AS template_name, 
        COUNT(d) AS template_count
    FROM users u
    INNER JOIN documents d ON u.id = d.user_id
    INNER JOIN templates t ON t.id = d.template_id
    GROUP BY 1, 3, 4
) src
GROUP BY client_code

不确定要如何设置连接字段的格式,但是否尝试将原始查询放入子查询并对其应用
string\u agg
?大概是这样的:

SELECT 
    u.client_code, 
    max(d.created_at) as last_document_created_at, 
    u.brand_id, 
    t.name as template_name, 
    count(d)
FROM users u
    INNER JOIN documents d ON u.id = d.user_id
    INNER JOIN templates t ON t.id = d.template_id
GROUP BY 1, 3, 4
client_code last_document_created_at    brand_id template_name  count
---------------------------------------------------------------------
client1     2017-12-06 10:03:47 +1100   39       newsletter_r   1
client1     2017-12-05 15:23:24 +1100   39       Other media    5
client2     2017-12-21 17:07:11 +1100   39       newsletter_r   4
client3     2018-01-11 12:10:43 +1100   39       newsletter_r   2
client3     2017-12-06 11:45:21 +1100   39       Other media    1
SELECT u.client_code, 
       max(d.created_at) as last_document_created_at, 
       u.brand_id, 
       string_agg(t.name, ',') as template_name, 
       count(distinct d.id)
FROM users u INNER JOIN
     documents d
     ON u.id = d.user_id INNER JOIN
     templates t
     ON t.id = d.template_id
GROUP BY 1, 3;
SELECT client_code, STRING_AGG(template_name || template_count, ',') 
FROM (
    SELECT 
        u.client_code, 
        MAX(d.created_at) AS last_document_created_at, 
        u.brand_id, 
        t.name AS template_name, 
        COUNT(d) AS template_count
    FROM users u
    INNER JOIN documents d ON u.id = d.user_id
    INNER JOIN templates t ON t.id = d.template_id
    GROUP BY 1, 3, 4
) src
GROUP BY client_code

我还没有测试它,所以您可能有一些语法错误。让我知道这是否有效。

您的
字符串\u agg()
中有一个错误(需要两个参数)。而且,当这个问题得到解决时,它不会产生任何与我的查询不同的结果。效果非常好!但是,我不知道如何通过
last\u document\u created\u at
列,因为我被迫将
u.client\u code
拆分为多行的列进行分组:
column“src.last\u document\u created\u at”必须出现在group by子句中或用于聚合函数中,如何确定每个
客户机代码在
值处创建的最后一个
文档要返回哪个
?或者您只是将其包含在
字符串\u agg
中?我刚刚做了另一个
agg
(数组\u agg(上次创建的文档))[1]作为上次创建的
。完成了(我想?)。非常感谢!呜呼!很高兴它成功了:)另一个选择是应用MAX--
MAX(上次创建文档时)
。噢,天哪。再次感谢!