Postgresql 9.2

It is necessary to display all these queries below in one table (I collect statistics)

SELECT "customer"."id", "customer"."email", "customer"."unsubscribed_types" FROM "customer" WHERE "customer"."unsubscribed_types" = 'daily_weekdays' OR "customer"."unsubscribed_types" = 'daily_weekends' OR "customer"."unsubscribed_types" = 'weekly_free' OR "customer"."unsubscribed_types" = 'weekly_best_weekly' OR "customer"."unsubscribed_types" = 'special_partner' OR "customer"."unsubscribed_types" = 'bonus_activation' LIMIT 100; 

 SELECT "customer".email, "customer".id, "customer".unsubscribed_types FROM "customer" WHERE "customer".unsubscribed_types ISNULL OR "customer".unsubscribed_types = '' LIMIT 100; 

 SELECT "customer".email, "customer".id, "customer".unsubscribed_types FROM "customer" WHERE "customer".unsubscribed_types = 'daily_weekdays' AND "customer".unsubscribed_types = 'daily_weekends' AND "customer".unsubscribed_types = 'weekly_free' AND "customer".unsubscribed_types = 'weekly_best_weekly' AND "customer".unsubscribed_types = 'special_partner' AND "customer".unsubscribed_types = 'bonus_activation'; LIMIT 100; 

 SELECT "customer".id, "customer".email, "customer".unsubscribed_types FROM "customer" WHERE unsubscribed_types = 'daily_weekdays'; LIMIT 100; 

Please note I do not combine and display statistics (for example)

 ------------------------------------------------- | Запрос 1 | Сколько нашел людей из 1-го селекта| |-----------------------------------------------| | Запрос 2 | Сколько нашел людей из 2-го селекта| |-----------------------------------------------| | Запрос 3 | Сколько нашел людей из 3-го селекта| |-----------------------------------------------| | Запрос 4 | Сколько нашел людей из 4-го селекта| ------------------------------------------------- 

    3 answers 3

    you can use the union of requests ( union ):

     select "запрос 1", count(*) from ... where ... union select "запрос 2", count(*) from ... where ... union ... 

      I will propose such an option: Write a procedure in which: 1) A table will be created defining your resulting table 2) Sampling the source table 3) Pass through each sample and insert data into the table created in paragraph 1

        Here's how to solve the problem.

         SELECT "request_1" AS "Запрос 1", "request_2" AS "Запрос 2", "request_3" AS "Запрос 3", "request_4" AS "Запрос 4" FROM ( -- Исполняем запрос 1 SELECT count("customer"."id") FROM "customer" WHERE "customer"."unsubscribed_types" = 'daily_weekdays' OR "customer"."unsubscribed_types" = 'daily_weekends' OR "customer"."unsubscribed_types" = 'weekly_free' OR "customer"."unsubscribed_types" = 'weekly_best_weekly' OR "customer"."unsubscribed_types" = 'special_partner' OR "customer"."unsubscribed_types" = 'bonus_activation' ) AS "request_1", ( -- Исполняем запрос 2 SELECT count("customer"."id") FROM "customer" WHERE "customer"."unsubscribed_types" ISNULL OR "customer"."unsubscribed_types" = '{}' ) AS "request_2", ( -- Исполняем запрос 3 SELECT count("customer"."id") FROM "customer" WHERE "customer"."unsubscribed_types" = 'daily_weekdays' AND "customer"."unsubscribed_types" = 'daily_weekends' AND "customer"."unsubscribed_types" = 'weekly_free' AND "customer"."unsubscribed_types" = 'weekly_best_weekly' AND "customer"."unsubscribed_types" = 'special_partner' AND "customer"."unsubscribed_types" = 'bonus_activation' ) AS "request_3", ( -- Исполняем запрос 4 SELECT count("customer"."id") FROM "customer" WHERE "unsubscribed_types" = 'daily_weekdays' ) AS "request_4", ;