我编写了下面的脚本,并且只想在activity_value有多个条目的情况下输出最早的日期。为了达到这个目的,我需要对我的脚本做些什么呢?
SELECT
cl.contact_number as contact_number,
c.label_name as label_name,
cc.activity_value as activity_value,
cc.activity as activity,
enquiry_source=case
WHEN cc.activity_value in ('P','C','E','A','I')AND cc.activity = 'LEGACY' then s.source_desc
else NULL
end,
enquiry_date=case
WHEN cc.activity_value = 'E' AND cc.activity = 'LEGACY' then cc.valid_from else NULL
end,
days_to_intender=case
WHEN cc.activity_value = 'I' AND cc.activity = 'LEGACY' then cc.valid_from else NULL
end,
intender_date=case
WHEN cc.activity_value = 'I' AND cc.activity = 'LEGACY' then cc.valid_from else NULL
end,
days_to_pledge=case
WHEN cc.activity_value = 'P' AND cc.activity = 'LEGACY' then cc.valid_from else NULL
end,
pledge_date=case
WHEN cc.activity_value = 'P' AND cc.activity = 'LEGACY' then cc.valid_from else NULL
end
FROM
contact_legacies cl
INNER JOIN contacts c ON cl.contact_number = c.contact_number
INNER JOIN contact_categories cc on cl.contact_number = cc.contact_number
INNER JOIN sources s on cl.source =s.source
WHERE
cc.activity_value in ('P','C','E','A','I')AND
cc.activity = 'LEGACY'
GROUP BY
cl.contact_number,c.label_name,cc.activity_value,cc.activity,s.source_desc,cc.valid_from
ORDER BY
cl.contact_number,c.label_name,cc.activity_value,cc.activity,s.source_desc,cc.valid_from
示例:
case语句days_to_pledge和pledge_date输出两行,如下所示。如何让它们只输出最早的日期,因为它们的活动值都是P?
label_name activity_value days_to_pledge pledge_date
Ms D Lom P 2007-10-10 00:00:00.000 2007-10-10 00:00:00.000
Ms D Lom P 2010-02-11 00:00:00.000 2010-02-11 00:00:00.000
提前致谢。
... UPDATE
我刚刚意识到activity_value列可以包含值的组合而不仅仅是'P'。我认为我的目标是输出最早的cc.valid_from日期,其中有多个activity_value。
因此,对于下面的输出,我想要看到的唯一结果是具有活动值I的行,因为它具有在intender_date中显示的所有条目的最早日期。
答案 0 :(得分:0)
以下查询使用Serpiton评论中的逻辑。找到每个活动值的最小承诺日期,然后加入以仅列出具有这些承诺日期的行:
修改强>:
已修改查询以删除内联视图中的activity_value
,以便在所有activity_value
中获取最早的日期。
SELECT
cl.contact_number as contact_number,
c.label_name as label_name,
cc.activity_value as activity_value,
cc.activity as activity,
enquiry_source=case
WHEN cc.activity_value in ('P','C','E','A','I')AND cc.activity = 'LEGACY' then s.source_desc
else NULL
end,
enquiry_date=case
WHEN cc.activity_value = 'E' AND cc.activity = 'LEGACY' then cc.valid_from else NULL
end,
days_to_intender=case
WHEN cc.activity_value = 'I' AND cc.activity = 'LEGACY' then cc.valid_from else NULL
end,
intender_date=case
WHEN cc.activity_value = 'I' AND cc.activity = 'LEGACY' then cc.valid_from else NULL
end,
days_to_pledge=case
WHEN cc.activity_value = 'P' AND cc.activity = 'LEGACY' then cc.valid_from else NULL
end,
min_pledge_dates.pledge_date AS pledge_date
FROM
contact_legacies cl
INNER JOIN contacts c ON cl.contact_number = c.contact_number
INNER JOIN contact_categories cc on cl.contact_number = cc.contact_number
inner join sources s on cl.source =s.source
inner join
(select
-- cc.activity_value as activity_value,
min(case when cc.activity_value = 'P' and cc.activity = 'LEGACY' then cc.valid_from else null
end) pledge_date
FROM
contact_legacies cl
INNER JOIN contacts c ON cl.contact_number = c.contact_number
INNER JOIN contact_categories cc on cl.contact_number = cc.contact_number
INNER JOIN sources s on cl.source =s.source
WHERE
cc.activity_value IN ('P','C','E','A','I')
AND cc.activity = 'LEGACY'
-- GROUP BY cc.activity_value
) min_pledge_dates
ON --cc.activity_value = min_pledge_dates.activity_value AND
cc.valid_from = min_pledge_dates.pledge_date
WHERE
cc.activity_value in ('P','C','E','A','I')AND
cc.activity = 'LEGACY'
GROUP BY
cl.contact_number,c.label_name,cc.activity_value,cc.activity,s.source_desc
order by
cl.contact_number,c.label_name,cc.activity_value,cc.activity,s.source_desc