Method, system, application, computer device and storage medium for generating SQL query statements from natural language

By analyzing user questions using preset splitting rules and professional knowledge graphs, and combining SQL generators and NIO multiplexing mechanisms, the problems of inaccurate generation and slow response in existing Text2SQL technologies have been solved, achieving stable SQL query generation and efficient multi-dimensional data processing.

CN122196011APending Publication Date: 2026-06-12联通数智医疗科技有限公司

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
联通数智医疗科技有限公司
Filing Date
2026-03-06
Publication Date
2026-06-12

Smart Images

  • Figure CN122196011A_ABST
    Figure CN122196011A_ABST
Patent Text Reader

Abstract

The application discloses a method, system, application, computer device and storage medium for generating an SQL query statement from natural language. The method for generating an SQL query statement from natural language comprises the following steps: generating a query task from a user question after analyzing the user question from input natural language according to preset splitting rules; extracting multi-dimensional data from the query task to generate a parameter dictionary; and inputting the parameter dictionary into an SQL generator to generate an SQL query statement. The application is used to improve query accuracy and response speed.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the technical field of computer data manipulation, and in particular to a method, system, application, computer device, and storage medium for generating SQL query statements from natural language. Background Technology

[0002] Typically, business data is stored in databases. To realize the value of data in BI (Business Intelligence) analysis, users need to master the language of interacting with databases. Text2SQL (text to SQL) is a technology that automatically converts user-input text into SQL query language. The process involves users expressing their query needs to the BI system using natural language. Text2SQL directly converts these needs into executable SQL query statements through an artificial intelligence system. The SQL engine then executes the database query to retrieve the data, which is finally presented to the user through a visual interface. With the popularity and widespread adoption of large-scale models, Text2SQL has gradually upgraded from the traditional deep learning architecture to a large-scale model architecture (LLM). BI analysis places great emphasis on accuracy and interpretability. For example, financial statistics and medical cases cannot tolerate any errors; every number and text presented must have an interpretable logic.

[0003] However, existing Text2SQL solutions face significant challenges. For highly specialized, heterogeneous data standards across sub-businesses, and stringent requirements for logical accuracy, especially when dealing with complex temporal causal logic, traditional Generative Large Models (LLMs) often struggle to reliably handle these strongly constrained logics, making it difficult to generate accurate SQL queries. The problems faced by intelligent BI systems are primarily due to the diverse nature of user question descriptions, making standardized processing difficult and leading to inaccurate SQL queries. Even with techniques like optimizing LLM output through prompt engineering and model fine-tuning, or using LLM-suggested keywords to populate schema link templates for SQL query generation, the phenomenon of inconsistent results when directly using LLM for the same question persists. Excessive reliance on LLM results in unstable SQL accuracy. Furthermore, when users pose questions involving multiple metrics, LLMs often struggle to determine the relationships between these metrics, instead opting to split the query into multiple SQL statements, which decreases accuracy and increases response time. Summary of the Invention

[0004] To overcome one or more technical problems existing in the prior art, this application provides a method, system, application, computer device, and storage medium for generating SQL query statements from natural language, so as to improve query accuracy and response speed.

[0005] The technical solution provided in this application is as follows.

[0006] In a first aspect, the present invention provides a method for generating SQL query statements from natural language, comprising the steps of: The query task is generated after parsing the user's question from the input natural language according to the preset splitting rules; Extract multi-dimensional data from the query task to generate a parameter dictionary; Input the parameter dictionary into the SQL generator to generate an SQL query statement.

[0007] As one preferred embodiment of the first aspect, the content of the splitting rules includes constraints, splitting modes, and task templates; The constraints include relevant professional knowledge information; The splitting mode includes corresponding analysis content information; The task template includes the processing information for the corresponding query task.

