CTE DML

  • SqlMel (6/17/2014)


    I actually thought "select distinct * ..." might raise an error.

    I have never used this and wonder why it would even be allowed.

    A "SELECT DISTINCT * ..." is just to return a resultset where each (complete) row is unique. It's the same as a GROUP BY on all columns. The DISTINCT has the same performance costs as a GROUP BY on all selected columns.

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
  • BWFC (6/17/2014)


    rhythmk (6/17/2014)


    BWFC (6/17/2014)


    rhythmk (6/17/2014)


    Thanks for question.It reminds me the trick to delete the duplicate records in a table 🙂

    Could you enlighten me on that please? It sounds very useful.

    Here you go.

    CREATE TABLE #TEMP (ID INT,FNAME VARCHAR(50),LNAME VARCHAR(50))

    GO

    INSERT INTO #TEMP

    SELECT 1,'LIONEL','MESSI' UNION ALL

    SELECT 1,'LIONEL','MESSI' UNION ALL

    SELECT 1,'LIONEL','MESSI' UNION ALL

    SELECT 2,'NEYMAR','SILVA' UNION ALL

    SELECT 2,'NEYMAR','SILVA' UNION ALL

    SELECT 3,'CHRISTIANO','RONALDO' UNION ALL

    SELECT 3,'CHRISTIANO','RONALDO' UNION ALL

    SELECT 4, 'THOMAS','MUELLER'

    GO

    --SELECT * FROM #TEMP

    ;WITH DUPL AS

    (SELECT ID,FNAME,LNAME,ROW_NUMBER() OVER (PARTITION BY ID,FNAME,LNAME ORDER BY ID,FNAME,LNAME) [RN]

    FROM #TEMP)

    DELETE FROM DUPL WHERE RN <> 1

    --SELECT * FROM #TEMP

    --DROP TABLE #TEMP

    I like that, there's another one to add to the toolbox. Thanks.

    Interesting 🙂

  • Nice QoD

  • I had set myself a task to increase my knowledge of CTEs and found some interesting things. I didn't know you could use a CTE to modify existing data either.

    The DISTINCT was a red herring for those who noticed I had the same student in there twice... 🙂

    There are no facts, only interpretations.
    Friedrich Nietzsche

  • You learn something new every day...The multiple rows on an Insert Into/Values fails on older versions of SQL, but has been added to SQL 2008+

  • BWFC (6/17/2014)


    rhythmk (6/17/2014)


    Thanks for question.It reminds me the trick to delete the duplicate records in a table 🙂

    Could you enlighten me on that please? It sounds very useful.

    Yes, please.

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • domenico.delbrocco (6/17/2014)


    I've never thought about insert data in a CTE

    Same here 🙂

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • nice question.. some what useful for interview preparation....

    Manik
    You cannot get to the top by sitting on your bottom.

  • BWFC (6/17/2014)


    rhythmk (6/17/2014)


    Thanks for question.It reminds me the trick to delete the duplicate records in a table 🙂

    Could you enlighten me on that please? It sounds very useful.

    Here is an article on it

    http://jasonbrimhall.info/2011/03/21/dedupe-data-cte/

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Straight forward.

    Thanks for the question.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • I still don't use CTEs often but that script to delete duplicates has me reconsidering their importance! Very cool. Nice question. Thanks Barry.

  • SQLRNNR (6/17/2014)


    BWFC (6/17/2014)


    rhythmk (6/17/2014)


    Thanks for question.It reminds me the trick to delete the duplicate records in a table 🙂

    Could you enlighten me on that please? It sounds very useful.

    Here is an article on it

    http://jasonbrimhall.info/2011/03/21/dedupe-data-cte/

    Thanks. Very nice.

  • marcia.j.wilson (6/17/2014)


    SQLRNNR (6/17/2014)


    BWFC (6/17/2014)


    rhythmk (6/17/2014)


    Thanks for question.It reminds me the trick to delete the duplicate records in a table 🙂

    Could you enlighten me on that please? It sounds very useful.

    Here is an article on it

    http://jasonbrimhall.info/2011/03/21/dedupe-data-cte/

    Thanks. Very nice.

    You're welcome.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Thank you very much for the question.

    Please enlighten:-

    Should we select from the CTE expression name instead of the actual table - Student_CTE vs #Students?

  • Easy, but a nice learning,,,!!!

Viewing 15 posts - 16 through 30 (of 33 total)

You must be logged in to reply to this topic. Login to reply