[英]capturing and summing string values from one column?
I have this query (with some help from mr Erwin Brandstetter ), which prints week intervals. 我有这个查询(在Erwin Brandstetter先生的帮助下),它打印出每周间隔。
SELECT to_char(d.day, 'YYYY/MM/DD - ') || to_char(d.day + 6, 'YYYY/MM/DD') AS week
FROM (
SELECT day::date
FROM generate_series('2014-08-01'::date, '2014-09-14'::date, interval '1 week') day
) d
JOIN account_details a ON a.date_opened >= d.day
AND a.date_opened < d.day + 6
GROUP BY d.day;
But I want to append and sum other info depending on the value in a specific column 但是我想根据特定列中的值来附加和求和其他信息
This should be the end result. 这应该是最终结果。
So the info for activated
and declined
comes from one string column situation
. 因此,有关activated
和declined
的信息来自一个字符串列situation
。
I tried to capture a regexp_matches
match value from the column and sum the amount of captures (bellow), but it did not do the trick. 我试图从该列中捕获一个regexp_matches
匹配值,并对捕获的总和(波纹管)求和,但没有成功。
Please, how would you go about capturing and summing string values from one column? 请,您将如何从一列捕获和求和字符串值?
SELECT to_char(d.day, 'YYYY/MM/DD - ') || to_char(d.day + 6, 'YYYY/MM/DD') AS week
, SUM(SELECT regexp_matches(situation, 'active')) AS activated
, SUM(SELECT regexp_matches(situation, 'declined')) AS declined
FROM (
SELECT day::date
FROM generate_series('2014-08-01'::date, '2014-09-14'::date, interval '1 week') day
) d
JOIN account_details a ON a.date_opened >= d.day
AND a.date_opened < d.day + 6
GROUP BY d.day;
SAMPLE DATA 样本数据 So there is quite a lot more columns, but the only two will be used here. 因此,还有很多列,但这里仅使用两列。 open_date
and situation
open_date
和situation
situation
also has quite a few different options, but only Active
and Declined
will be used situation
也有很多不同的选择,但是只会使用“ Active
和“ Declined
Not sure if this will work, but possibly you want to count matches: 不知道这是否行得通,但可能您想计算匹配数:
SELECT to_char(d.day, 'YYYY/MM/DD - ') || to_char(d.day + 6, 'YYYY/MM/DD') AS week
, SUM(CASE WHEN LOWER(situation) LIKE '%active%' THEN 1 ELSE 0 END) AS activated
, SUM(CASE WHEN LOWER(situation) LIKE '%declined%' THEN 1 ELSE 0 END) AS declined
FROM (
SELECT day::date
FROM generate_series('2014-08-01'::date, '2014-09-14'::date, interval '1 week') day
) d
JOIN account_details a ON a.date_opened >= d.day
AND a.date_opened < d.day + 6
GROUP BY d.day;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.