>Hi Svein,thanks a lot for your help! >I also need to add one condition to my query,but I don't know how to do it >using your script,so >my new query: > > SELECT a.date,a.id FROM mytable1 a WHERE a.year=2013 > AND (a.month=1 OR (A.NUMB > IN (SELECT DISTINCT X.NUMB FROM PR_GET_SOLD(2013,1) X ) >)) > >so adding this I think I can't use join as you suggested in your script
Well, the added requirement makes the query slightly different, John, but changing it to a LEFT JOIN is simple: with ExecuteMeOnce as (SELECT DISTINCT numb FROM PR_GET_SOLD(2013,1)) SELECT a.date, a.id FROM mytable1 a LEFT JOIN ExecuteMeOnce x ON a.numb = x.numb WHERE a.year = 2013 AND (a.month = 1 OR x.numb IS NOT NULL) I'm curious to learn whether this does improve the speed or if it still takes two minutes, so please report back. HTH, Set