agg_query
import TabItem from "@theme/TabItem"; import Tabs from "@theme/Tabs";
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:
<Tabs defaultValue="cURL" values={[ { label: "cURL", value: "cURL" }, { label: "Python", value: "Python" }, { label: "JavaScript", value: "JavaScript" }, { label: "go Chains", value: "go" } ]}
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"
}'
sql = "SELECT category, count(*) as counts FROM bookshop.book group by category"
db = client.db(mk)
db.query(sql)
const db = client.db(mk) const sql = "SELECT category, count(*) as
counts FROM bookshop.book group by category" await db.query(sql)
sql = "SELECT category, count(*) as counts FROM bookshop.book group by
category" resp, err := glitterClient.Query(ctx, sql)
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:
<Tabs defaultValue="cURL" values={[ { label: "cURL", value: "cURL" }, { label: "Python", value: "Python" }, { label: "JavaScript", value: "JavaScript" }, { label: "go Chains", value: "go" } ]}
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"
}'
sql = "SELECT category, MAX(price) as max_price, MIN(price) as min_price FROM bookshop.book group by category"
db = client.db(mk)
db.query(sql)
const db = client.db(mk);
const sql =
"SELECT category, MAX(price) as max_price, MIN(price) as min_price FROM bookshop.book group by category";
await db.query(sql);
sql = "SELECT category, MAX(price) as max_price, MIN(price) as min_price FROM bookshop.book group by category"
resp, err := glitterClient.Query(ctx, sql)
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:
<Tabs defaultValue="cURL" values={[ { label: "cURL", value: "cURL" }, { label: "Python", value: "Python" }, { label: "JavaScript", value: "JavaScript" }, { label: "go Chains", value: "go" } ]}
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"
}'
sql = "SELECT category, SUM(price) as sum_price, AVG(price) as avg_price FROM bookshop.book group by category"
db = client.db(mk)
db.query(sql)
const db = client.db(mk);
const sql =
"SELECT category, SUM(price) as sum_price, AVG(price) as avg_price FROM bookshop.book group by category";
await db.query(sql);
sql = "SELECT category, SUM(price) as sum_price, AVG(price) as avg_price FROM bookshop.book group by category"
resp, err := glitterClient.Query(ctx, sql)