CTE with TEMP table

Santhosh Kunder 41 Reputation points
2022-12-03T12:54:15.15+00:00

What are the other ways to achieve the below results?

-- Run pre-results and store it in temp table

CREATE TABLE #TEMP_SAVE (USERID VARCHAR(100))  
INSERT INTO  #TEMP_SAVE  
SELECT USERID FROM IMSIDENTITYPRE  
EXCEPT  
SELECT USERID FROM PPPRETEST  
  

-- Run Post results and filter results out with TEMP_Save results

;WITH CTE(USERID)  
AS   
(SELECT ims.USERID FROM IMSIdentityPOST ims  
EXCEPT  
SELECT pp.USERID FROM PPposttest pp)  
SELECT * FROM CTE WHERE  USERID  IN (SELECT USERID FROM #TEMP_SAVE)  
Developer technologies | Transact-SQL
SQL Server | Other
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 121.8K Reputation points MVP Volunteer Moderator
    2022-12-03T17:59:05.573+00:00

    Rather than using the temp table, you could have two CTEs:

       ;WITH CTE(USERID) AS (  
          SELECT ims.USERID FROM IMSIdentityPOST ims  
          EXCEPT  
          SELECT pp.USERID FROM PPposttest pp  
       ), TEMP_SAVE(USERID) AS (  
          SELECT USERID FROM IMSIDENTITYPRE  
          EXCEPT  
          SELECT USERID FROM PPPRETEST  
       )  
       SELECT * FROM CTE WHERE USERID IN (SELECT USERID FROM TEMP_SAVE)  
    

    You could also make use of the INTERSECT operator:

       (SELECT ims.USERID FROM IMSIdentityPOST ims  
        EXCEPT  
        SELECT pp.USERID FROM PPposttest pp  
       )  
       INTERSECT  
       (  
          SELECT USERID FROM IMSIDENTITYPRE  
          EXCEPT  
          SELECT USERID FROM PPPRETEST  
       )  
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2022-12-05T03:08:49.127+00:00

    Hi @Santhosh Kunder

    In addition to the two methods proposed by ErlandSommarskog, I have come up with the following solutions.

    In addition to CTE, you can also use derived tables.

    select * from (SELECT ims.USERID FROM IMSIdentityPOST ims  
                   EXCEPT  
                   SELECT pp.USERID FROM PPposttest pp) as D  
       WHERE USERID IN (  
       SELECT USERID FROM (SELECT USERID FROM IMSIDENTITYPRE  
                           EXCEPT  
                           SELECT USERID FROM PPPRETEST) as P);  
    

    You can also use in and not in after the where condition.

    select * from (SELECT ims.USERID FROM IMSIdentityPOST ims  
                   EXCEPT  
                   SELECT pp.USERID FROM PPposttest pp) as D  
       WHERE USERID IN (SELECT USERID FROM IMSIDENTITYPRE)   
         and USERID not IN (SELECT USERID FROM PPPRETEST);  
    

    Best regards,
    Percy Tang

    ----------

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.