[0008] More preferably, the step of generating a query task from the input natural language statement using a large language model according to preset splitting rules includes: Use large language models to identify and parse user questions described in natural language; The large language model retrieves a rule base containing multiple splitting rules under constraints to obtain one or more splitting rules that match the user's question; The rule base outputs one or more splitting rules to the task self-arranging agent; The task self-arranging agent uses the user question and one or more corresponding splitting rules to form a structured text describing the query task.

[0009] As another preferred implementation of the first aspect, the step of extracting multi-dimensional data from the query task to generate a parameter dictionary includes: Using NLP tools based on professional knowledge graphs, named entity recognition is performed on the query task to obtain the corresponding query dimension indicators; Use the SUTime time library to identify time parameters in query tasks; Dependency parsing is used to identify filtering conditions in query tasks; Use text classification models to identify the analysis methods in query tasks; The dimension indicators to be queried, the time parameters, the filtering conditions, and the analysis methods are each mapped to a corresponding parameter dictionary.

[0010] More preferably, the step of inputting the parameter dictionary into the SQL generator to generate the SQL query statement includes: Construct a complete table join topology based on the parameter dictionary to determine the JOIN relationship, and map the fields to be queried to the SQL syntax tree; Generate the corresponding SQL query statement based on the SQL syntax tree.

[0011] In a further preferred embodiment, when performing database dialect conversion to generate corresponding SQL query statements, if multiple query dimension indicators are involved, the SQL generator uses a decision tree based on the NIO multiplexing mechanism to determine the relationship between the multiple query dimension indicators, and then selects the corresponding SQL query statement generation strategy based on the relationship between the multiple query dimension indicators. The SQL query generation strategies include single SQL join query, CTE join query, and parallel multi-SQL query.

[0012] In a second aspect, the present invention provides a system for generating SQL query statements from natural language, including a query task generation module, a rule base, an extraction module, and an SQL generator; The rule base stores preset splitting rules; The query task generation module is used to generate query tasks after parsing user questions from the input natural language according to preset splitting rules. The extraction module is used to extract multi-dimensional data from the query task to generate a parameter dictionary; The SQL generator is used to generate SQL query statements based on the parameter dictionary.

[0013] Thirdly, the present invention provides an application of the method for generating SQL query statements from natural language as described in any of the preceding claims in medical case querying, wherein the preset splitting rules are set according to medical knowledge; the query task is a medical case query task; and it further includes the following steps: Input the generated SQL query into the medical case knowledge base to retrieve the corresponding case results; The case results and corresponding user questions are added to the medical case knowledge base and the large language model is continuously learned. When performing a new round of case queries on the medical case knowledge base, the user's question in natural language is parsed first, and then the medical case knowledge base is searched for similar user questions. If they are found, the case results corresponding to the user's question are directly output. If they are not found, the method of generating SQL query statements from natural language is executed.

[0014] Fourthly, the present invention provides a computer device, the computer device including a processor and a memory; the memory stores a computer program that can be executed by the processor; when the computer program is executed by the processor, it implements the method for generating SQL query statements from natural language as described in any of the preceding claims.

[0015] Fifthly, the present invention provides a storage medium storing a computer program; the computer program is used to cause a computer device to execute the method for generating SQL query statements from natural language according to any of the preceding claims.

[0016] Compared with existing technologies, the methods, systems, applications, computer devices, and storage media for generating SQL query statements from natural language in this application have the following advantages: Compared to existing technologies that use large language models for the entire process of inference and SQL generation, this application only uses large language models in the stage of parsing the query intent of natural language. By using preset splitting rules, the large language model can output structured query task content. The large language model is completely removed when extracting multi-dimensional data and generating SQL query statements. The SQL generator processes the data through deterministic logic, and the generated SQL query statements can significantly improve the accuracy of the query under the constraint of professional knowledge. For the same natural language content, the generated SQL query statement results can be guaranteed to be stable each time, avoiding the probabilistic defects of large language models. For the generation of SQL query statements containing multiple query dimensions, an NIO multiplexing mechanism is used to select the generation strategy, which can maintain accuracy and will not reduce the performance of the SQL generator due to the increase in the number of query dimensions. The response time can also be kept at a high level. Attached Figure Description

