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. Select Distinct Question

Select Distinct Question

Scheduled Pinned Locked Moved Database
questiontutorial
6 Posts 4 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.
  • M Offline
    M Offline
    matthias s 0
    wrote on last edited by
    #1

    Hi there, I've got a Messages table which looks roughly like this: SenderID (int) RecipientID (int) TimeStamp (datetime) Body (varchar(512) Now I'd like to do a select which gets me (RecipientID = 1) the first message (depending on the timestamp) of each sender. Example: Sender ID 5 has sent two messages, the recordset should contain the oldest message of this fella. Sender ID 6 has sent five messages, the recordset should contain the oldest message of this guy as well. I thought this could be done by tying a DISTINCT to a column. I hoped I could do it somehow like this: SELECT DISTINCT(SenderID), TimeStamp, Body FROM Messages WHERE RecipientID = 1 ORDER BY TimeStamp DESC I was just guessing into the wild. Is there an easy way to do that? Thanks in advance!

    /matthias

    I love deadlines. I like the whooshing sound they make as they fly by.
    [Douglas Adams]

    P P P 5 Replies Last reply
    0
    • M matthias s 0

      Hi there, I've got a Messages table which looks roughly like this: SenderID (int) RecipientID (int) TimeStamp (datetime) Body (varchar(512) Now I'd like to do a select which gets me (RecipientID = 1) the first message (depending on the timestamp) of each sender. Example: Sender ID 5 has sent two messages, the recordset should contain the oldest message of this fella. Sender ID 6 has sent five messages, the recordset should contain the oldest message of this guy as well. I thought this could be done by tying a DISTINCT to a column. I hoped I could do it somehow like this: SELECT DISTINCT(SenderID), TimeStamp, Body FROM Messages WHERE RecipientID = 1 ORDER BY TimeStamp DESC I was just guessing into the wild. Is there an easy way to do that? Thanks in advance!

      /matthias

      I love deadlines. I like the whooshing sound they make as they fly by.
      [Douglas Adams]

      P Offline
      P Offline
      Paddy Boyd
      wrote on last edited by
      #2

      Try

      SELECT M.SenderId, M.TimeStamp, M.Body FROM Messages AS M
      WHERE M.TimeStamp = (SELECT MIN(M1.TimeStamp) FROM Messages AS M1 WHERE M1.SenderId = M.SenderId)

      1 Reply Last reply
      0
      • M matthias s 0

        Hi there, I've got a Messages table which looks roughly like this: SenderID (int) RecipientID (int) TimeStamp (datetime) Body (varchar(512) Now I'd like to do a select which gets me (RecipientID = 1) the first message (depending on the timestamp) of each sender. Example: Sender ID 5 has sent two messages, the recordset should contain the oldest message of this fella. Sender ID 6 has sent five messages, the recordset should contain the oldest message of this guy as well. I thought this could be done by tying a DISTINCT to a column. I hoped I could do it somehow like this: SELECT DISTINCT(SenderID), TimeStamp, Body FROM Messages WHERE RecipientID = 1 ORDER BY TimeStamp DESC I was just guessing into the wild. Is there an easy way to do that? Thanks in advance!

        /matthias

        I love deadlines. I like the whooshing sound they make as they fly by.
        [Douglas Adams]

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

        Distinct won't do this because unless the timestamp was the same for both records, then you have two distinct records. What you could do is to select based on the MIN timestamp (there are may ways to do this).

        Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world." Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that." Deja View - the feeling that you've seen this post before.

        1 Reply Last reply
        0
        • M matthias s 0

          Hi there, I've got a Messages table which looks roughly like this: SenderID (int) RecipientID (int) TimeStamp (datetime) Body (varchar(512) Now I'd like to do a select which gets me (RecipientID = 1) the first message (depending on the timestamp) of each sender. Example: Sender ID 5 has sent two messages, the recordset should contain the oldest message of this fella. Sender ID 6 has sent five messages, the recordset should contain the oldest message of this guy as well. I thought this could be done by tying a DISTINCT to a column. I hoped I could do it somehow like this: SELECT DISTINCT(SenderID), TimeStamp, Body FROM Messages WHERE RecipientID = 1 ORDER BY TimeStamp DESC I was just guessing into the wild. Is there an easy way to do that? Thanks in advance!

          /matthias

          I love deadlines. I like the whooshing sound they make as they fly by.
          [Douglas Adams]

          P Offline
          P Offline
          Private_Void
          wrote on last edited by
          #4

          Assuming "Oldest" means been in the DB the Longest. This is more complex than first meets the eye due to the fact that you need to tie in the Oldest TimeStamp with its cooresponding Body field. SELECT T1.SenderID, T2.MIN([TimeStamp]), T2.MIN(Body) AS Body FROM Messages T1 INNER JOIN ( SELECT MIN[TimeStamp] AS MINTimeStamp ,Body FROM Messages GROUP BY Body )T2 ON T1.Body = T2.Body AND T1.[TimeStamp] = T2.[TimeStamp] WHERE RecipeintID = 1 GROUP BY T1.SenderID I did this blind, without sql around, so forgive me if it is a little off. It should be pretty close to what you are looking for if I understood what you were trying to do. SELECT T1.SenderID, MIN(TimeStamp) AS MINStamp, Body AS Body FROM @Messages T1 INNER JOIN ( SELECT GROUP BY SenderID

          1 Reply Last reply
          0
          • M matthias s 0

            Hi there, I've got a Messages table which looks roughly like this: SenderID (int) RecipientID (int) TimeStamp (datetime) Body (varchar(512) Now I'd like to do a select which gets me (RecipientID = 1) the first message (depending on the timestamp) of each sender. Example: Sender ID 5 has sent two messages, the recordset should contain the oldest message of this fella. Sender ID 6 has sent five messages, the recordset should contain the oldest message of this guy as well. I thought this could be done by tying a DISTINCT to a column. I hoped I could do it somehow like this: SELECT DISTINCT(SenderID), TimeStamp, Body FROM Messages WHERE RecipientID = 1 ORDER BY TimeStamp DESC I was just guessing into the wild. Is there an easy way to do that? Thanks in advance!

            /matthias

            I love deadlines. I like the whooshing sound they make as they fly by.
            [Douglas Adams]

            P Offline
            P Offline
            Private_Void
            wrote on last edited by
            #5

            Assuming "Oldest" means been in the DB the Longest. And I understood what you are trying to do this should be pretty close. I did this without a SQL Server so forgive if a little off. This is more complex than first meets the eye due to the fact that you need to tie in the Oldest TimeStamp with its cooresponding Body field. SELECT T1.SenderID, T2.MIN([TimeStamp]), T2.MIN(Body) AS Body FROM Messages T1 INNER JOIN ( SELECT MIN[TimeStamp] AS MINTimeStamp ,Body FROM Messages GROUP BY Body )T2 ON T1.Body = T2.Body AND T1.[TimeStamp] = T2.[TimeStamp] WHERE RecipeintID = 1 GROUP BY T1.SenderID

            1 Reply Last reply
            0
            • M matthias s 0

              Hi there, I've got a Messages table which looks roughly like this: SenderID (int) RecipientID (int) TimeStamp (datetime) Body (varchar(512) Now I'd like to do a select which gets me (RecipientID = 1) the first message (depending on the timestamp) of each sender. Example: Sender ID 5 has sent two messages, the recordset should contain the oldest message of this fella. Sender ID 6 has sent five messages, the recordset should contain the oldest message of this guy as well. I thought this could be done by tying a DISTINCT to a column. I hoped I could do it somehow like this: SELECT DISTINCT(SenderID), TimeStamp, Body FROM Messages WHERE RecipientID = 1 ORDER BY TimeStamp DESC I was just guessing into the wild. Is there an easy way to do that? Thanks in advance!

              /matthias

              I love deadlines. I like the whooshing sound they make as they fly by.
              [Douglas Adams]

              P Offline
              P Offline
              Private_Void
              wrote on last edited by
              #6

              Assuming "Oldest" means been in the DB the Longest. And I understood what you are trying to do this should be pretty close. I did this without a SQL Server so forgive if a little off. SELECT T1.SenderID, T2.MIN([TimeStamp]), T2.MIN(Body) AS Body FROM Messages T1 INNER JOIN ( SELECT MIN[TimeStamp] AS MINTimeStamp ,Body FROM Messages GROUP BY Body )T2 ON T1.Body = T2.Body AND T1.[TimeStamp] = T2.[TimeStamp] WHERE RecipeintID = 1 GROUP BY T1.SenderID

              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