PostgreSQL:创建topN查询选择

时间:2015-01-23 15:30:41

标签: sql postgresql

我尝试编写一些查询,允许我为特定表格的每一行选择N行(按特定顺序)。

换句话说 - 我有选择查询,可以获得我需要的所有结果, 但是我需要修改它以通过添加额外的过滤器(附加表)来返回统一的数据分布。

我有什么:

 Table "target_table"
   Column   |          Type          |       Modifiers       
------------+------------------------+-----------------------
 site_id    | integer                | not null default 0
 code       | integer                | not null default 0
 teaser_id  | integer                | not null default 0
 cpm        | bigint                 | not null
 show_type  | smallint               | not null default 1
 ctr_table  | character varying(255) | 


Table "table_filter_1"
   Column    |  Type  | Modifiers 
-------------+--------+-----------
 campaign_id | bigint | not null
 group_id    | bigint | not null


   View "view_1"
   Column    |  Type   | Modifiers 
-------------+---------+-----------
 teaser_id   | bigint  | 
 user_id     | bigint  | 
 campaign_id | bigint  | 

AND查询:

SELECT
    cache.teaser_id,
    cache.cpm,
    cache.show_type

FROM target_table as cache


    JOIN view_1 t
        ON cache.teaser_id = t.teaser_id
    JOIN table_filter_1 g
        ON t.campaign_id = g.campaign_id AND g.group_id =  55

WHERE cache.show_type IN (4444) AND
      cache.site_id = 999999
      AND (cache.code = 4)
GROUP BY cache.teaser_id, cache.cpm, cache.show_type
ORDER BY cpm DESC

LIMIT 500;

我也有桌子:

Table "table_filter_2"
   Column    |  Type  | Modifiers 
-------------+--------+-----------
 campaign_id | bigint | not null
 group_id    | bigint | not null

Table "table_filter_3"
   Column    |  Type  | Modifiers 
-------------+--------+-----------
 campaign_id | bigint | not null
 group_id    | bigint | not null

" table_filter_2"和" table_filter_3"类似于" table_filter_1"

目标是修改查询,以便它可以从" target_table"中选择数据。根据" table_filter_2"和" table_filter_3", 等

1)对于" table_filter_2"的每一行从" target_table"中选择5个值,然后为" table_filter_3"的每一行选择从" target_table";中选择5个值 2)联盟结果;
3)按teaser_id列分组以防止重复;
4)退货由" cpm"栏目结果。

我尝试以下一种方式做到:
1)在CTE中选择第一个查询的campaign_ids 2)从" table_filter_2"中选择group_ids;在CTE。
3)从" table_filter_3"中选择group_ids。在CTE。
4)过滤查询但未结束(错误)。

WITH temp_data AS (
    SELECT DISTINCT
        t.campaign_id,
        cache.cpm

    FROM target_table as cache

        JOIN view_1  t
            ON cache.teaser_id = t.teaser_id
        JOIN table_filter_1 g
            ON t.campaign_id = g.campaign_id  AND  g.group_id =  17

    WHERE cache.show_type IN (4444)
          AND cache.site_id = 999999
          AND (cache.geo_code = 4)
    GROUP BY cache.teaser_id, cache.cpm, cache.show_type
    ORDER BY cache.cpm DESC, t.campaign_id ASC

), cte_3 AS (
    SELECT cdg.group_id, cdg.campaign_id
        FROM campaigns_devices_groups cdg
        WHERE cdg.campaign_id IN (SELECT campaign_id FROM temp_data)
), cte_4 AS (
    SELECT cosg.group_id, cosg.campaign_id
        FROM campaigns_operation_systems_groups cosg
        WHERE cosg.campaign_id IN (SELECT campaign_id FROM temp_data)
)

SELECT (_row).*
FROM (
         SELECT unnest(array(

                           SELECT
                               cache.teaser_id,
                               cache.cpm,
                               cache.show_type

                           FROM target_table AS cache

                               JOIN view_1 t
                                   ON cache.teaser_id = t.teaser_id
                               JOIN table_filter_1 g
                                   ON t.campaign_id = g.campaign_id AND g.group_id = 17
                               JOIN table_filter_2 cdg
                                   ON t.campaign_id = cdg.campaign_id
                                      AND cdg.group_id = cte_3.group_id

                           WHERE cache.show_type IN (4444) AND
                                 cache.site_id = 999999
                                 AND (cache.geo_code = 4)
                           GROUP BY cache.teaser_id, cache.cpm, cache.show_type
                           ORDER BY cpm DESC

                           LIMIT 5
                       )) AS _row
         FROM cte_3
         WHERE group_id IS NOT NULL
         OFFSET 0
     ) AS _t;

任何帮助将不胜感激!

示例结果:

我得到的第一个(原始)查询:

 teaser_id |   cpm   | show_type |
-----------+---------+-----------+
   3391517 | 192644  |   4096    |
   401516  | 180232  |   4096    |
   3391215 | 165000  |   4096    |
   3391514 | 193244  |   4096    |
   3321513 | 192244  |   4096    |
   3491511 | 194544  |   4096    |
   3393512 | 122644  |   4096    |

对于最终查询或查询,我需要相同的结果格式。

0 个答案:

没有答案