次の方法で共有


クイック スタート: スマート クエリ ビルダーを使用する

このクイック スタートでは、クエリ作成アシスタントを使用して、生の SQL または任意の ORM を使用して、効率的で正確で安全なクエリを作成する方法について説明します。 コード優先の開発者とデータ優先の開発者の両方向けに設計されており、接続されたデータベース スキーマに合わせて運用対応のロジックをより迅速に生成できます。

概要

データベースに接続されていて、MSSQL 拡張機能でアクティブなエディター ウィンドウが開かれていることを確認します。 この接続により、 @mssql チャット参加者はデータベース環境のコンテキストを理解できるため、正確でコンテキストに対応した提案が可能になります。 データベース接続がないと、チャット参加者は意味のある応答を提供するスキーマまたはデータ コンテキストを持ちません。

次の例では、 AdventureWorksLT2022 サンプル データベースを使用します。このデータベースは、 Microsoft SQL Server サンプルとコミュニティ プロジェクト のホーム ページからダウンロードできます。

最適な結果を得るには、独自の環境に合わせてテーブル名とスキーマ名を調整します。

チャットに @mssql プレフィックスが含まれていることを確認します。 たとえば、「 @mssql 」と入力し、その後に質問またはプロンプトを入力します。 これにより、チャット参加者は、SQL 関連のサポートを求めていることを理解できます。

クエリの作成

GitHub Copilot は、Visual Studio Code 内で直接インテリジェントなクエリ構築をサポートします。 基本的な SELECT から複雑な結合、フィルター、集計まで、ベスト プラクティスに従って現在のスキーマを反映する SQL または ORM クエリが生成されるため、アプリケーション ロジックに集中できます。

チャット参加者から質問できる一般的なユース ケースと例を次に示します。

時間ベースの分析

これらのプロンプトは、最近の販売活動、期間別のトップ パフォーマー、過去の平均との比較など、時間の経過に伴う傾向を分析するのに役立ちます。 GitHub Copilot では、現在のシステム日付に基づく想定を回避して、データの最新の日付に対して値を計算するクエリを作成できます。

過去 6 か月間の平均以上の販売注文の返品リスト

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).

年別にグループ化された上位 3 人の顧客を返す

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).

過去 1 年間の顧客と注文を返す

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.

売上に基づいて上位10人の顧客を提供する

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.

返品率に基づいて上位 5 つの製品を返す

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 の応答の強化に関するアイデアを共有します。