我有这两个表,我将使用EMP_PHONE_ID加入以获得结果。 JOIN条件多一点 很复杂,但这只是一个入门的样本。当我加入两个表时,我为每个EMP_ID获得了2行 因为每个EMP_ID映射到两个EMP_PHONE_ID。我希望最终结果是单行结合PERM和TEMP值。
TABLE B
EMP_PHONE_ID || PERM_VALUE || TEMP_VALUE
1 || value1 || NULL
2 || NULL || value2
3 || value3 || NULL
4 || NULL || value4
TABLE A
EMP_ID ||EMP_PHONE_ID ||CODE
1 ||1 ||1
1 ||2 ||2
2 ||3 ||1
2 ||4 ||2
DESIRED RESULTS
EMP_ID || PERM_VALUE || TEMP_VAL
1 || value1 || value2
2 || value3 || value4
Here's my select
SELECT
CASE WHEN A.CODE = 1 THEN B.PERM_VALUE END AS PERM_VALUE1,
ASE WHEN A.CODE = 2 THEN B.PERM_VALUE END AS PERM_VALUE2
FROM TABLE A A, TABLE B B
WHERE A.EMP_PHONE_ID = B.EMP_PHONE_ID;
DESIRED RESULTS
EMP_ID || PERM_VALUE || TEMP_VAL
1 || value1 || value2
2 || value3 || value4
答案 0 :(得分:0)
Inner join
+ Group By
+ Min or Max
汇总应该适合您
SELECT EMP_ID,
Min(PERM_VALUE) as PERM_VALUE,
Min(TEMP_VALUE) as TEMP_VALUE
FROM TABLEA A
JOIN TABLEB B
ON A.EMP_PHONE_ID = B.EMP_PHONE_ID
GROUP BY EMP_ID
如果您想根据Code
显示数据,请使用此
SELECT EMP_ID,
Min(CASE WHEN A.CODE = 1 then PERM_VALUE end) as PERM_VALUE,
Min(CASE WHEN A.CODE = 2 then TEMP_VALUE end) as TEMP_VALUE
FROM TABLEA A
JOIN TABLEB B
ON A.EMP_PHONE_ID = B.EMP_PHONE_ID
GROUP BY EMP_ID
我认为您在TABLEA