[0017] Figure 1 This is a flowchart of one method for generating SQL query statements from natural language according to the present invention.

[0018] Figure 2 This is a system architecture diagram of one of the present inventions for generating SQL query statements from natural language.

[0019] Figure 3 This is a flowchart illustrating the application of one of the methods for generating SQL query statements from natural language in medical case queries. Detailed Implementation

[0020] Please refer to the diagrams, where the same component symbols represent the same components. The principles of the invention are illustrated by way of example implemented in a suitable operating environment. The following description is based on the illustrative specific embodiments of the invention and should not be construed as limiting the invention to other specific embodiments not detailed herein.

[0021] As used herein, the term "preferred" is meant as an example, illustration, or illustration. Any aspect or design described herein as "preferred" need not be construed as being more advantageous than other aspects or designs. Rather, the use of the term "preferred" is intended to present the concept in a specific manner. As used in this application, the term "or" is intended to mean an inclusive "or" rather than an exclusionary "or." That is, unless otherwise specified or clear from the context, "X uses A or B" naturally includes either of the permutations. That is, if X uses A; X uses B; or X uses both A and B, then "X uses A or B" is satisfied in any of the foregoing examples.

[0022] The first embodiment of the present invention is described below.

[0023] Please refer to Figure 1 This embodiment provides a method for generating SQL query statements from natural language. The method for generating SQL query statements from natural language in this embodiment includes the following steps.

[0024] S1. Generate a query task by parsing the user's question from the input natural language according to the preset splitting rules.

[0025] The splitting rules include constraints, splitting patterns, and task templates. Constraints include relevant professional knowledge information; splitting patterns include relevant analysis content information; and task templates include processing information for the corresponding query task. In this embodiment, splitting rules are stored in a rule database. Constraints include one or more professional knowledge graphs, result output specifications, and triggering conditions (regular expression matching or keyword combinations). The analysis content information of the splitting pattern may include rule ID, rule name, applicable scenarios, keyword list, similarity threshold, required field list, optional field list, verification rules, splitting strategy description, task execution order, dependencies between multiple query tasks, and query tasks that can be executed in parallel. The processing information of the task template includes the task name, processing method, associated MCP tool, verification method, etc., for each query task. When setting a splitting pattern, parsing examples can be set for the applicable scenarios, triggering conditions, verification rules, splitting strategy description, task execution order, and dependencies between multiple query tasks based on the relevant professional knowledge information. This improves the accuracy of the large language model in recognizing the corresponding content. The dependencies between multiple query tasks are described by a DAG (Directed Acyclic Graph).

[0026] In this embodiment, the splitting rules are edited using APL (Analysis Plan Language). The splitting rules are pre-set based on the corresponding professional knowledge graph (e.g., medical knowledge). Using the splitting rules can converge the divergent natural language content into a limited splitting result, thereby reducing the decision complexity of the large language model (from free generation to selection), ensuring the consistency and predictability of the splitting results, and providing structured text for subsequent multi-dimensional data extraction that is easy to recognize. Multiple different professional knowledge graphs can be selected for dynamic expansion to adapt to more professional content query scenarios.

