快速入门:使用智能查询生成器

在本快速入门中,你将了解查询生成助手如何帮助你使用原始 SQL 或首选 ORM 创建高效、准确和安全查询。 它专为代码优先和数据优先开发人员设计,可更快地生成与连接的数据库架构一致的生产就绪逻辑。

开始

确保已连接到数据库,并使用 MSSQL 扩展打开活动编辑器窗口。 通过此连接, @mssql 聊天参与者可以了解数据库环境的上下文,从而提供准确的上下文感知建议。 如果没有数据库连接,聊天参与者将没有架构或数据上下文来提供有意义的响应。

以下示例使用 AdventureWorksLT2022 示例数据库,可以从 Microsoft SQL Server 示例和社区项目 主页下载该数据库。

为了获得最佳结果,请调整表和架构名称以匹配自己的环境。

确保聊天包含 @mssql 前缀。 例如,键入 @mssql 后跟问题或提示。 这可确保聊天参与者了解你请求与 SQL 相关的帮助。

查询生成

GitHub Copilot 直接在 Visual Studio Code 中支持智能查询构造。 从基本SELECT 到复杂的联接、筛选器和聚合,它生成遵循最佳做法并反映当前架构的 SQL 或 ORM 查询,以便你可以专注于应用程序逻辑。

下面是通过聊天参与者可以询问的常见用例和示例:

基于时间的分析

这些提示有助于分析随时间推移的趋势,例如最近的销售活动、按周期排名最高的表现者,或与历史平均值的比较。 GitHub Copilot 可以生成查询来计算相对于数据最近日期的值,从而避免基于当前系统日期的假设。

返回过去六个月的高于平均销售订单的列表

Generate a nested query to fetch orders from `SalesLT.SalesOrderHeader` where the total is above the average order amount for the last six months, relative to the most recent order date in the database (not relative to the current date).

返回按年份分组的前三位客户

Write a query to find the top three customers by total sales in the `SalesLT.SalesOrderHeader` table, grouped by year.

在过去 30 天内返回每个客户的总收入

Find the total revenue for each customer in `SalesLT.Customer` who has placed orders in the last 30 days, relative to the most recent order date in `SalesLT.SalesOrderHeader` (not relative to the current date).

去年回头客和订单

Create a Sequelize query to fetch `Customers` (`SalesLT.Customers`) along with their orders (`SalesLT.SalesOrderDetail`) and total revenue, sorted by descending revenue during the last year in the database (not relative to the current date).

复杂关系

使用这些提示生成跨多个相关表的查询。 无论你是在将客户数据与订单详细信息合并,还是在生成收入聚合,GitHub Copilot 都能够利用架构上下文来导航复杂的关系,从而生成准确的合并和条件。

返回高于平均总数的订单列表

Using the actual schema of the `SalesLT.SalesOrderHeader` table, generate a nested SQL query that retrieves orders where the order total is above the average order total for the last six months. The six-month period should be calculated relative to the most recent order date in the table (not the current date).

返回按收入排序的客户

Using my current database, create a SQLAlchemy query to fetch customers along with their orders and total revenue, sorted by descending revenue.

生成每个客户总收入的查询

Using Prisma, generate a query that joins `SalesLT.Customer`, `SalesLT.SalesOrderHeader`, and `SalesLT.SalesOrderDetail` and calculates total revenue per customer.

按销售数据返回前十名客户

In Entity Framework, write a LINQ query that returns the top 10 customers by sales in the past year using the `SalesLT` schema.

相对于近期销售,退回未售出的产品

Write a TypeORM query that finds products that haven't been sold in the last six months. The six-month period should be calculated relative to the most recent order date in the table (not the current date).

根据总支出检索客户

Write a Django ORM query that retrieves all customers who have made purchases in the last year, sorted by total spending. The "last year" period should be calculated relative to the most recent order date in the table (not the current date).

业务见解

这些提示旨在从数据中呈现可作的见解。 从识别流失风险客户到查找未售出的产品,GitHub Copilot 可以帮助构建逻辑,这些逻辑支持根据连接的数据库定制的战略决策和报告。

识别新客户

Using my current database, generate a list that shows which customers have placed their first order in the last six months, using the most recent order date in the database as the reference point.

识别没有最近销售的产品

Using my current database, generate a list that identifies products that haven't been sold in the last 12 months, using the most recent order date in the database as the reference.

识别没有最近购买的高价值客户

Identify customers who have placed more than five orders but none in the last 90 days, using the most recent order date in the database as reference.

根据返回率返回前五个产品

List the top five products with the highest return rate based on order returns or cancellations, calculated relative to the most recent order date.

生成每月收入趋势数据

Generate a trend of monthly revenue over the last 12 months based on `OrderDate` in `SalesLT.SalesOrderHeader`, using the most recent order date as the anchor.

创建下降的订单频率报告

Using SQLAlchemy and Pandas, create a report that identifies customers with declining order frequency over the last three quarters based on the most recent order date.

共享您的体验

为了帮助我们优化和改进 MSSQL 扩展的 GitHub Copilot,请使用以下 GitHub 问题模板提交反馈: GitHub Copilot 反馈

提交反馈时,请考虑包括:

  • 测试方案 – 告诉我们你关注哪些领域,例如架构创建、查询生成、安全性、本地化。

  • 效果良好 - 描述任何感觉流畅、有用或超出预期的体验。

  • 问题或漏洞 – 包括任何问题、不一致或令人困惑的行为。 屏幕截图或屏幕录制特别有用。

  • 改进建议 - 分享改进可用性、扩大覆盖范围或增强 GitHub Copilot 响应的想法。