Skip to main content

agg_query

Aggregated Query

If you want to focus on the entire status of the data rather than partial data, you can use the GROUP BY statement in conjunction with aggregate functions to create an aggregated query that will help you comprehend the general situation of the data. For example, if you want to know the total number of books with the category "web3", you can group the basic information of books by the category column and then use the COUNT() function to count:

curl --location 'http://api.xian.glitter.link/blockved/glitterchain/index/sql/query' \
--header 'Content-Type: application/json' \
--data '{
"sql":"SELECT category, count(*) as counts FROM bookshop.book group by category"
}'

For example, if you want to know the highest and lowest prices of books with the category "web3", you can group the basic information of books by the category column and then use the MIN() and MAX() functions to calculate:

curl --location 'http://api.xian.glitter.link/blockved/glitterchain/index/sql/query' \
--header 'Content-Type: application/json' \
--data '{
"sql":"SELECT category, MAX(price) as max_price, MIN(price) as min_price FROM bookshop.book group by category"
}'

If you want to know the total and average price of books in the category "web3," group the basic information of the books by the category column then use the SUM() and AVG() functions to calculate:

curl --location 'http://api.xian.glitter.link/blockved/glitterchain/index/sql/query' \
--header 'Content-Type: application/json' \
--data '{
"sql":"SELECT category, SUM(price) as sum_price, AVG(price) as avg_price FROM bookshop.book group by category"
}'