[0027] The specific process of step S1 includes: S11. Use a large language model to identify and parse the user's question described in the natural language statement; the large language model used in this step S11 can be one of GPT-4, Claude 3.7, Gemini 2.5 Pro, DeepSeek V3, Grok 3, Qwen, etc. S12. Under constraints, the large language model retrieves a rule base containing multiple splitting rules to obtain one or more splitting rules that match the user's question. In this embodiment, the large language model can learn all the splitting rules in the rule base in advance. The rule base sets the splitting mode and associates it with the corresponding task template through the rule ID. In other embodiments, one or more of the rule name, applicable scenario, and triggering condition can also be set to associate the splitting mode with the corresponding task template. In this step S12, the rule base can be an independent database or a sub-database of the queried database. S13. The rule base outputs one or more corresponding splitting rules to the task self-arranging agent; in this step S13, the task self-arranging agent can be designed based on a general large model; S14. Using a task self-arranging agent, a structured text describing the query task is generated based on the user question and one or more corresponding splitting rules. In this embodiment, the structured text describing the query task is in JSON format, and the task self-arranging agent specifically uses the splitting pattern in the splitting rules to break down a user question into multiple query tasks. In step S14, the structured text describing the query task can also be in Markdown format.

[0028] S2. Extract multi-dimensional data from multiple query tasks to generate a parameter dictionary.

[0029] In this step S2, the specific data dimensions are generated based on the corresponding professional knowledge graph. Different professional knowledge requires different data dimensions to be extracted. Each dimension has its own corresponding indicators. For example, under the constraints of the medical knowledge graph, if the input natural language is "the situation of sexually transmitted disease outpatient examination in the past three years", then the corresponding data dimensions should at least include time and blood test items. The measurement standard for each blood test item (such as white blood cell count, platelet count, etc.) is the corresponding indicator. The indicator is expressed as a numerical range, the corresponding unit, negative or positive, etc.

[0030] In SQL queries, a parameter dictionary refers to organizing query parameters in the form of key-value pairs. This is used to prevent SQL injection, improve readability, and reuse queries.

[0031] In this embodiment, step S2 includes the following specific steps: S21. Use NLP tools based on professional knowledge graphs to perform named entity recognition (NER) on the query task to obtain the corresponding query dimension indicators; for example, in medical knowledge related queries, extract the corresponding query dimensions and indicators based on the BioBERT model. The BioBERT model is a bidirectional encoder representation model for biomedical text mining, which is a domain-specific language representation model pre-trained based on a large-scale biomedical corpus. S22. Use the SUTime time library to identify time parameters in the query task; the SUTime time library is an open-source library for parsing time expressions in natural language. It can identify and process various time formats, including absolute time, relative time, time intervals, etc. The SUTime time library is developed based on Java. S23. Use dependency parsing to identify filtering conditions in query tasks; Dependency parsing (DP) is a language processing technique that reveals the syntactic structure by analyzing the dependency relationships between components within a language unit. The core idea of ​​this analysis method is to treat a sentence as a directed graph, where words are nodes and dependency relationships are edges. By constructing such a graph structure, the grammatical relationships between the words in the sentence can be clearly displayed. S24. Use a text classification model to identify the analysis method in the query task; In this step S24, text classification is one of the core tasks of NLP, which aims to automatically classify unstructured text data such as news, comments, emails, etc. into a predefined category system. Its essence is to capture the semantic features of the text through an algorithm model and map them to the target category label, thereby solving the problem of information overload and improving data utilization efficiency. The preferred text classification model in this embodiment is the FastText model. S25. Map the dimension indicators to be queried, time parameters, filtering conditions, and analysis methods to the corresponding parameter dictionaries. In step S25, the confidence level of the dimension indicators to be queried can be evaluated first, and then the mapping can be completed by constructing a table set, selecting fields, and performing multi-table joins to form a structured parameter dictionary. In step S2, steps S21, S22, S23, and S24 can be executed in parallel, or multiple structured text inputs for different query tasks can be processed.

[0032] S3. Input the parameter dictionary into the SQL generator to generate an SQL query statement.

[0033] In this embodiment, the specific process of step S3 includes: S31. Construct a complete table join topology based on the parameter dictionary to determine the JOIN relationship, and map the fields to be queried to the SQL syntax tree. In this step S31, when determining the JOIN relationship, Cartesian product risk detection is used to analyze whether the JOIN relationship may cause data explosion. If data explosion may occur, the entire step of generating SQL query statements ends. S32. Generate the corresponding SQL query statement based on the SQL syntax tree. In this step S32, if there are parameters that require special logical calculations, process their corresponding query logic first, and then generate the corresponding SQL query statement based on the database dialect conversion.

