Create a column that shows SQL percentage

Asked

Viewed 164 times

0

I have a query that searches 2 values and when divided should return the percentage, but this with integer number.

I believe I’m wrong here :

(COUNT(distinct neighborhood) / COUNT(distinct tbl.city)) as percentual

How do I make her understand the value of the field and not the account

SELECT 

city, 
neighborhood, 
count(neighborhood) as Soma_bairros,

(select count(city) FROM tb_listings WHERE city = tbl.city) as Soma_cidade ,

(COUNT(distinct neighborhood) / COUNT(distinct tbl.city)) as percentual

FROM tb_listings tbl

GROUP BY city, neighborhood 

ORDER BY city, neighborhood DESC

Current result:

Percentual retorna 100 e não 25 %

You would have to classify field as INT ?

  • Which one SGBD? What type of column data neighborhood?

  • would be that CREATE TABLE tb_listings ( "advertiser_id" varchar, "listing_id" varchar, "portal" varchar, "city" varchar, "Neighbhoodor" varchar);

  • Ah disguises. What the SGBD?

  • Sorry postgre, I’m using the sql fiddle tool

  • Pass the fiddle link then

  • http://sqlfiddle.com/#! 15/1d25e/316

  • Ué, but the two sides of the division give 1, there will always return 1 even. You better explain what are your data and what you want to extract from them

  • Take Neighborhood from group by , will always give 1 , multiply by 100 to have in percentage.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.