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. How to get a multi colum select with one colum SELECT DISTINCT with only one command ?

How to get a multi colum select with one colum SELECT DISTINCT with only one command ?

Scheduled Pinned Locked Moved Database
databasetutorialquestion
6 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.
  • F Offline
    F Offline
    fracalifa
    wrote on last edited by
    #1

    Hi all, I want to get a multicolumn selection but with the behavior of the DISTINCT cmd only on the first column, something like that SELECT DISTINCT order_id, date FROM Orders WHERE ..... but the distinction should only refer on the "order_id" and not even on the "date" . How does the sql-cmd looks like ? thanks in advance Frank

    K P 2 Replies Last reply
    0
    • F fracalifa

      Hi all, I want to get a multicolumn selection but with the behavior of the DISTINCT cmd only on the first column, something like that SELECT DISTINCT order_id, date FROM Orders WHERE ..... but the distinction should only refer on the "order_id" and not even on the "date" . How does the sql-cmd looks like ? thanks in advance Frank

      K Offline
      K Offline
      Kschuler
      wrote on last edited by
      #2

      Can you give us a sample of what data exists in the table, and then what data you want after the query has been run? Because what you have doesn't seem to make sense...if your data looks like this: order_id - date 1 - 1/1/2008 2 - 1/1/2008 2 - 1/2/2008 then which date are you going to want to display? If you distinct on only the order_id (which isn't really possible using a distinct, you would have to use a group by) then the SQL doesn't know which date you want...(which is why it's not possible using Distinct)....so please supply us some more info so we can help.

      F 1 Reply Last reply
      0
      • F fracalifa

        Hi all, I want to get a multicolumn selection but with the behavior of the DISTINCT cmd only on the first column, something like that SELECT DISTINCT order_id, date FROM Orders WHERE ..... but the distinction should only refer on the "order_id" and not even on the "date" . How does the sql-cmd looks like ? thanks in advance Frank

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        This doesn't make sense. Either all of the data is distinct or none of it is. If you want a single order_id, then you can only retrieve one date otherwise this order will span multiple lines.

        Deja View - the feeling that you've seen this post before.

        My blog | My articles

        1 Reply Last reply
        0
        • K Kschuler

          Can you give us a sample of what data exists in the table, and then what data you want after the query has been run? Because what you have doesn't seem to make sense...if your data looks like this: order_id - date 1 - 1/1/2008 2 - 1/1/2008 2 - 1/2/2008 then which date are you going to want to display? If you distinct on only the order_id (which isn't really possible using a distinct, you would have to use a group by) then the SQL doesn't know which date you want...(which is why it's not possible using Distinct)....so please supply us some more info so we can help.

          F Offline
          F Offline
          fracalifa
          wrote on last edited by
          #4

          Hi Kschuler, my problem is that I have to show the date and I have to show the order_id but only once. I know, in most cases it doesn't makes sense. But for my application it doesn't matters which date has to be displayed when double entries exists. The central point is that all entries have to show a date multiple entries or not. This is the crux. Now I think there is no SQL solution for that problem. Now I filter the recordset. Thank you for your help

          K 1 Reply Last reply
          0
          • F fracalifa

            Hi Kschuler, my problem is that I have to show the date and I have to show the order_id but only once. I know, in most cases it doesn't makes sense. But for my application it doesn't matters which date has to be displayed when double entries exists. The central point is that all entries have to show a date multiple entries or not. This is the crux. Now I think there is no SQL solution for that problem. Now I filter the recordset. Thank you for your help

            K Offline
            K Offline
            Kschuler
            wrote on last edited by
            #5

            You can accomplish it with something like this: SELECT order_id, MAX(date) FROM tablename GROUP BY order_id The idea is that you will return one order_id and the largest date with that order_id. I know it would work for a number, I'm just not sure how a date reacts to the MAX() function...you could also use MIN if you want the smallest date...and if you did this SELECT order_id, MAX(date), COUNT(*) FROM tablename GROUP BY order_id It would also display how many dates exist for that order id. So maybe you need to do some research on GROUP BY and how it affects date datatypes. Hope this helps.

            F 1 Reply Last reply
            0
            • K Kschuler

              You can accomplish it with something like this: SELECT order_id, MAX(date) FROM tablename GROUP BY order_id The idea is that you will return one order_id and the largest date with that order_id. I know it would work for a number, I'm just not sure how a date reacts to the MAX() function...you could also use MIN if you want the smallest date...and if you did this SELECT order_id, MAX(date), COUNT(*) FROM tablename GROUP BY order_id It would also display how many dates exist for that order id. So maybe you need to do some research on GROUP BY and how it affects date datatypes. Hope this helps.

              F Offline
              F Offline
              fracalifa
              wrote on last edited by
              #6

              Great Kschuler, it works !!!! thanks It works because you are using agregat functions as a second and/or third parameter after the select. That helps for the future. Frank

              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