After you generate SQL query statements using natural language to SQL (NL2SQL), some business scenarios require high-level summaries and in-depth analysis in addition to the query results. For this purpose, PolarDB provides the NL2SQL_SUMMARY model to generate intelligent summaries and deep insights from SQL query results. This helps you efficiently understand the core information behind the data.
Workflow
The core process of NL2SQL_SUMMARY involves two stages. First, it uses natural language to SQL (NL2SQL) model to convert natural language into an SQL query statement. Then, it uses the parsed SQL statement to generate an analytical summary.
Generate an SQL query statement from natural language using the NL2SQL model.
/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_nl2sql, select 'Query the credit score of each merchant') WITH (basic_index_name='schema_index',pattern_index_name='pattern_index');The generated SQL statement is as follows. Copy the SQL statement generated.
SELECT merchname AS merchant_name, limitscore AS credit_score FROM hkrt_merchant_info ORDER BY limitscore;Use the NL2SQL_SUMMARY model to summarize and analyze the SQL query results. Replace
<SQL_statement>with the code copied from Step 1./*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_nl2sql_summary,<SQL_statement> WITH (usr_query = <usr_query>);Parameters
Parameter
Description
Example
<usr_query>
The user's question. This parameter clarifies the requirements for data analytics and summarization.
'Merchant type distribution'.<SQL_statement>
The SQL query statement generated by the NL2SQL model. It is used to fetch the data for summarization. Do not add a semicolon at the end.
SELECT merchname AS merchant_name, limitscore AS credit_score FROM hkrt_merchant_info ORDER BY limitscore.Example
/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_nl2sql_summary, SELECT merchname AS merchant_name, limitscore AS credit_score FROM hkrt_merchant_info ORDER BY limitscore ) WITH (usr_query='Query the credit score of each merchant');Output:
This query returned the credit scores for 15 merchants, ranging from 60 to 95. The lowest score is 60 (Future Star Merchant), and the highest is 95 (Tech City Merchant). Overall, most merchants have scores above 70, indicating good credit standing.
SQL query results:
No.
Credit Score
Merchant Name
1
60
Future Star Merchant
2
65
Stellar Tech Merchant
3
68
Innovative Future Merchant
4
70
Stellar Glory Merchant
5
72
Stellar Dream Merchant
6
75
Future Tech Merchant
7
80
Dream Tech Merchant
8
82
Dream Tech Merchant
9
85
Xiaotian Tech Merchant
10
85
Glorious Dream Merchant
11
88
Glorious Future Merchant
12
90
Glorious Tech Merchant
13
90
Glorious Star Merchant
14
92
Tech Star Merchant
15
95
Tech City Merchant
Best practices
You can combine NL2SQL and NL2SQL_SUMMARY to automate the entire flow from natural language queries to intelligent summaries.
Ensure that your questions are clear. Providing a clear
usr_queryimproves the quality and relevance of the summary.For better visualization, use the NL2Chart feature to generate charts that complement the summary.
Recommended scenarios:
Scenario
Typical application
Data overview
Quickly understand the data distribution across different categories or time periods in a database.
Decision support
Provide data-driven summary recommendations for business decisions.
Anomaly detection
Detect abnormal categories or trends and suggest possible causes.
Report writing
Automatically generate key sections of data reports to improve efficiency.
NL2SQL_SUMMARY quickly extracts key information from raw data and creates structured summaries. This provides a solid foundation for subsequent data analytics and decision-making.