下面是一個表格,其中包含候選人 ID、他們參加的兩次面試以及面試官的姓名以及每次面試的結果。
| 候選人 ID | 采訪_1 | 采訪_2 | 結果_1 | 結果_2 |
|---|---|---|---|---|
| 1 | 面試官_A | 面試官_B | 經過 | 經過 |
| 2 | 面試官_C | 面試官_D | 經過 | 拒絕 |
我需要幫助將interview_1和interview_2列合并為一列,并計算每個面試官給候選人的通過和拒絕的數量,我希望看到的結果如下:
| 面試官姓名 | pass_count | 拒絕計數 |
|---|---|---|
| 面試官_A | 1 | 0 |
| 面試官_B | 1 | 0 |
| 面試官_C | 1 | 0 |
| 面試官_D | 0 | 1 |
SQL 或 Python 都適合我!非常感激!
uj5u.com熱心網友回復:
在 SQL Server 中,這對于一個 CROSS APPLY
例子
Select [candidate_id]
,B.[Interview_name]
,pass_count = case when result='Pass' then 1 else 0 end
,reject_count = case when result='Pass' then 0 else 1 end
From YourTable A
Cross Apply ( values ([interview_1],[result_1])
,([interview_2],[result_2])
) B(Interview_name,result)
結果
candidate_id Interview_name pass_count reject_count
1 Interviewer_A 1 0
1 Interviewer_B 1 0
2 Interviewer_C 1 0
2 Interviewer_D 0 1
uj5u.com熱心網友回復:
您可以將資料集拉兩次并合并在一起,我假設它不是一個笨重的集合。
就像是 -
WITH combined_set AS(
SELECT
candidate_id,
interviewer_1 as interviewer,
result_1 as result
from candidate_table
UNION
SELECT
candidate_id,
interviewer_2 as interviewer,
result_2 as result
from candidate_table)
SELECT
interviewer,
count(case when result = 'Pass' then 1 end) as pass_count,
count(case when result = 'Reject' then 1 end) as reject_count
FROM combined_set
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/376788.html
標籤:Python sql sql-server
