使用条件

时间:2015-11-16 08:21:58

标签: ms-access

我有1个表FIK2015,如何简化这个sql。处理这么长时间。 TQ

SELECT t1.AGILIR, 
(SELECT COUNT(*) FROM FIK2015 t2 WHERE t2.AGILIR = t1.AGILIR AND t2.GRED01  = 'A')+
(SELECT COUNT(*) FROM FIK2015 t3 WHERE t3.AGILIR = t1.AGILIR AND t3.GRED02 = 'A')+
(SELECT COUNT(*) FROM FIK2015 t4 WHERE t4.AGILIR = t1.AGILIR AND t4.GRED03 = 'A') & "A" & " " & 
(SELECT COUNT(*) FROM FIK2015 t9 WHERE t9.AGILIR = t1.AGILIR AND t9.GRED01 = 'B')+
(SELECT COUNT(*) FROM FIK2015 t10 WHERE t10.AGILIR = t1.AGILIR AND t10.GRED02 = 'B')+
(SELECT COUNT(*) FROM FIK2015 t11 WHERE t11.AGILIR = t1.AGILIR AND t11.GRED03 = 'B') & "B" & " "&  
(SELECT COUNT(*) FROM FIK2015 t16 WHERE t16.AGILIR = t1.AGILIR AND t16.GRED01 = 'C')+
(SELECT COUNT(*) FROM FIK2015 t17 WHERE t17.AGILIR = t1.AGILIR AND t17.GRED02 = 'C')+
(SELECT COUNT(*) FROM FIK2015 t18 WHERE t18.AGILIR = t1.AGILIR AND t18.GRED03 = 'C') & "C" & " " & 
(SELECT COUNT(*) FROM FIK2015 t23 WHERE t23.AGILIR = t1.AGILIR AND t23.GRED01 = 'D')+
(SELECT COUNT(*) FROM FIK2015 t24 WHERE t24.AGILIR = t1.AGILIR AND t24.GRED02 = 'D')+
(SELECT COUNT(*) FROM FIK2015 t25 WHERE t25.AGILIR = t1.AGILIR AND t25.GRED03 = 'D') AS RESULT
FROM FIK2015 AS t1
GROUP BY t1.AGILIR;

这是我的桌子。

AGILIR   GRED01 GRED02  GRED03  expected result
L0011001    A     B       A          2A 1B
L0011002    A     B       B          1A 2B
L0011003    A     A       C          2A 1C
L0011004    A     A       C          2A 1C

0 个答案:

没有答案