SQL 查询查找每个客户的第一条记录并更新
SQL query to find first record of each customer and update
我有一个 table,其中有两列,即客户,project_id。我的要求是为每个客户找到第一个 project_id,并相应地命名例如 'General Project',如果为客户找到了多个项目,则将下一个项目命名为 concat('PROJECT', '-', project_id)。在下图中,您可以看到第 4 列数据是我要求的结果,但我不确定如何找到它?提前致谢
您可以使用 row_number()
和字符串操作:
select t.*,
(case when row_number() over (partition by customer order by row_number()) = 1
then 'General Project'
else replace('Project-[n]', '[n]', project_id)
end) as project_name
from t;
这恰好适用于 Oracle 和 SQL 服务器。
这是一个选项(在 Oracle 中有效;无法说明 SQL 服务器)。示例数据(您已经拥有并且不输入它位于第 1 - 13 行。您可能感兴趣的查询从第 15 行开始。
SQL> with
2 test (row_num, customer, project_Id) as
3 -- sample data
4 (select 1, 115432, 1 from dual union all
5 select 2, 115432, 2 from dual union all
6 select 3, 115432, 3 from dual union all
7 --
8 select 4, 116500, 1 from dual union all
9 select 5, 116500, 2 from dual union all
10 --
11 select 6, 112342, 3 from dual union all
12 select 7, 112342, 4 from dual
13 ),
14 --
15 data as
16 (select row_num, customer, project_id,
17 row_number() over (partition by customer order by project_id) rn
18 from test
19 )
20 -- final query
21 select row_num, customer, project_id,
22 --
23 case when rn = 1 then 'GENERAL PROJECT'
24 else 'PROJECT-' || project_id
25 end as project_name
26 from data
27 order by row_Num;
ROW_NUM CUSTOMER PROJECT_ID PROJECT_NAME
---------- ---------- ---------- -------------------------
1 115432 1 GENERAL PROJECT
2 115432 2 PROJECT-2
3 115432 3 PROJECT-3
4 116500 1 GENERAL PROJECT
5 116500 2 PROJECT-2
6 112342 3 GENERAL PROJECT
7 112342 4 PROJECT-4
7 rows selected.
如果第一个项目编号总是小于其他项目那么你可以尝试:
select customer,project_id,
case when project_id=(select min(project_id) from table t2 where
t.customer=t2.customer) then 'General Project'
else concat('Project-',project_id)
end Project_Name
from table t ;
这适用于 mysql。
我有一个 table,其中有两列,即客户,project_id。我的要求是为每个客户找到第一个 project_id,并相应地命名例如 'General Project',如果为客户找到了多个项目,则将下一个项目命名为 concat('PROJECT', '-', project_id)。在下图中,您可以看到第 4 列数据是我要求的结果,但我不确定如何找到它?提前致谢
您可以使用 row_number()
和字符串操作:
select t.*,
(case when row_number() over (partition by customer order by row_number()) = 1
then 'General Project'
else replace('Project-[n]', '[n]', project_id)
end) as project_name
from t;
这恰好适用于 Oracle 和 SQL 服务器。
这是一个选项(在 Oracle 中有效;无法说明 SQL 服务器)。示例数据(您已经拥有并且不输入它位于第 1 - 13 行。您可能感兴趣的查询从第 15 行开始。
SQL> with
2 test (row_num, customer, project_Id) as
3 -- sample data
4 (select 1, 115432, 1 from dual union all
5 select 2, 115432, 2 from dual union all
6 select 3, 115432, 3 from dual union all
7 --
8 select 4, 116500, 1 from dual union all
9 select 5, 116500, 2 from dual union all
10 --
11 select 6, 112342, 3 from dual union all
12 select 7, 112342, 4 from dual
13 ),
14 --
15 data as
16 (select row_num, customer, project_id,
17 row_number() over (partition by customer order by project_id) rn
18 from test
19 )
20 -- final query
21 select row_num, customer, project_id,
22 --
23 case when rn = 1 then 'GENERAL PROJECT'
24 else 'PROJECT-' || project_id
25 end as project_name
26 from data
27 order by row_Num;
ROW_NUM CUSTOMER PROJECT_ID PROJECT_NAME
---------- ---------- ---------- -------------------------
1 115432 1 GENERAL PROJECT
2 115432 2 PROJECT-2
3 115432 3 PROJECT-3
4 116500 1 GENERAL PROJECT
5 116500 2 PROJECT-2
6 112342 3 GENERAL PROJECT
7 112342 4 PROJECT-4
7 rows selected.
如果第一个项目编号总是小于其他项目那么你可以尝试:
select customer,project_id,
case when project_id=(select min(project_id) from table t2 where
t.customer=t2.customer) then 'General Project'
else concat('Project-',project_id)
end Project_Name
from table t ;
这适用于 mysql。