Create a new table of unique values from a field in an existing table

  • Seems like a homework question.

    What have you tried so far?

  • This is not a homework question, I am looking for the SQL Command to write this in an SQL statement. I know this is a simple question, that is why I put it in the "newbie" section, any help would be really appreciated.

    Thanks

  • Give us the DDL statments along with the DDL statements to load the tables.... we'll show you how to do it.

  • There really are no "DDL statements" as such. I am building the database by importing a bunch of seperate excel tables into Access and then importing the access database into SQL Server 2000.

  • Cool, do that then we'll be able to help you.

  • Its done

  • Ok i think i am almost there, I have just found out about the "Distinct" keyword, this allows me to show just the distinct values in an SQL statement:

    Select Distinct (fieldname) from (tablename)

    Now I just need to work out how to save that output into a new table

  • Ok I have got it:

    CREATE TABLE new_table

    AS (SELECT Distinct (fieldname) FROM (tableName));

    So simple!

    Yet it took me an hour of research on the internet, but it should only take an expert 5 seconds to work out.

  • I do believe that researching "SELECT INTO" should get you where you want to be.

    Kyle

  • Like this:

    INSERT Into {target_table} ( ..target_columns.. )

    SELECT Distinct ..source_columns..

    From {source_table}

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Thank you Kyle,

    That was exactly what I needed, the "create table as" was not working, but "select into" worked perfect.

  • I'm glad it helped.

    I'll now add that the table you just created did not inherit any properties other than names and data types from the source table. Any indexes or constraints (including defaults) that you may need will have to be manually applied.

    This method is a quick and dirty way to get a table created, but as you familiarize yourself with SQL Server, I suggest you strive to create your tables first then fill them (using the aforementioned INSERT statement). You'll have more control and understand better what is going on.

    Good luck!

    Kyle

  • garethmann101 (9/11/2008)Yet it took me an hour of research on the internet, but it should only take an expert 5 seconds to work out.

    But if we just hand you the solution without your having tried to solve it youself, how much do you truely learn? That's why you were asked to show what you had already done to solve your problem.

    If you have tried and failed, and show what you have done, and where you are having problems, many of us are more than willing to jump in and help increase your knowledge.

    😎

  • I still don't see any code to replace the city name to cityid in the base table, nor indexing, nor foreign keys strategies in place...

    You guys still have much work to do!!!

    BTW, I always do a manual data cleaning after a task like this. There's always someone who screwed up the data (New-York, New-York City, NYC, N-Y City...). That needs to be manually cleansed (script can only point you in the right direction, manual intervention is necessary here).

    Good luck.

  • [Quote]

    But if we just hand you the solution without your having tried to solve it youself, how much do you truely learn? That's why you were asked to show what you had already done to solve your problem.[/Quote]

    My question was entirely of syntax, I was aksing for the correct syntax for a particular function. I was not asking for someone to complete an entire project for me.

    If I had tried to work out that syntax myself the only thing I would have learnt is that it takes too long and its much better to ask on an online forum, as I have learnt many times in the last 8 years I have spent computer programming. You can post a question on a forum about syntax, then spend a whole day trying to work it out and then, having finally worked it out, you see that someone has posted the answer right there.

    If I was doing this as "homework", I would have asked the teacher for the syntax.

Viewing 15 posts - 1 through 15 (of 20 total)

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