Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Distinct

SQL Distinct

Scheduled Pinned Locked Moved Database
databasehelp
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nlindley7
    wrote on last edited by
    #1

    So I have a database with multiple records with the same agtMast field. What I need to do is select unique agtMast records, but i need the whole record. I know this will select the unique fields... SELECT DISTINCT agtMast FROM table_name; ...but it only selects the agtMast field and I need the entire record. Any help would be greatly appreciated. Nathan Lindley

    D E 2 Replies Last reply
    0
    • N nlindley7

      So I have a database with multiple records with the same agtMast field. What I need to do is select unique agtMast records, but i need the whole record. I know this will select the unique fields... SELECT DISTINCT agtMast FROM table_name; ...but it only selects the agtMast field and I need the entire record. Any help would be greatly appreciated. Nathan Lindley

      D Offline
      D Offline
      Drew McGhie
      wrote on last edited by
      #2

      Well, say there are 3 rows agtMast ID Name Other One 22 James Other1 One 22 James Other2 Two 22 Bond Whee! There are 2 distinct values for the agtMast column, but 2 possible sets of values are different. Do you want one row for each agtMast value, and are there specifics as to which one should be selected, or do you want the distinct sets of values (So if One/22/James/Other1 appeared 10 times, you would want it to appear only once, but have One/22/James/Other2 appear as well after the query) I need some more information as to how you're clarifying the "correct" row.

      N 1 Reply Last reply
      0
      • N nlindley7

        So I have a database with multiple records with the same agtMast field. What I need to do is select unique agtMast records, but i need the whole record. I know this will select the unique fields... SELECT DISTINCT agtMast FROM table_name; ...but it only selects the agtMast field and I need the entire record. Any help would be greatly appreciated. Nathan Lindley

        E Offline
        E Offline
        ednrgc
        wrote on last edited by
        #3

        Group by agtMast

        N 1 Reply Last reply
        0
        • D Drew McGhie

          Well, say there are 3 rows agtMast ID Name Other One 22 James Other1 One 22 James Other2 Two 22 Bond Whee! There are 2 distinct values for the agtMast column, but 2 possible sets of values are different. Do you want one row for each agtMast value, and are there specifics as to which one should be selected, or do you want the distinct sets of values (So if One/22/James/Other1 appeared 10 times, you would want it to appear only once, but have One/22/James/Other2 appear as well after the query) I need some more information as to how you're clarifying the "correct" row.

          N Offline
          N Offline
          nlindley7
          wrote on last edited by
          #4

          Thanks for the reply Drew. What I am looking for is just one of each of the agtMast (which is the agent's code) records. What the situation is is before on the old system, they listed each agent every time they changed an address (instead of updating, they just added new rows). What I need to do is just get one of the records (1 for each agtMast code, but no more than 1), for the main contact information and if the address is not the current one, it doesn't matter. I would just put the DISTINCT records into a holding table, but i'm not able to grab just a single record for each DISTINCT agtMast. Thanks again for the reply! Nathan Lindley

          1 Reply Last reply
          0
          • E ednrgc

            Group by agtMast

            N Offline
            N Offline
            nlindley7
            wrote on last edited by
            #5

            Thanks for the reply Ed. How can I use group by agtMast if the select includes more columns than the agtMast? Nathan Lindley

            E 1 Reply Last reply
            0
            • N nlindley7

              Thanks for the reply Ed. How can I use group by agtMast if the select includes more columns than the agtMast? Nathan Lindley

              E Offline
              E Offline
              ednrgc
              wrote on last edited by
              #6

              I don't mean to be condescending, but are you familiar with the GROUP BY clause?

              N 1 Reply Last reply
              0
              • E ednrgc

                I don't mean to be condescending, but are you familiar with the GROUP BY clause?

                N Offline
                N Offline
                nlindley7
                wrote on last edited by
                #7

                Well, you can't include a column in the select statement without it being contained in either an aggregate function or containing it in the GROUP BY clause. Nathan Lindley

                E 1 Reply Last reply
                0
                • N nlindley7

                  Well, you can't include a column in the select statement without it being contained in either an aggregate function or containing it in the GROUP BY clause. Nathan Lindley

                  E Offline
                  E Offline
                  ednrgc
                  wrote on last edited by
                  #8

                  Exactly. I must be missing something on your requirement. You want one record per agtMast, and you want the other columns also. You have to determine what you want from the other columns. For example: SELECT agtMast, MAX(CustName), MAX(DateIssued).... FROM table_name;

                  N 1 Reply Last reply
                  0
                  • E ednrgc

                    Exactly. I must be missing something on your requirement. You want one record per agtMast, and you want the other columns also. You have to determine what you want from the other columns. For example: SELECT agtMast, MAX(CustName), MAX(DateIssued).... FROM table_name;

                    N Offline
                    N Offline
                    nlindley7
                    wrote on last edited by
                    #9

                    Yea, I used MIN(), but what I essentially did was MIN(column1) As Column1, etc.. and inserted the records into a temp table, cleared the original table, and then loaded the records back into the original table. I don't know why I didn't do this in the first place, but i guess i was looking for a more concrete way to do it. Oh well, this works. Thanks again for the responses! Nathan Lindley

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups