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. Get a single record for each user

Get a single record for each user

Scheduled Pinned Locked Moved Database
database
10 Posts 6 Posters 2 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
    Meax
    wrote on last edited by
    #1

    I have the following SQL table, ID LogDate Name Department ----------------------------------------------- 1 3/26/2015 7:55 AM Adam HR 2 3/26/2015 7:53 AM Tony Fin 3 3/26/2015 7:59 AM Mark Mang 1 3/26/2015 8:15 AM Adam HR 3 3/26/2015 8:10 AM Mark Mang 2 3/26/2015 7:53 AM Tony Fin i like to get this output ID LogDate Name Department ------------------------------------------------- 2 3/26/2015 7:53 AM Tony Fin 1 3/26/2015 7:55 AM Adam HR 3 3/26/2015 7:59 AM Mark Mang i want to get a single record for each user ordered by min LogDate. my table name is "DeviceLogs"

    A S C U M 5 Replies Last reply
    0
    • M Meax

      I have the following SQL table, ID LogDate Name Department ----------------------------------------------- 1 3/26/2015 7:55 AM Adam HR 2 3/26/2015 7:53 AM Tony Fin 3 3/26/2015 7:59 AM Mark Mang 1 3/26/2015 8:15 AM Adam HR 3 3/26/2015 8:10 AM Mark Mang 2 3/26/2015 7:53 AM Tony Fin i like to get this output ID LogDate Name Department ------------------------------------------------- 2 3/26/2015 7:53 AM Tony Fin 1 3/26/2015 7:55 AM Adam HR 3 3/26/2015 7:59 AM Mark Mang i want to get a single record for each user ordered by min LogDate. my table name is "DeviceLogs"

      A Offline
      A Offline
      Afzaal Ahmad Zeeshan
      wrote on last edited by
      #2

      A simple solution would be,

      SELECT TOP 1 * FROM DeviceLogs ORDER BY LogDate;

      Which will select the top 1 record from your records, ordering them by your column containing LogData, you can add or remove DESC at the end of the ORDER BY clause if it returns the opposite side and so on.

      The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

      M 1 Reply Last reply
      0
      • A Afzaal Ahmad Zeeshan

        A simple solution would be,

        SELECT TOP 1 * FROM DeviceLogs ORDER BY LogDate;

        Which will select the top 1 record from your records, ordering them by your column containing LogData, you can add or remove DESC at the end of the ORDER BY clause if it returns the opposite side and so on.

        The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

        M Offline
        M Offline
        Meax
        wrote on last edited by
        #3

        then is selects just a single record. i want records of all users to be selected but without repeating. if userid 1 has more than one log then just select earliest record for user id 1 and ignore other records for him.

        A 1 Reply Last reply
        0
        • M Meax

          then is selects just a single record. i want records of all users to be selected but without repeating. if userid 1 has more than one log then just select earliest record for user id 1 and ignore other records for him.

          A Offline
          A Offline
          Afzaal Ahmad Zeeshan
          wrote on last edited by
          #4

          Then you should group your records using GROUP BY clause over your LogTime column.

          The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

          M 1 Reply Last reply
          0
          • A Afzaal Ahmad Zeeshan

            Then you should group your records using GROUP BY clause over your LogTime column.

            The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

            M Offline
            M Offline
            Meax
            wrote on last edited by
            #5

            SELECT * FROM DeviceLogs GROUP BY LogDate this gives me error "because it is not contained in either an aggregate function or the GROUP BY clause"

            R 1 Reply Last reply
            0
            • M Meax

              SELECT * FROM DeviceLogs GROUP BY LogDate this gives me error "because it is not contained in either an aggregate function or the GROUP BY clause"

              R Offline
              R Offline
              RUs123
              wrote on last edited by
              #6

              Well you select all columns and group by LogDate. Group by makes a single row out of all the rows that have the same LogDate in this example. But what information is it supposed to put in ? That's what aggregate functions are for. However I think you should group by name, where LogDate=the day you want to select by. You could also group by LogDate and name. Anything that isn't in group by must be in an aggregate function. So you have to decide which time you want to have selected (from what I deducted, first log ? ). A bit unrelated to your question, but why do you have ID's that repeat ?

              1 Reply Last reply
              0
              • M Meax

                I have the following SQL table, ID LogDate Name Department ----------------------------------------------- 1 3/26/2015 7:55 AM Adam HR 2 3/26/2015 7:53 AM Tony Fin 3 3/26/2015 7:59 AM Mark Mang 1 3/26/2015 8:15 AM Adam HR 3 3/26/2015 8:10 AM Mark Mang 2 3/26/2015 7:53 AM Tony Fin i like to get this output ID LogDate Name Department ------------------------------------------------- 2 3/26/2015 7:53 AM Tony Fin 1 3/26/2015 7:55 AM Adam HR 3 3/26/2015 7:59 AM Mark Mang i want to get a single record for each user ordered by min LogDate. my table name is "DeviceLogs"

                S Offline
                S Offline
                Simon_Whale
                wrote on last edited by
                #7

                Are your Logdates varchars?

                Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

                1 Reply Last reply
                0
                • M Meax

                  I have the following SQL table, ID LogDate Name Department ----------------------------------------------- 1 3/26/2015 7:55 AM Adam HR 2 3/26/2015 7:53 AM Tony Fin 3 3/26/2015 7:59 AM Mark Mang 1 3/26/2015 8:15 AM Adam HR 3 3/26/2015 8:10 AM Mark Mang 2 3/26/2015 7:53 AM Tony Fin i like to get this output ID LogDate Name Department ------------------------------------------------- 2 3/26/2015 7:53 AM Tony Fin 1 3/26/2015 7:55 AM Adam HR 3 3/26/2015 7:59 AM Mark Mang i want to get a single record for each user ordered by min LogDate. my table name is "DeviceLogs"

                  C Offline
                  C Offline
                  Corporal Agarn
                  wrote on last edited by
                  #8

                  Would this help?

                  SELECT ID, MIN(LogDate) AS LogDate, Name, Department
                  FROM yourtable
                  GROUP BY ID, Name, Department;

                  Mongo: Mongo only pawn... in game of life.

                  1 Reply Last reply
                  0
                  • M Meax

                    I have the following SQL table, ID LogDate Name Department ----------------------------------------------- 1 3/26/2015 7:55 AM Adam HR 2 3/26/2015 7:53 AM Tony Fin 3 3/26/2015 7:59 AM Mark Mang 1 3/26/2015 8:15 AM Adam HR 3 3/26/2015 8:10 AM Mark Mang 2 3/26/2015 7:53 AM Tony Fin i like to get this output ID LogDate Name Department ------------------------------------------------- 2 3/26/2015 7:53 AM Tony Fin 1 3/26/2015 7:55 AM Adam HR 3 3/26/2015 7:59 AM Mark Mang i want to get a single record for each user ordered by min LogDate. my table name is "DeviceLogs"

                    U Offline
                    U Offline
                    User 11524996
                    wrote on last edited by
                    #9

                    SELECT d.ID, d.LogDate, d.Name, d.Department
                    FROM DeviceLogs d, (SELECT MIN(LogDate) FROM DeviceLogs GROUP BY Name) s
                    WHERE s.MIN(LogDate) = d.LogDate
                    ORDER BY d.LogDate;

                    SELECT ID, MIN(LogDate) AS LogDate, Name, Department
                    FROM yourtable
                    GROUP BY ID, Name, Department;

                    1 Reply Last reply
                    0
                    • M Meax

                      I have the following SQL table, ID LogDate Name Department ----------------------------------------------- 1 3/26/2015 7:55 AM Adam HR 2 3/26/2015 7:53 AM Tony Fin 3 3/26/2015 7:59 AM Mark Mang 1 3/26/2015 8:15 AM Adam HR 3 3/26/2015 8:10 AM Mark Mang 2 3/26/2015 7:53 AM Tony Fin i like to get this output ID LogDate Name Department ------------------------------------------------- 2 3/26/2015 7:53 AM Tony Fin 1 3/26/2015 7:55 AM Adam HR 3 3/26/2015 7:59 AM Mark Mang i want to get a single record for each user ordered by min LogDate. my table name is "DeviceLogs"

                      M Offline
                      M Offline
                      Meax
                      wrote on last edited by
                      #10

                      thank you all

                      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