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

SQL Select Question

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelpquestion
4 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.
  • A Offline
    A Offline
    Aptiva Dave
    wrote on last edited by
    #1

    Got myself a head scratcher here. I'm needing to pull employee names and pictures from a database, which I can do with the following select:

    SELECT dbo.vwEmployee.BadgeName, dbo.Image.Photo, dbo.vwEmployee.Updated
    FROM dbo.Image INNER JOIN
    dbo.vwEmployee ON dbo.Image.OwnerGUID = dbo.vwEmployee.OwnerGUID
    WHERE (dbo.vwEmployee.Department = 'IT')

    Problem is that the application that was used to store the employee information and photos also keeps every photo taken of the employee. I want to be able to return only the row with the most recent photo taken (or the first instance of the most recent update since some have multiple records with the same updated date). I know I can do this with the application I am writing to use this query, but I would rather let the SQL Server do most of the heavy lifting and to further narrow down my results.

    C D J 3 Replies Last reply
    0
    • A Aptiva Dave

      Got myself a head scratcher here. I'm needing to pull employee names and pictures from a database, which I can do with the following select:

      SELECT dbo.vwEmployee.BadgeName, dbo.Image.Photo, dbo.vwEmployee.Updated
      FROM dbo.Image INNER JOIN
      dbo.vwEmployee ON dbo.Image.OwnerGUID = dbo.vwEmployee.OwnerGUID
      WHERE (dbo.vwEmployee.Department = 'IT')

      Problem is that the application that was used to store the employee information and photos also keeps every photo taken of the employee. I want to be able to return only the row with the most recent photo taken (or the first instance of the most recent update since some have multiple records with the same updated date). I know I can do this with the application I am writing to use this query, but I would rather let the SQL Server do most of the heavy lifting and to further narrow down my results.

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

      Does the Image table have multiple records or multiple columns for the pictures? If record then does it have a time stamp? If so use a subquery to find the max time and pass that picture.

      1 Reply Last reply
      0
      • A Aptiva Dave

        Got myself a head scratcher here. I'm needing to pull employee names and pictures from a database, which I can do with the following select:

        SELECT dbo.vwEmployee.BadgeName, dbo.Image.Photo, dbo.vwEmployee.Updated
        FROM dbo.Image INNER JOIN
        dbo.vwEmployee ON dbo.Image.OwnerGUID = dbo.vwEmployee.OwnerGUID
        WHERE (dbo.vwEmployee.Department = 'IT')

        Problem is that the application that was used to store the employee information and photos also keeps every photo taken of the employee. I want to be able to return only the row with the most recent photo taken (or the first instance of the most recent update since some have multiple records with the same updated date). I know I can do this with the application I am writing to use this query, but I would rather let the SQL Server do most of the heavy lifting and to further narrow down my results.

        D Offline
        D Offline
        Dr Walt Fair PE
        wrote on last edited by
        #3

        If you have a date field, then use TOP 1 and ORDER BY update_date DESC.

        CQ de W5ALT

        Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

        1 Reply Last reply
        0
        • A Aptiva Dave

          Got myself a head scratcher here. I'm needing to pull employee names and pictures from a database, which I can do with the following select:

          SELECT dbo.vwEmployee.BadgeName, dbo.Image.Photo, dbo.vwEmployee.Updated
          FROM dbo.Image INNER JOIN
          dbo.vwEmployee ON dbo.Image.OwnerGUID = dbo.vwEmployee.OwnerGUID
          WHERE (dbo.vwEmployee.Department = 'IT')

          Problem is that the application that was used to store the employee information and photos also keeps every photo taken of the employee. I want to be able to return only the row with the most recent photo taken (or the first instance of the most recent update since some have multiple records with the same updated date). I know I can do this with the application I am writing to use this query, but I would rather let the SQL Server do most of the heavy lifting and to further narrow down my results.

          J Offline
          J Offline
          Jeremy Hutchinson
          wrote on last edited by
          #4

          You need to do a sub-select to get a list of the employees and the date of their most recent picture. Then you join to that on the EmployeeID (OwnerGuid) and the date.

          select c.BadgeName, a.Photo, c.Updated
          from Image a
          join (select OwnerGUID, MAX(PictureDate) PictureDate from Image group by OwnerGUID) b on a.OwnerGUID = b.OwnerGUID and a.PictureDate = b.PictureDate
          join vwEmployee c ON a.OwnerGUID = c.OwnerGUID
          where c.Department = 'IT'

          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