You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
summary: Redis Data Integration keeps Redis in sync with the primary database in near
14
+
real time.
15
+
type: integration
16
+
weight: 30
17
+
---
18
+
19
+
The `CASE` statement allows you to specify conditions and return different values based on those conditions. You can use it both to create new fields or filter existing data.
20
+
21
+
## Using SQL CASE to create a new field
22
+
The example below demonstrates how to use the `CASE` statement to create a new field called `Market` based on the value of the `BillingCountry` field in the `Invoice` table. The new field categorizes countries into regions such as "North America" and "Europe".
23
+
24
+
```yaml
25
+
source:
26
+
table: Invoice
27
+
28
+
transform:
29
+
- uses: add_field
30
+
with:
31
+
field: "Market"
32
+
language: sql
33
+
expression: |
34
+
CASE
35
+
WHEN BillingCountry = 'USA' THEN 'North America'
36
+
WHEN BillingCountry = 'Canada' THEN 'North America'
37
+
WHEN BillingCountry = 'UK' THEN 'Europe'
38
+
WHEN BillingCountry = 'France' THEN 'Europe'
39
+
ELSE 'Other'
40
+
END
41
+
```
42
+
43
+
## Using SQL CASE to filter data
44
+
45
+
You can also use the `CASE` statement to filter data based on specific conditions. The example below demonstrates how to filter the `Invoice` table to include only invoices from the USA and Canada, with total above country specific thresholds.
46
+
47
+
Due to the `Total` field being a Decimal in the source table, it represented as string in Debezium and needs to be cast to `REAL` type for comparison in the `CASE` statement.
48
+
Not casting it will result in an incorrect comparison results and incorrect filtering.
49
+
50
+
```yaml
51
+
source:
52
+
table: Invoice
53
+
54
+
transform:
55
+
- uses: filter
56
+
with:
57
+
language: sql
58
+
expression: |
59
+
CASE
60
+
WHEN BillingCountry = 'USA' AND CAST(Total AS REAL) > 11.99 THEN True
61
+
WHEN BillingCountry = 'Canada' AND CAST(Total AS REAL) > 9.99 THEN True
0 commit comments