In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Editor to share with you how to achieve group by range in postgresql, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Set up a test table
Testdb=# CREATE TEMP TABLE team (
Id serial
Name text
Birth_year integer
Salary integer
);
Insert record
Testdb=# INSERT INTO team (name, birth_year, salary)
VALUES ('Gabriel', 1970, 44000)
('Tom', 1972, 36000)
('Bill', 1978, 39500)
('Bob', 1980, 29000)
('Roger', 1976, 26800)
('Lucas', 1965, 56900)
('Jerome', 1984, 33500)
('Andrew', 1992, 41600)
('John', 1991, 40000)
('Paul', 1964, 39400)
('Richard', 1986, 23000)
('Joseph', 1988, 87000)
('Jason', 1990, 55000)
Query result
Testdb=# WITH series AS (
SELECT generate_series (1950, 2000, 10) AS time_start-- 1950 = min, 2010 = max, 10 = 10 year interval
), range AS (
SELECT time_start, (time_start + 9) AS time_end FROM series-- 9 = interval (10 years) minus 1
)
SELECT time_start, time_end
(SELECT count (*) FROM team WHERE birth_year BETWEEN time_start AND time_end) as team_members
Round ((SELECT AVG (salary) FROM team WHERE birth_year BETWEEN time_start AND time_end), 2) as salary_avg
(SELECT MIN (salary) FROM team WHERE birth_year BETWEEN time_start AND time_end) as salary_min
(SELECT MAX (salary) FROM team WHERE birth_year BETWEEN time_start AND time_end) as salary_max
FROM range
Output result:
Time_start | time_end | team_members | salary_avg | salary_min | salary_max
-+-
1950 | 1959 | 0 |
1960 | 1969 | 2 | 48150.00 | 39400 | 56900
1970 | 1979 | 4 | 36575.00 | 26800 | 44000
1980 | 1989 | 4 | 43125.00 | 23000 | 87000
1990 | 1999 | 3 | 45533.33 | 40000 | 55000
2000 | 2009 | 0 |
(6 rows)
Testdb=#
These are all the contents of the article "how to achieve group by range in postgresql". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.