如何 'detect' 更改 SQL 中的列中的值?
How to 'detect' a change of value in a column in SQL?
我是 SQL 的新手,我想问:
我已将多个 table 与 CTE 合并并加入并生成此 Image here。
由此 table,我想检测并计算有多少工人从第一份或第二份工作更改了类别。
例如,Jonathan Carey 的第一个 'Sales Lapangan' job_category,并在他的第二份工作中更改为 'other',我想将此 job_category 更改为一.
我尝试过 Case,但我越来越困惑了。
这是我创建的 table 的语法:
with data_apply2 as(with data_apply as(with all_apply as(with job_id as(select job_category,
row_number() over(order by job_category) as job_id
from job_post
group by job_category)
select jp.*, job_id.job_id from job_post jp
join job_id
on job_id.job_category=jp.job_category)
select ja.worker_id, wk.name, ja.id as id_application, aa.job_category, aa.job_id
from job_post_application ja
join all_apply aa
on aa.id=ja.job_post_id
join workers wk
on wk.id = ja.worker_id
order by worker_id,ja.id)
select *,
row_number() over(partition by worker_id order by worker_id) as worker_num
from data_apply)
谢谢
select case when job_category<> job_category then 1 else 0 end as cnt
从
(
select
worker_id,
姓名,
id_application,
job_category,
job_id,
worker_num,
coalesce(lag(job_category) over(partition by worker_id order by id_application), job_category) as job_category
从
sales_table
) x
这应该会有所帮助,我正在使用滞后函数访问前一行的数据。并将其与 job_category 进行比较,如果它们不相等,我们将它们计为 1.
您可以按工人分组并查看不同工作类别的数量:
SELECT worker_id,
COUNT(DISTINCT job_category) > 1 category_change
FROM data_apply
GROUP BY worker_id;
我是 SQL 的新手,我想问:
我已将多个 table 与 CTE 合并并加入并生成此 Image here。
由此 table,我想检测并计算有多少工人从第一份或第二份工作更改了类别。
例如,Jonathan Carey 的第一个 'Sales Lapangan' job_category,并在他的第二份工作中更改为 'other',我想将此 job_category 更改为一.
我尝试过 Case,但我越来越困惑了。
这是我创建的 table 的语法:
with data_apply2 as(with data_apply as(with all_apply as(with job_id as(select job_category,
row_number() over(order by job_category) as job_id
from job_post
group by job_category)
select jp.*, job_id.job_id from job_post jp
join job_id
on job_id.job_category=jp.job_category)
select ja.worker_id, wk.name, ja.id as id_application, aa.job_category, aa.job_id
from job_post_application ja
join all_apply aa
on aa.id=ja.job_post_id
join workers wk
on wk.id = ja.worker_id
order by worker_id,ja.id)
select *,
row_number() over(partition by worker_id order by worker_id) as worker_num
from data_apply)
谢谢
select case when job_category<> job_category then 1 else 0 end as cnt 从 ( select worker_id, 姓名, id_application, job_category, job_id, worker_num, coalesce(lag(job_category) over(partition by worker_id order by id_application), job_category) as job_category 从 sales_table ) x
这应该会有所帮助,我正在使用滞后函数访问前一行的数据。并将其与 job_category 进行比较,如果它们不相等,我们将它们计为 1.
您可以按工人分组并查看不同工作类别的数量:
SELECT worker_id,
COUNT(DISTINCT job_category) > 1 category_change
FROM data_apply
GROUP BY worker_id;