Abstract
In this paper, we introduce EHR-SeqSQL, a novel sequential text-to-SQL dataset for Electronic Health Record (EHR) databases. EHR-SeqSQL is designed to address critical yet underexplored aspects in text-to-SQL parsing: interactivity, compositionality, and efficiency. To the best of our knowledge, EHR-SeqSQL is not only the largest but also the first medical text-to-SQL dataset benchmark to include sequential and contextual questions. We provide a data split and the new test set designed to assess compositional generalization ability. Our experiments demonstrate the superiority of a multi-turn approach over a single-turn approach in learning compositionality. Additionally, our dataset integrates specially crafted tokens into SQL queries to improve execution efficiency. With EHR-SeqSQL, we aim to bridge the gap between practical needs and academic research in the text-to-SQL domain.
Introduction
Electronic Health Records (EHRs) are massive, complex relational databases containing vital patient information. While text-to-SQL systems allow non-experts, such as clinicians, to query these databases using natural language, existing datasets are largely "single-turn." They assume a user asks one complex question and receives one answer in isolation.
In practice, medical data exploration is inherently interactive. A clinician might ask, "Which patients have diabetes?" followed by, "Which of those patients were prescribed Metformin?" Existing models struggle with this sequential context. Furthermore, complex medical queries often involve deeply nested SQL structures that are computationally expensive and difficult for AI models to generate accurately from scratch.
Methodology
The researchers developed a three-stage pipeline to transform the existing EHRSQL dataset into a sequential format:
- SQL Decomposition: Complex, nested SQL queries were broken down into a sequence of simpler subqueries. This occurred at both the "nested level" (separating inner and outer queries) and the "clause level" (isolating WHERE, ORDER BY, and aggregation functions).
- Subquery Merging (BPE): To ensure questions felt natural, a Byte Pair Encoding (BPE) algorithm identified and merged frequently occurring subquery patterns that medical experts naturally group together.
- Natural Language Generation: Templates were created for these subqueries, and ChatGPT was utilized to paraphrase them into diverse, natural-sounding medical questions.
Crucially, the dataset introduces Special Context Tokens: PREV_QUERY and PREV_RESULT. Instead of re-generating a massive nested query at every turn, the model can refer to the output of a previous step, acting as a form of "query memory."
Dataset Characteristics
EHR-SeqSQL is a large-scale benchmark featuring 9,195 interactions and 31,669 turns based on the MIMIC-III clinical database. Compared to general-domain multi-turn datasets like ATIS, SParC, and CoSQL, EHR-SeqSQL offers a significantly higher number of turns and focuses specifically on the complex medical domain. It includes a specialized "Compositional Split" to test a model's ability to handle unseen complex query structures by combining known sub-parts.
Experimental Results
The study found that training on sequential steps significantly aids compositional generalization. By learning the components of a query in a multi-turn setting, models became much better at answering complex, unseen questions.
Key Findings:
- Accuracy Boost: On the Compositional Split, T5-Base's performance jumped from 52.15% in a single-turn setting to 82.49% in the multi-turn EHR-SeqSQL setting.
- Execution Efficiency: The introduction of
PREV_RESULTtokens reduced average execution time by 18%. For the most complex queries in the largest test database, execution time was slashed by up to 81%. - Model Scaling: Interestingly, smaller models like T5-Base often outperformed larger counterparts like T5-3B in compositional tests, suggesting that task-specific fine-tuning on sequential data is more critical than raw parameter count.
- Robustness: Models successfully generalized to interactions of up to 14 turns, despite being trained on an average of 3.5 turns.
Discussion and Future Work
While EHR-SeqSQL marks a significant advancement, it currently focuses on structured relational data and excludes ambiguous or unanswerable questions common in clinical settings. Future work aims to incorporate unstructured clinical notes and extend the dataset to other databases like eICU to ensure broader clinical applicability. By bridging the gap between academic research and practical clinical needs, EHR-SeqSQL provides a robust foundation for the next generation of interactive medical AI.
Summary updated