0
我在我的桌子下面的表中的數據如何基於其他列的值條件
SQL> select * from mate2;
A_CD S_ID E_CD S_E S_NE E_IND
---------- ---------- ---------- ----- ----- -
200 1 2 A J Y
200 2 3 B A N
200 3 1 C D
200 4 2 A C Y
200 5 1 D C
200 6 3 B N Y
200 7 3 N K
我需要a_cd和(S_E,S_NE),以獲得不同的S_ID組的數量爲顯示的列數用下面條件
1)取S_NE字母代碼時E_CD = 2或(E_CD = 3 AND E_IND = N)
2)取S_E當E_CD = 1或(E_CD = 3 AND E_IND = Y )
3)取兩者S_E和S_NE當E_CD = 3和E_IND爲空
輸出應該像
200 A 3
200 B 2
200℃2
..
.. 。
我寫了這樣的事情,但它不工作
select a.a_cd, a.letter_code, COUNT (DISTINCT a.s_id) AS cnt
FROM (SELECT m.a_cd,
m.s_id,
CASE
WHEN (m.e_cd = 1)
OR (m.e_cd = 3 AND m.e_ind = 'Y')
THEN
m.S_E
WHEN (m.e_cd = 2) OR (m.e_cd = 3 AND m.e_ind = 'N') then m.S_NE
WHEN m.e_ind is null then m.S_NE
END
AS letter_code from mate2 m
union
SELECT m.a_cd,
m.s_id,
CASE
WHEN (m.e_cd = 1)
OR (m.e_cd = 3 AND m.e_ind = 'Y')
THEN
m.S_E
WHEN (m.e_cd = 2) OR (m.e_cd = 3 AND m.e_ind = 'N') then m.S_NE
WHEN m.e_ind is null then m.S_NE
END
AS letter_code from mate2 m) a group by a.ai_cd,a.letter_code