Возвратившиеся.txt
| 1 |
CREATE OR REPLACE VIEW public.returning_clients |
|---|---|
| 2 |
AS SELECT sf.year_month, |
| 3 |
sf.clientaltkey, |
| 4 |
sf.group_format, |
| 5 |
sf.region, |
| 6 |
sf.format, |
| 7 |
sf.revenue, |
| 8 |
sf1.year_month_before, |
| 9 |
sf1.group_format_before, |
| 10 |
news.mark |
| 11 |
FROM segmentation_formats sf |
| 12 |
LEFT JOIN ( SELECT DISTINCT segmentation_formats.year_month AS year_month_before, |
| 13 |
segmentation_formats.clientaltkey, |
| 14 |
segmentation_formats.group_format AS group_format_before |
| 15 |
FROM segmentation_formats) sf1 ON sf.clientaltkey::text = sf1.clientaltkey::text AND sf.year_month = (sf1.year_month_before + '1 mon'::interval) |
| 16 |
LEFT JOIN ( SELECT make_date(n.year::integer, n.month::integer, 1) AS date_new, |
| 17 |
n.clientaltkey, |
| 18 |
1 AS mark |
| 19 |
FROM newclients n) news ON news.date_new = sf.year_month AND news.clientaltkey::text = sf.clientaltkey::text |
| 20 |
WHERE sf.year_month >= '2025-01-01'::date; |