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 can a get top each event in my related table

how can a get top each event in my related table

Scheduled Pinned Locked Moved Database
help
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
    mhd sbt
    wrote on last edited by
    #1

    hi to all ho can i get top 1 from my table. i have a event log table that 3 event inserted in it i want to get top 1 each event by desc in it . my code is but this code is wrong and correct code probably similar to this code

    SELECT [Guid] ,
    UserGuid ,
    ReferenceGuid ,
    ReferenceID ,
    [Event] ,
    Comments ,
    [Time] ,
    IPAddress
    FROM EventLogs
    WHERE ( [Guid] = ( SELECT TOP ( 1 )
    [Guid]
    FROM EventLogs AS elog
    WHERE ( ReferenceGuid = EventLogs.ReferenceGuid )
    ORDER BY Time DESC
    ) )

    thanks for any help

    C M E 3 Replies Last reply
    0
    • M mhd sbt

      hi to all ho can i get top 1 from my table. i have a event log table that 3 event inserted in it i want to get top 1 each event by desc in it . my code is but this code is wrong and correct code probably similar to this code

      SELECT [Guid] ,
      UserGuid ,
      ReferenceGuid ,
      ReferenceID ,
      [Event] ,
      Comments ,
      [Time] ,
      IPAddress
      FROM EventLogs
      WHERE ( [Guid] = ( SELECT TOP ( 1 )
      [Guid]
      FROM EventLogs AS elog
      WHERE ( ReferenceGuid = EventLogs.ReferenceGuid )
      ORDER BY Time DESC
      ) )

      thanks for any help

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

      How about something like this:

      SELECT [Guid] ,
      UserGuid ,
      ReferenceGuid ,
      ReferenceID ,
      [Event] ,
      Comments ,
      [Time] ,
      IPAddress
      FROM EventLogs AS elog
      INNER JOIN (
      SELECT [Guid]
      FROM EventLogs elog1
      INNER JOIN (
      SELECT [Event], MAX([Time]) AS MTime
      FROM EventLogs
      GROUP BY [Event] ) AS t ON elog1.[Event] = t.[Event] AND elog1.[Time] = t.MTime
      ) AS elog3 ON elog.[Guid] = elog3.[Guid]

      M 1 Reply Last reply
      0
      • M mhd sbt

        hi to all ho can i get top 1 from my table. i have a event log table that 3 event inserted in it i want to get top 1 each event by desc in it . my code is but this code is wrong and correct code probably similar to this code

        SELECT [Guid] ,
        UserGuid ,
        ReferenceGuid ,
        ReferenceID ,
        [Event] ,
        Comments ,
        [Time] ,
        IPAddress
        FROM EventLogs
        WHERE ( [Guid] = ( SELECT TOP ( 1 )
        [Guid]
        FROM EventLogs AS elog
        WHERE ( ReferenceGuid = EventLogs.ReferenceGuid )
        ORDER BY Time DESC
        ) )

        thanks for any help

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        Or you can use row_number, can't remember the syntax but there are good examples in BOL.

        Never underestimate the power of human stupidity RAH

        M 1 Reply Last reply
        0
        • C Corporal Agarn

          How about something like this:

          SELECT [Guid] ,
          UserGuid ,
          ReferenceGuid ,
          ReferenceID ,
          [Event] ,
          Comments ,
          [Time] ,
          IPAddress
          FROM EventLogs AS elog
          INNER JOIN (
          SELECT [Guid]
          FROM EventLogs elog1
          INNER JOIN (
          SELECT [Event], MAX([Time]) AS MTime
          FROM EventLogs
          GROUP BY [Event] ) AS t ON elog1.[Event] = t.[Event] AND elog1.[Time] = t.MTime
          ) AS elog3 ON elog.[Guid] = elog3.[Guid]

          M Offline
          M Offline
          mhd sbt
          wrote on last edited by
          #4

          thanks for your answer this problem has been fix with some few change like this:

          SELECT [Guid] ,
          UserGuid ,
          ReferenceGuid ,
          ReferenceID ,
          [Event] ,
          Comments ,
          [Time] ,
          IPAddress
          FROM EventLogs
          WHERE ( [Guid] = ( SELECT TOP ( 1 )
          [Guid]
          FROM EventLogs AS elog
          WHERE ( ReferenceGuid = EventLogs.ReferenceGuid AND Event=EventLogs.Event)
          ORDER BY Time DESC
          ) )

          1 Reply Last reply
          0
          • M mhd sbt

            hi to all ho can i get top 1 from my table. i have a event log table that 3 event inserted in it i want to get top 1 each event by desc in it . my code is but this code is wrong and correct code probably similar to this code

            SELECT [Guid] ,
            UserGuid ,
            ReferenceGuid ,
            ReferenceID ,
            [Event] ,
            Comments ,
            [Time] ,
            IPAddress
            FROM EventLogs
            WHERE ( [Guid] = ( SELECT TOP ( 1 )
            [Guid]
            FROM EventLogs AS elog
            WHERE ( ReferenceGuid = EventLogs.ReferenceGuid )
            ORDER BY Time DESC
            ) )

            thanks for any help

            E Offline
            E Offline
            EralperYilmaz
            wrote on last edited by
            #5

            With SQL Server 2005, we can use Row_Number() function and Partition By clause to number rows in categories So you can partition your data in Event category and filter only the rows with row number is 1 So you will get only 1 row per category

            with cte as (
            SELECT [Guid] ,
            UserGuid ,
            ReferenceGuid ,
            ReferenceID ,
            [Event] ,
            Comments ,
            [Time] ,
            IPAddress,
            rn = ROW_NUMBER() OVER (Partition By Event Order By Time Desc)
            FROM EventLogs
            )
            select * from cte where rn = 1

            Please check the following URL for similar sample code http://www.kodyaz.com/articles/top-n-random-rows-foreach-category-or-group.aspx[^]

            1 Reply Last reply
            0
            • M Mycroft Holmes

              Or you can use row_number, can't remember the syntax but there are good examples in BOL.

              Never underestimate the power of human stupidity RAH

              M Offline
              M Offline
              mhd sbt
              wrote on last edited by
              #6

              thanks yea its ok and a good solution

              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