Pages

Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Saturday, October 17, 2026

Data 360: Calculated Insight using SQL for Segment

For Calculated Insights created using SQL (not Visual Builder), there is no "Show in Builder" option under the dropdown menu. 

If you edit the SQL and save the Calculated Insight, you must republish the Calculated Insight to get the new result. Also, you can't edit the SQL if there is a structural change, such as changing or removing an output field; the recommended solution is to create a new CI.

What is the SQL requirement in a Calculated Insight?

Syntax
SELECT <Attributes>, <Aggregation[_Measures_]>.
FROM <Data Model Object>
JOIN [Inner | Left | Right | Full] <Data Model Object> [Optional]
WHERE <predicate on rows> [Optional]
GROUP BY <columns[_Dimensions_]>

1. Must include a Profile Key
The output must contain a key that can be related back to the segment population, depending on whether your segment is built on Unified Individual or Individual.

2. Must include at least one aggregated measure
Calculated Insights require aggregation such as: COUNT(), SUM(), MAX(), MIN(), AVG().

3. Must have a GROUP BY

Sample SQL:
SELECT 
UnifiedIndividual__dlm.ssot__Id__c AS unifiedindividual_id__c, 
UnifiedIndividual__dlm.ssot__FirstName__c AS first_name__c, 
UnifiedIndividual__dlm.ssot__LastName__c AS last_name__c, 
ssot__EmailEngagement__dlm.ssot__EngagementChannelActionId__c AS channel__c,
COUNT(ssot__EmailEngagement__dlm.ssot__EmailMessageId__c) AS count_of_email_engagement__c, 
MIN(ssot__EmailEngagement__dlm.ssot__EngagementDateTm__c) AS first_date__c,
MAX(ssot__EmailEngagement__dlm.ssot__EngagementDateTm__c) AS last_date__c  
FROM ssot__EmailEngagement__dlm 
JOIN ssot__Individual__dlm ON (ssot__EmailEngagement__dlm.ssot__IndividualId__c = ssot__Individual__dlm.ssot__Id__c) 
JOIN IndividualIdentityLink__dlm ON (ssot__Individual__dlm.ssot__Id__c = IndividualIdentityLink__dlm.SourceRecordId__c) 
JOIN UnifiedIndividual__dlm ON (IndividualIdentityLink__dlm.UnifiedRecordId__c = UnifiedIndividual__dlm.ssot__Id__c) 
WHERE (ssot__EmailEngagement__dlm.ssot__EngagementDateTm__c >= date_sub(current_date(), 7)) 
GROUP BY unifiedindividual_id__c, first_name__c, last_name__c, channel__c

Sample data from the above query to show how the JOIN works:
ssot__EmailEngagement__dlm.ssot__IndividualId__c = 003xxx or 00Qxxx
ssot__Individual__dlm.ssot__Id__c = 003xxx or 00Qxxx
IndividualIdentityLink__dlm.SourceRecordId__c = 003xxx or 00Qxxx
IndividualIdentityLink__dlm.UnifiedRecordId__c = c434453739ba80ff0cee6df928b19cde 
UnifiedIndividual__dlm.ssot__Id__c = c434453739ba80ff0cee6df928b19cde


In the Segment

Create a segment, type the CI name under the Search Attributes box, and use the aggregated "measure" attribute in the Include tab.


Can I run SQL from Calculated Insight in Query Editor?

Calculated Insight SQL and Query Editor SQL are similar, but there are some differences;

1.  When filtering by date field
In Query Editor: 
WHERE (ssot__EmailEngagement__dlm.ssot__EngagementDateTm__c >= DATE_ADD('day', -7, CURRENT_DATE)) 
OR 
WHERE (ssot__EmailEngagement__dlm.ssot__EngagementDateTm__c >= CURRENT_DATE - INTERVAL '7' DAY) 

In Calculated Insight: 
WHERE (ssot__EmailEngagement__dlm.ssot__EngagementDateTm__c >= date_sub(current_date(), 7)) 


2. Attribute name must end with __c in CI
In Query Editor
SELECT UnifiedIndividual__dlm.ssot__FirstName__c AS first_name

In Calculated Insight: 
SELECT UnifiedIndividual__dlm.ssot__FirstName__c AS first_name__c


3. The ORDER BY clause is not supported in CI


Wednesday, October 14, 2026

Data 360: Query Syntax

1. Object or Field Name
Use " " or nothing.
SELECT * FROM EmailActivity_0Uvxxxx__dll WHERE "eventType__c" <> 'OPEN' LIMIT 10

2. String match - Text field
Use ' ' and case-sensitive
SELECT * FROM EmailActivity_0Uvxxxx__dll WHERE "eventType__c" <> 'OPEN' LIMIT 10

3. Filter on Boolean field
SELECT * FROM ssot__Individual__dlm  WHERE "Is_FieldName__c" IS NOT TRUE LIMIT 10
OR
SELECT * FROM ssot__Individual__dlm  WHERE "Is_FieldName__c" IS TRUE LIMIT 10

4. Filter on Text field
SELECT * FROM "ssot__Account__dlm" WHERE "Customer__c" <> '' LIMIT 10
OR
SELECT * FROM "ssot__Account__dlm" WHERE "Customer__c" = '' LIMIT 10

5. Filter on DateTime field
SELECT
    l."Status__c" AS Lead_Status,
    COUNT(DISTINCT l."Id__c") AS Total_Leads,
    COUNT(
        DISTINCT CASE
            WHEN e."createdAt__c" >= TIMESTAMP '2026-05-01 00:00:00'
             AND e."createdAt__c" <= TIMESTAMP '2026-05-31 23:59:59'
             AND e."sendStatus__c" = 'SENT'
            THEN l."Id__c"
        END
    ) AS Contacted_May_2026
FROM "Lead_Home__dll" l
LEFT JOIN "EmailActivity_0Uvxxxx__dll" e
    ON l."Id__c" = e."prospectSalesforceId__c"
GROUP BY Lead_Status
ORDER BY Lead_Status;




Referral:

Page-level ad