[0034] In step S32, if multiple query dimensions are involved, the SQL generator uses a decision tree based on the NIO (Synchronous Non-Blocking I / O) multiplexing mechanism to determine the relationship between the multiple query dimensions, and then selects the corresponding SQL query statement generation strategy according to the relationship between the multiple query dimensions. The SQL query statement generation strategies include single SQL join query, CTE join query, and parallel multi-SQL query. In step S3, before executing step S31, the following steps can be introduced sequentially in the following order: (1) Verify and complete the parameter dictionary: Check the completeness and rationality of the parameter content corresponding to the dimension indicators, time parameters, filtering conditions, and analysis methods in the parameter dictionary. If there are missing parameters and / or unreasonable parameters, use the preset default values ​​or manual settings to complete them. (2) Indicator verification: Based on the relevant professional knowledge graph, semantic analysis is performed on the indicators corresponding to each dimension to be investigated, and the correlation between the indicators and the data dimensions and whether the calculation logic of the indicators is correct are examined, thereby filtering out erroneous and invalid indicators.

[0035] In step S3, after generating the corresponding SQL query statement in step S32, the following steps can be performed: performance optimization and verification of the generated SQL query statement. The performance optimization includes predicate pushdown, redundancy elimination, index suggestions, splitting multiple subqueries, etc. When performing performance optimization, the EXPLAIN statement tool is used to evaluate the generated SQL query statement first. When performing verification, relevant professional knowledge information is used as constraints, and the Drools rule engine is used to verify the rationality of the generated SQL query statement in terms of professional knowledge to ensure the accuracy of the query.

[0036] In other implementations, after generating the SQL query statement in step S3, errors may occur in the query due to schema changes, boundary conditions, or other reasons in the database to be queried. Therefore, the generated SQL query statement can be further processed by the agent of hierarchical diagnosis and intelligent repair to optimize the final SQL query statement that can be input into the database to be queried.

[0037] Compared to existing technologies, the method for generating SQL query statements from natural language in this embodiment has the following advantages: Compared to existing technologies that use large language models for the entire SQL inference and generation process, this embodiment only uses the large language model in the stage of parsing the query intent of natural language. By using preset splitting rules, the large language model can output structured query task content. The large language model is completely removed when extracting multi-dimensional data and generating SQL query statements. The SQL generator processes the data through deterministic logic, and the generated SQL query statements can significantly improve the accuracy of the query under the constraint of professional knowledge. For the same natural language content, the generated SQL query statement results can be guaranteed to be stable each time, avoiding the probabilistic defects of the large language model. For the generation of SQL query statements containing multiple query dimensions, the NIO multiplexing mechanism is used to select the generation strategy, which can maintain the accuracy and will not reduce the performance of the SQL generator due to the increase in the number of query dimensions. The response time can also be kept at a high level.

[0038] The second embodiment of the present invention is described below.

[0039] Please refer to Figure 2 The system for generating SQL query statements from natural language in this embodiment includes a query task generation module, a rule base, an extraction module, and an SQL generator.

[0040] The rule base stores preset splitting rules, which are used to set the corresponding splitting rules based on the relevant professional knowledge graph.

[0041] In this embodiment, the splitting rule includes constraints, splitting modes, and task templates. Constraints include corresponding professional knowledge information; splitting modes include corresponding analysis content information; task templates include processing information for the corresponding query task. Constraints include one or more professional knowledge graphs. The analysis content information of the splitting mode may include rule ID, rule name, applicable scenarios, keyword list, triggering conditions (regular expression matching or keyword combination), similarity threshold, list of required fields, list of optional fields, verification rules, splitting strategy description, task execution order, dependencies between multiple query tasks, and query tasks that can be executed in parallel. The processing information of the task template includes the task name, processing method, associated MCP tool, verification method, etc., for each query task.

