I have table which has store number and Fruits in it like below
Store Number | fruit |
---|---|
111 | Apple |
121 | Orange |
111 | Pinable |
132 | Apple |
145 | Grapes |
121 | Apple |
111 | Apple |
I want to to add a generated column "Number of Apples", which tells me how many apples are there in each store. Resultant table would look like this
I am a beginner to PostgreSQL. Help me if anybody knows how to add this generated column in PostgreSQL
2
Answers
You can get what you are asking for by using a subquery in your query:
You can use the window function
COUNT()
andCASE
clause to performs a conditional count of the Apple fruits for each store number :Demo here