-2

有没有更短的方法...??

SELECT clid, cldoel, clanker, clprefix, clleerlijn, f1, f2, f3, f4 FROM Clusters  
WHERE clleerlijn like 'WJNLLUI%'   AND (f3 IS NOT NULL  OR f4 IS NOT NULL )
AND clid in(
Select agClid FROM agenda2 as a JOIN evaluaties2 as e ON a.agclid = e.evalclid and agLkrid = evallkrid 
WHERE (agSchoolid='18' and agDatum >=   '2013/01/01' and agDatum <=   '2013/02/12')
GROUP BY agClid
except
(SELECT agClid
FROM agenda2 as a 
    JOIN evaluaties2 as e 
        ON a.agclid = e.evalclid and agLkrid = evallkrid 
WHERE agSchoolid='18' 
    and agLkrid in (1,2932,2935) 
    and agDatum >= '2013/01/01' 
    and agDatum <= '2013/02/12' 
    AND evalWaarde = 3
GROUP BY agclid
HAVING COUNT(DISTINCT agLkrid) = 3)
)
4

1 回答 1

0

由于相同的 CTE 被使用了两次,所以只有一点点短,但可读性更好:

WITH cte(clid) As (
  Select agClid FROM agenda2 as a JOIN evaluaties2 as e ON a.agclid = e.evalclid and agLkrid = evallkrid 
  WHERE (agSchoolid='18' and agDatum >= '2013/01/01' and agDatum <= '2013/02/12')
)
SELECT clid, cldoel, clanker, clprefix, clleerlijn, f1, f2, f3, f4 FROM Clusters  
WHERE clleerlijn like 'WJNLLUI%' AND (f3 IS NOT NULL  OR f4 IS NOT NULL )
AND clid in(
  SELECT DISTINCT agClid FROM cte
  except
  (SELECT agClid FROM cte WHERE agLkrid in (1,2932,2935) AND evalWaarde = 3
   GROUP BY agclid
   HAVING COUNT(DISTINCT agLkrid) = 3
  )
)

我还将第一个 GROUP BY 更改为 SELECT DISTINCT。

如果您正在寻找性能更高的解决方案,这将无济于事。重写的 SQL 语句不会被更快地评估。

于 2013-02-12T17:39:32.207 回答