[0042] In this embodiment, the splitting rules are edited using the APL language, and there are multiple splitting rules preset based on the corresponding professional knowledge graph.

[0043] The query task generation module is used to generate query tasks after parsing user questions from the input natural language according to preset splitting rules.

[0044] In this embodiment, the query task generation module and the rule base jointly execute the following processes (a)-(d) sequentially: (a) The query task generation module uses a large language model to identify and parse the user's question described in natural language statements; (b) The large language model of the query task generation module retrieves a rule base containing multiple splitting rules under constraints to obtain one or more splitting rules that match the user's question; (c) The rule base outputs one or more corresponding splitting rules to the task self-arranging agent of the query task generation module; (d) The query task generation module uses a task self-arranging agent to generate structured text describing the query task based on the user's question and one or more corresponding splitting rules; The extraction module is used to extract multi-dimensional data from multiple query tasks to generate a parameter dictionary. The specific data dimensions are generated based on the corresponding professional knowledge graph. Different professional knowledge requires different data dimensions to be extracted, and each dimension has its own corresponding indicators.

[0045] In this embodiment, the extraction module first executes the following processes (e)-(h) in parallel, and then executes the process (i): (e) Use NLP tools based on professional knowledge graphs to perform named entity recognition on the query task to obtain the corresponding query dimension indicators; (f) Use the SUTime time library to identify time parameters in query tasks; (g) Use dependency parsing to identify filtering conditions in query tasks; (h) Use a text classification model to identify the analysis methods in query tasks; (i) Map the dimension indicators to be queried, time parameters, filtering conditions, and analysis methods to the corresponding parameter dictionaries respectively; in (i), the confidence level of the dimension indicators to be queried can be evaluated first, and then the mapping can be completed by constructing a table set, selecting fields, and performing multi-table associations to form a structured parameter dictionary; The SQL generator is used to generate SQL query statements based on the parameter dictionary.

[0046] In this embodiment, the SQL generator executes the following processes (j) and (k) sequentially: (j) Construct a complete table join topology based on the parameter dictionary to determine the JOIN relationship, and map the fields to be queried to the SQL syntax tree; when determining the JOIN relationship, use Cartesian product risk detection to analyze whether the JOIN relationship may cause data explosion. If data explosion may occur, end the entire process of generating SQL query statements. (k) Generate the corresponding SQL query statement based on the SQL syntax tree; if there are parameters that require special logical calculations, process their corresponding query logic first, and then generate the corresponding SQL query statement based on the database dialect conversion.

[0047] In (k) processing, if multiple query dimensions are involved, the SQL generator uses a decision tree based on the NIO multiplexing mechanism to determine the relationship between the multiple query dimensions, and then selects the corresponding SQL query statement generation strategy according to the relationship between the multiple query dimensions. The SQL query statement generation strategies include single SQL join query, CTE join query, and parallel multi-SQL query.

[0048] In this embodiment, before performing (j) processing, the parameter dictionary can be verified and completed. The completeness and rationality of the various parameter contents corresponding to the dimension indicators to be queried, time parameters, filtering conditions, and analysis methods in the parameter dictionary can be checked. If there are missing parameters and / or unreasonable parameters, the preset default values ​​or manual settings can be used to complete them. Indicator verification: Based on the corresponding professional knowledge graph, the indicators corresponding to each dimension to be queried are semantically parsed to examine the correlation between the indicators and the data dimensions and whether the calculation logic of the indicators is correct, thereby filtering out erroneous and invalid indicators.

[0049] In this embodiment, after the (k) processing is completed, the generated SQL query statement can also be: optimized and verified. The performance optimization includes predicate pushdown, redundancy elimination, index suggestions, splitting multiple subqueries, etc. When optimizing the performance, the EXPLAIN statement tool is used to evaluate the generated SQL query statement first. When verifying, the corresponding professional knowledge information is used as a constraint condition, and the Drools rule engine is used to verify the rationality of the generated SQL query statement in terms of professional knowledge to ensure the accuracy of the query.

