这段代码一直让我感到难过,我有两个培训课程,我试图找到哪个员工已经拿走了它们的记录,代码是:
SELECT O896IA_VEMPPRSA.EMP_NA, O867IA_VTRAING.SYS_EMP_ID_NR, O867IA_VTRAING.REG_NR,
O867IA_VTRAING.DIS_NR, O867IA_VTRAING.TNG_SYS_NR
FROM O896IA_VEMPPRSA INNER JOIN O867IA_VTRAING ON O896IA_VEMPPRSA.SYS_EMP_ID_NR =
O867IA_VTRAING.SYS_EMP_ID_NR
WHERE (((O896IA_VEMPPRSA.EMP_EMT_STS_CD)<>"T"))
GROUP BY O896IA_VEMPPRSA.EMP_NA, O867IA_VTRAING.SYS_EMP_ID_NR, O867IA_VTRAING.REG_NR,
O867IA_VTRAING.DIS_NR, O867IA_VTRAING.TNG_SYS_NR;
我需要找到TNG_SYS_NR 1095或1743.如果他们有记录,那么我需要一个Y,如果不是,我需要它来显示N.
我需要它来显示姓名,ID,Reg,Dis,1095 Y或N,1743 Y或N.因此,如果他们选择了两个课程,我只需要一行包含他们所有的数据,Y需要1743&amp;现在,它显示了一名员工两次列出Y,1095 N 1743和N 1095 Y 1743,如果他们同时两者。
+-----------------------------------+--------------------------------+--------+--------+------+------+
| EMP_NA | SYS_EMP_ID_NR | REG_NR | DIS_NR | 1095 | 1743 |
+-----------------------------------+--------------------------------+--------+--------+------+------+
| AMOS, SHARNELE | 1531218 | 20 | 10 | N | Y |
| AMOS, SHARNELE | 1531218 | 20 | 10 | Y | N |
| ANDERSON JERNAGIN, SHERRY | 0354362 | 25 | 80 | N | Y |
| ANDERSON JERNAGIN, SHERRY | 0354362 | 25 | 80 | Y | N |
| BAILEY, EDWARD | 0070433 | 20 | 10 | N | Y |
| BAILEY, EDWARD | 0070433 | 20 | 10 | Y | N |
+-----------------------------------+--------------------------------+--------+--------+------+------+
答案 0 :(得分:2)
您应该可以使用IIF()
来获取计算结果:
IIF(O867IA_VTRAING.TNG_SYS_NR IN (1095, 1743), 'Y', 'N')
因此,如果您将此添加到您的查询中,它将是:
SELECT DISTINCT O896IA_VEMPPRSA.EMP_NA,
O867IA_VTRAING.SYS_EMP_ID_NR,
O867IA_VTRAING.REG_NR,
O867IA_VTRAING.DIS_NR,
IIF(O867IA_VTRAING.TNG_SYS_NR IN (1095, 1743), 'Y', 'N')
FROM O896IA_VEMPPRSA
INNER JOIN O867IA_VTRAING
ON O896IA_VEMPPRSA.SYS_EMP_ID_NR = O867IA_VTRAING.SYS_EMP_ID_NR
WHERE (((O896IA_VEMPPRSA.EMP_EMT_STS_CD)<>"T"));
编辑,根据您的更改,您应该可以使用IIF周围的聚合函数:
SELECT O896IA_VEMPPRSA.EMP_NA,
O867IA_VTRAING.REG_NR,
O867IA_VTRAING.DIS_NR,
max(IIF(O867IA_VTRAING.TNG_SYS_NR IN (1095), 'Y', 'N')) as Class1095 ,
Max(IIF(O867IA_VTRAING.TNG_SYS_NR IN (1743), 'Y', 'N')) as Class1743
FROM O896IA_VEMPPRSA
INNER JOIN O867IA_VTRAING
ON O896IA_VEMPPRSA.SYS_EMP_ID_NR = O867IA_VTRAING.SYS_EMP_ID_NR
group by O896IA_VEMPPRSA.EMP_NA, O867IA_VTRAING.REG_NR, O867IA_VTRAING.DIS_NR