共用方式為


快速入門:使用智慧查詢建構器

在這份快速入門中,你將學習查詢建置助理如何幫助你使用原始 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 回應的想法。