[0050] In other implementations, after generating the SQL query statement, errors may occur in the query due to schema changes, boundary conditions, or other reasons in the database to be queried. Therefore, an intelligent agent based on hierarchical diagnosis and intelligent repair can be introduced to process the generated SQL query statement, thereby optimizing the final SQL query statement that can be input into the database to be queried.

[0051] Compared to existing technologies, the system in this embodiment that generates SQL query statements from natural language has the following advantages: Compared to existing technologies that use large language models for the entire SQL inference and generation process, this embodiment only uses the large language model in the stage of parsing the query intent of natural language. By using preset splitting rules, the large language model can output structured query task content. The large language model is completely removed when extracting multi-dimensional data and generating SQL query statements. The SQL generator processes the data through deterministic logic, and the generated SQL query statements can significantly improve the accuracy of the query under the constraint of professional knowledge. For the same natural language content, the generated SQL query statement results can be guaranteed to be stable each time, avoiding the probabilistic defects of the large language model. For the generation of SQL query statements containing multiple query dimensions, the NIO multiplexing mechanism is used to select the generation strategy, which can maintain the accuracy and will not reduce the performance of the SQL generator due to the increase in the number of query dimensions. The response time can also be kept at a high level.

[0052] The third embodiment of the present invention is described below.

[0053] Please refer to Figure 3 This embodiment provides an application of the method for generating SQL query statements from natural language in the first embodiment to medical case query, wherein the preset splitting rules are set according to medical professional knowledge, and the query task is a medical case query task.

[0054] The application of this embodiment, after generating the SQL query statement in the first embodiment, also includes the following steps: Input the generated SQL query into the medical case knowledge base to retrieve the corresponding case results; Add the case results and corresponding user questions to the medical case knowledge base and allow the large language model to learn continuously. When performing a new round of case queries on the medical case knowledge base, after the large language model identifies and parses the user's question in natural language, it first checks whether there is a user question in the medical case knowledge base with a similarity greater than or equal to 95%. If there is, it directly outputs the case result corresponding to the user question. If not, it executes steps S1 to S3 of the method for generating SQL query statements from natural language in the first embodiment, and then continues to input the generated SQL query statement into the medical case knowledge base to retrieve the corresponding case result.

[0055] In this preferred embodiment, the medical case knowledge base simultaneously receives and associates the input and output data from intermediate steps such as natural language input, query tasks, and parameter dictionaries when generating corresponding SQL query statements, and provides them to the large language model for continuous learning.

[0056] Compared with the prior art, this embodiment can continuously update and learn the medical case knowledge base. After a sufficient number of medical cases are queried, it can quickly respond to improve query efficiency and the quality of medical case retrieval.

[0057] The fourth embodiment of the present invention is described below.

[0058] This embodiment provides a computer device, which includes a processor and a memory; the memory stores a computer program that can be executed by the processor; when the computer program is executed by the processor, it implements the method of generating SQL query statements from natural language in the first embodiment.

[0059] The fifth embodiment of the present invention is described below.

[0060] This embodiment provides a storage medium storing a computer program; the computer program is used to cause a computer device to execute the method of generating SQL query statements from natural language according to the first embodiment.

[0061] It should be understood that the various forms of processes shown above can be used to rearrange, add, or delete steps. For example, the steps described in this disclosure can be executed in parallel, sequentially, or in different orders, as long as the desired result of the technical solution disclosed in this disclosure can be achieved, and this is not limited herein.

[0062] The specific embodiments described above do not constitute a limitation on the scope of protection of this disclosure. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the principles of this disclosure should be included within the scope of protection of this disclosure.

Claims

