2016-09-07 1 views
0

Ceci est ma requête SQL et je veux que ces résultats soient affichés verticalement. J'ai aussi cherché google pour ceci où j'ai trouvé pour activer le mode bascule en utilisant \x\g\x mais je ne sais pas où mettre cette syntaxe. S'il vous plaît aider à obtenir une sortie comme ceci:Comment afficher verticalement le résultat de ma requête dans postgresql?

enter image description here

Mais, cette requête donne une sortie comme ceci:

enter image description here

   select 
      round(
      100.00 * 
      (sum(case when "WELL_AGE" <= '5' AND "CONC_ARSC" <= '10' then 1 else 0 end))/(sum(case when "WELL_AGE" <= '5' then 1 else 0 end)),1) conc_arscbelow5_wellageGrp, 

      round(
      100.00 * 
      (sum(case when "WELL_AGE" >= '6' AND "WELL_AGE" <= '10' AND "CONC_ARSC" <= '10' then 1 else 0 end))/(sum(case when "WELL_AGE" >= '6' AND "WELL_AGE" <= '10' then 1 else 0 end)),1) conc_arscbet6_10wellageGrp, 

      round(
      100.00 * 
      (sum(case when "WELL_AGE" >= '11' AND "WELL_AGE" <= '15' AND "CONC_ARSC" <= '10' then 1 else 0 end))/(sum(case when "WELL_AGE" >= '11' AND "WELL_AGE" <= '15' then 1 else 0 end)),1) conc_arscbet11_15_wellageGrp, 

      round(
      100.00 * 
      (sum(case when "WELL_AGE" >= '16' AND "WELL_AGE" <= '30' AND "CONC_ARSC" <= '10' then 1 else 0 end))/(sum(case when "WELL_AGE" >= '16' AND "WELL_AGE" <= '30' then 1 else 0 end)),1) conc_arscbet16_30wellageGrp, 

      round(
      100.00 * 
      (sum(case when "WELL_AGE" >= '31' AND "WELL_AGE" <= '50' AND "CONC_ARSC" <= '10' then 1 else 0 end))/(sum(case when "WELL_AGE" >= '31' AND "WELL_AGE" <= '50' then 1 else 0 end)),1) conc_arscbet31_50wellageGrp, 

      round(
      100.00 * 
      (sum(case when "WELL_AGE" > '50' AND "CONC_ARSC" <= '10' then 1 else 0 end))/(sum(case when "WELL_AGE" > '50' then 1 else 0 end)),1)conc_arscabove50_wellageGrp 


      from public."Arsenic_Test"; 
+0

Voir cette http://stackoverflow.com/questions/23060256/postgres-transpose-rows-to-columns –

+1

La commande '\ x' est seulement valide pour le client de ligne de commande' psql' et elle va simplement "faire tourner" la sortie de n'importe quelle requête SQL. Il n'y a pas de métacommande 'psql'' \ x \ g \ x'. '\ g' exécutera l'instruction à partir du tampon d'édition. –

+0

Alors, que dois-je faire pour répondre à mes besoins? –

Répondre

0

J'espère que cela correspondra à vos besoins.

En utilisant hstore:

with t as (SELECT '80.13' aS "0-5", '80.7' AS "6-10", '81.6' AS "11-15", '84.27' AS "16-30", '84.04' AS "31-50", '85.33' AS ">50") 
SELECT * 
FROM (SELECT (each(hstore(t))).* FROM t) AS tmp (well_age, below10_conc_arsc_wells) 

Remplacer t avec votre table. En utilisant JSON:

with t as (SELECT '80.13' aS "0-5", '80.7' AS "6-10", '81.6' AS "11-15", '84.27' AS "16-30", '84.04' AS "31-50", '85.33' AS ">50") 
SELECT * 
FROM (SELECT (json_each_text(row_to_json(t))).* FROM t) AS tmp (well_age, below10_conc_arsc_wells) 
0

Utilisez le groupe sur range type à:

with r (r,s) as (values 
    (int4range(null,5,'[]'), '0 - 5'), 
    (int4range(6,10,'[]'), '6 - 10'), 
    (int4range(11,15,'[]'), '11 - 15'), 
    (int4range(16,30,'[]'), '16 - 30'), 
    (int4range(31,50,'[]'), '31 - 50'), 
    (int4range(50,null,'(]'), '>50') 
) 
select s, 100* count("CONC_ARSC" <= '10' or null)/count(*) 
from 
    public."Arsenic_Test" 
    inner join 
    r on "WELL_AGE" <@ r 
group by s;