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