1. A method for generating SQL query statements from natural language, characterized in that, Including the following steps: The query task is generated after parsing the user's question from the input natural language according to the preset splitting rules; Extract multi-dimensional data from the query task to generate a parameter dictionary; Input the parameter dictionary into the SQL generator to generate an SQL query statement.

2. The method for generating SQL query statements from natural language according to claim 1, characterized in that, The splitting rules include constraints, splitting modes, and task templates; The constraints include relevant professional knowledge information; The splitting mode includes corresponding analysis content information; The task template includes the processing information for the corresponding query task.

3. The method for generating SQL query statements from natural language according to claim 2, characterized in that, The step of generating a query task after parsing the user's question from the input natural language according to preset splitting rules includes: Use large language models to identify and parse user questions described in natural language; The large language model retrieves a rule base containing multiple splitting rules under constraints to obtain one or more splitting rules that match the user's question; The rule base outputs one or more splitting rules to the task self-arranging agent; The task self-arranging agent uses the user question and one or more corresponding splitting rules to form a structured text describing the query task.

4. The method for generating SQL query statements from natural language according to claim 1, characterized in that, The steps involved in extracting multi-dimensional data from a query task to generate a parameter dictionary include: Using NLP tools based on professional knowledge graphs, named entity recognition is performed on the query task to obtain the corresponding query dimension indicators; Use the SUTime time library to identify time parameters in query tasks; Dependency parsing is used to identify filtering conditions in query tasks; Use text classification models to identify the analysis methods in query tasks; The dimension indicators to be queried, the time parameters, the filtering conditions, and the analysis methods are each mapped to a corresponding parameter dictionary.

5. The method for generating SQL query statements from natural language according to claim 4, characterized in that, The step of inputting the parameter dictionary into the SQL generator to generate the SQL query statement includes: Construct a complete table join topology based on the parameter dictionary to determine the JOIN relationship, and map the fields to be queried to the SQL syntax tree; Generate the corresponding SQL query statement based on the SQL syntax tree.

6. The method for generating SQL query statements from natural language according to claim 5, characterized in that, When performing database dialect conversion to generate corresponding SQL query statements, if multiple query dimensions are involved, the SQL generator uses a decision tree based on the NIO multiplexing mechanism to determine the relationship between the multiple query dimensions, and then selects the corresponding SQL query statement generation strategy based on the relationship between the multiple query dimensions. The SQL query generation strategies include single SQL join query, CTE join query, and parallel multi-SQL query.

7. A system for generating SQL query statements from natural language, characterized in that, Includes a query task generation module, a rule base, an extraction module, and an SQL generator; The rule base stores preset splitting rules; The query task generation module is used to generate query tasks after parsing user questions from the input natural language according to preset splitting rules. The extraction module is used to extract multi-dimensional data from the query task to generate a parameter dictionary; The SQL generator is used to generate SQL query statements based on the parameter dictionary.

8. The application of the method for generating SQL query statements from natural language as described in any one of claims 1-6 in medical case queries, characterized in that, The preset splitting rules are set based on medical knowledge; the query task is a medical case query task; it also includes the following steps: Input the generated SQL query into the medical case knowledge base to retrieve the corresponding case results; The case results and corresponding user questions are added to the medical case knowledge base and the large language model is continuously learned. When performing a new round of case queries on the medical case knowledge base, the user's question in natural language is first parsed, and then the question is searched for similar user questions in the medical case knowledge base. If similar user questions are found, the case results corresponding to the user questions are directly output. If not, the method of generating SQL query statements from natural language as described in any one of claims 1-6 is executed.

9. A computer device, characterized in that, The computer device includes a processor and a memory; the memory stores a computer program that can be executed by the processor; when the computer program is executed by the processor, it implements the method of generating SQL query statements from natural language according to any one of claims 1-6.

10. A storage medium, characterized in that, The computer program is stored therein; the computer program is used to cause a computer device to execute the method of generating SQL query statements from natural language according to any one of claims 1-6.