sql
  • postgresql
  • pivot
  • crosstab
  • 2016-09-07 85 views 0 likes 
    0

    这是我的SQL查询,我想要这些结果垂直显示。我也搜索谷歌这一点,我发现通过使用\x\g\x打开切换模式,但我不知道该把语法放在哪里。请帮助得到的输出是这样的:如何在postgresql中垂直显示我的查询结果?

    enter image description here

    但是,我的这个查询给出输出是这样的:

    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

    看到这个http://stackoverflow.com/questions/23060256/postgres-transpose-rows-to-columns –

    +1

    '\ x'命令只对命令行客户端'psql'有效,它将简单地“旋转”任何SQL查询的输出。没有'psql' metacommand'\ x \ g \ x'。 '\ g'将从编辑缓冲区运行语句。 –

    +0

    那么,我需要做些什么来实现我的需求呢? –

    回答

    0

    我希望这将匹配您的要求。

    使用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) 
    

    与表替换吨。 使用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

    使用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; 
    

     相关问题

    • 暂无相关问题^_^