How to create nested SELECT COUNT with alias in Postgres

ε祈祈猫儿з 提交于 2019-12-24 04:17:11

问题


i'm writing the following SQL query for my Postgres database:

SELECT(
(SELECT count(*) as A FROM merchant WHERE nome LIKE 'A%'),
(SELECT count(*) as B FROM merchant WHERE nome LIKE 'B%'),
(SELECT count(*) as C FROM merchant WHERE nome LIKE 'C%'),
(SELECT count(*) as D FROM merchant WHERE nome LIKE 'D%'),
(SELECT count(*) as E FROM merchant WHERE nome LIKE 'E%'),
(SELECT count(*) as F FROM merchant WHERE nome LIKE 'F%'),
(SELECT count(*) as G FROM merchant WHERE nome LIKE 'G%'),
(SELECT count(*) as H FROM merchant WHERE nome LIKE 'H%'),
(SELECT count(*) as I FROM merchant WHERE nome LIKE 'I%'),
(SELECT count(*) as J FROM merchant WHERE nome LIKE 'J%'),
(SELECT count(*) as K FROM merchant WHERE nome LIKE 'K%'),
(SELECT count(*) as L FROM merchant WHERE nome LIKE 'L%'),
(SELECT count(*) as M FROM merchant WHERE nome LIKE 'M%'),
(SELECT count(*) as N FROM merchant WHERE nome LIKE 'N%'),
(SELECT count(*) as O FROM merchant WHERE nome LIKE 'O%'),
(SELECT count(*) as P FROM merchant WHERE nome LIKE 'P%'),
(SELECT count(*) as Q FROM merchant WHERE nome LIKE 'Q%'),
(SELECT count(*) as R FROM merchant WHERE nome LIKE 'R%'),
(SELECT count(*) as S FROM merchant WHERE nome LIKE 'S%'),
(SELECT count(*) as T FROM merchant WHERE nome LIKE 'T%'),
(SELECT count(*) as U FROM merchant WHERE nome LIKE 'U%'),
(SELECT count(*) as V FROM merchant WHERE nome LIKE 'V%'),
(SELECT count(*) as W FROM merchant WHERE nome LIKE 'W%'),
(SELECT count(*) as X FROM merchant WHERE nome LIKE 'X%'),
(SELECT count(*) as Y FROM merchant WHERE nome LIKE 'Y%'),
(SELECT count(*) as Z FROM merchant WHERE nome LIKE 'Z%')
)

The output is one column named "row", with the following content:

(26,20,28,13,15,9,13,16,13,1,0,13,20,7,10,20,0,17,44,25,3,8,7,1,2,2)

I should get 26 rows (named "A", "B", and so on... according to my alias) with the related total. Why does it give me one row?

If i read it through PHP var_dump the output is the following:

string(68) "(26,20,28,13,15,9,13,16,13,1,0,13,20,7,10,20,0,17,44,25,3,8,7,1,2,2)"

What's wrong? Am i making any mistake or is it a Postgres related matter?


回答1:


You want to create a separate row for each character. One way is to generate all the characters and then aggregate by them. Here is one approach:

select chr(chars.c + ascii('A')) as c,
       sum(case when ascii(left(m.nome, 1)) = chars.c + ascii('A') then 1 else 0 end)
from generate_series(0, 25) as chars(c) cross join
     merchant m
group by c;

EDIT:

Alan's suggestion is a better query:

select chr(chars.c + ascii('A')) as c,
       count(m.nome)
from generate_series(0, 25) as chars(c) left join
     merchant m
     on ascii(left(m.nome, 1)) = chars.c + ascii('A')
group by c;



回答2:


Apart from the fact that you accidentally used PostgreSQL's ROW constructor, here's a solution to generate the required rows:

SELECT chr(letter), count(nome)
FROM 
  generate_series(65,90) letters(letter)
LEFT JOIN
  merchant ON nome LIKE chr(letter) || '%'
GROUP BY chr(letter)
ORDER BY 1


来源:https://stackoverflow.com/questions/30084359/how-to-create-nested-select-count-with-alias-in-postgres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!