-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 が 2 回使用されているため、読みやすくなっています。

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 に答える