2016-10-14 1 views
0

J'essaie d'exécuter une requête sql dans Postgres qui nécessite un tableau croisé. Je n'ai jamais utilisé de requêtes de tableau croisé auparavant. Mes 3 tableaux sont présentés ci-dessous:Requête Postgres Crosstab pour compter les statuts par ville

emplacements TABLEAU:

location_id, id_langue, chemin

crm_statuses TABLE:

crm_status_id, crm_status_name

store_crm TABLE:

store_crm_id, état (références dans crm_status_id de table crm_statuses), location_id (références dans location_id de table emplacements)

Je veux obtenir les emplacements que les colonnes de la table endroits en se joignant à la table store_crm ou au moins de les écrire codé en dur comme ils sont seulement 3 (Londres, Manchester, Leeds). En tant que lignes, je veux obtenir les statuts crm. Comme le contenu que je veux compter combien de magasins actifs, inactifs et en attente, chaque emplacement a. Les actifs, inactifs, en attente sont mes crm_statuses. Le tableau de résultats souhaité aura le format suivant.

Status London Manchester Leeds 
Active 2  4   5 
Inactive 6  1   3 
Pending 4  4   5 

Comment puis-je y parvenir?

+0

Vous shouldn » t utilisez un tableau croisé pour afficher une table comme celle-là. Il peut être facilement réalisé avec une simple boucle en php. –

+0

Je dois le faire dans Crosstab parce qu'il va être exécuté dans un système de rapport. –

Répondre

0

Vous pouvez hardcode villes comme colonnes dans une requête suivante:

SELECT 
    * 
FROM crosstab(' 
     SELECT 
      cs.crm_status_name, 
      l.path, 
      COUNT(*) 
     FROM store_crm AS sc 
     JOIN locations AS l ON (sc.location_id=l.location_id) 
     JOIN crm_statuses AS cs ON (cs.crm_status_id=sc.status) 
     GROUP BY cs.crm_status_name, l.path 
     ORDER BY 1,2 
    ', 
    ' 
     SELECT 
      path 
     FROM 
      locations 
    ') AS f("Status" text, "London" text, "Manchester" text, "Leeds" text); 

Avec un résultat:

Status | London | Manchester | Leeds 
----------+--------+------------+------- 
Active | 1  | 2   | 1 
Inactive | 2  | 1   | 4 
Pending |  | 1   | 1 
(3 rows) 

Si vous avez le schéma de base de données comme:

test=# \d+ locations 
                Table "public.locations" 
    Column | Type |       Modifiers       | Storage | Stats target | Description 
-------------+---------+-----------------------------------------------------------------+----------+--------------+------------- 
location_id | integer | not null default nextval('locations_location_id_seq'::regclass) | plain |    | 
lang_id  | integer |                 | plain |    | 
path  | text |                 | extended |    | 
Indexes: 
    "locations_pkey" PRIMARY KEY, btree (location_id) 
Referenced by: 
    TABLE "store_crm" CONSTRAINT "store_crm_location_id_fkey" FOREIGN KEY (location_id) REFERENCES locations(location_id) 

test=# \d+ crm_statuses 
                 Table "public.crm_statuses" 
    Column  | Type |        Modifiers        | Storage | Stats target | Description 
-----------------+---------+----------------------------------------------------------------------+----------+--------------+------------- 
crm_status_id | integer | not null default nextval('crm_statuses_crm_status_id_seq'::regclass) | plain |    | 
crm_status_name | text |                  | extended |    | 
Indexes: 
    "crm_statuses_pkey" PRIMARY KEY, btree (crm_status_id) 
Referenced by: 
    TABLE "store_crm" CONSTRAINT "store_crm_status_fkey" FOREIGN KEY (status) REFERENCES crm_statuses(crm_status_id) 

test=# \d+ store_crm 
                Table "public.store_crm" 
    Column | Type |       Modifiers        | Storage | Stats target | Description 
--------------+---------+------------------------------------------------------------------+---------+--------------+------------- 
store_crm_id | integer | not null default nextval('store_crm_store_crm_id_seq'::regclass) | plain |    | 
status  | integer |                 | plain |    | 
location_id | integer |                 | plain |    | 
Indexes: 
    "store_crm_pkey" PRIMARY KEY, btree (store_crm_id) 
Foreign-key constraints: 
    "store_crm_location_id_fkey" FOREIGN KEY (location_id) REFERENCES locations(location_id) 
    "store_crm_status_fkey" FOREIGN KEY (status) REFERENCES crm_statuses(crm_status_id)