---
title: "Sitecore CDPでSQLクエリを書きます"
slug: "write-an-sql-query-in-sitecore-cdp"
abstract: "セグメントを作成するためのSQLクエリの書き方(Sitecore CDP)。"
---
# Sitecore CDPでSQLクエリを書きます

SQLを知り、Sitecore CDPのデータモデルに慣れていれば、複雑なSQLクエリを書くために上級モードに切り替えることができます。回帰論理、四分位数、その他の高度なセグメンテーション機能を適用したい場合は、上級モードへの切り替えをお勧めします。

> **\[!注\]始める前に**[、SQLクエリをアドバンスドモードで書く前に、基本モードでセグメントを作成してください。](https://doc.sitecore.com/cdp/ja/users/sitecore-cdp/introduction-to-batch-segmentation/using-advanced-mode-in-batch-segmentation-in-sitecore-cdp/create-and-add-rules-to-a-segment-in-sitecore-cdp.html "Create and add rules to a segment in Sitecore CDP")

SQLクエリを書いてセグメントを作成するには:

1.  **セグメントビルダーの右下にあるセグメントルール****の下、「アドバンスドモード」をクリックしてください**
    
2.  確認ウィンドウで「**はい、切り替え**」をクリックします。**SQLエディタ** が表示されます。Sitecore CDPは、基本モードで作成したセグメントをSQL文に変換し、さらに編集できます。
    
    > !注SQLクエリを書く際には、SQLエディタで[SQLクエリを書く際のルール](https://doc.sitecore.com/cdp/ja/users/sitecore-cdp/introduction-to-batch-segmentation/using-advanced-mode-in-batch-segmentation-in-sitecore-cdp/rules-for-writing-sql-queries-in-the-sql-editor-in-sitecore-cdp.html "Rules for writing SQL queries in the SQL editor in Sitecore CDP")に従わなければなりません。
    > 
    > SQLエディタを可能にするクエリサービスであるAmazon Athenaには、複雑さやリソース使用に [クエリの制限](https://docs.aws.amazon.com/athena/latest/ug/service-limits.html) があります。実行失敗を避けるために、可能な限りクエリを簡素化しましょう。
    
3.  値やフィールドをコピー&ペーストするには、**Data**タブでエンティティブロックの矢印![cdp\_seg\_downarrow.png](https://doc.sitecore.com/api/markdown-image/cdp/ja/users/sitecore-cdp/images/cdp_seg_downarrow.png)アイコンをクリックすると、SQLクエリに含められる利用可能なフィールドや値が表示されます。フィールドまたは値の横にある ![Copy\_SQL.png](https://doc.sitecore.com/api/markdown-image/cdp/ja/users/sitecore-cdp/images/Copy_SQL.png) をクリックし、SQLエディタに貼り付けます。
    
    ![Viewing fields and values on the Data tab in the SQL editor.](https://doc.sitecore.com/api/markdown-image/cdp/ja/users/sitecore-cdp/images/Segment_builder_advanced.png)
    
4.  コードスニペットをコピーするには、ス**ニペット**タブでエンティティブロックをクリックし、コピーしたいコードの隣にある**コピー**リンクをクリックし、そのコードをSQLエディタに貼り付けてください。
    
5.  **「保存**」をクリックします。アドバンスモードでセグメントを保存した後は、基本モードに戻すことはできません。
    
6.  セグメントのオーディエンスサイズを計算し、ゲストサンプルを見るには、「[**Calculated audience**」タブにアクセスする](urn:resource:component:1574985/section-idm2348128760626)か[セグメントビルダーで計算してください](urn:resource:component:1574985/section-idm1703481287675516)。
    
7.  [セグメントを組織の夜間セグメントビルドに組み込むようにスケジュール](https://doc.sitecore.com/cdp/ja/users/sitecore-cdp/introduction-to-batch-segmentation/managing-segment-status-in-sitecore-cdp/schedule-a-segment-in-sitecore-cdp.html "Schedule a segment in Sitecore CDP")[してください。Sitecore CDP内でセグメントを使用するには、これを行う必要があります。](https://doc.sitecore.com/cdp/ja/users/sitecore-cdp/introduction-to-batch-segmentation/using-segments-within-sitecore-cdp.html "Using segments within Sitecore CDP")