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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Distinct in join - please help

Distinct in join - please help

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelpquestion
3 Posts 2 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.
  • X Offline
    X Offline
    xaphod
    wrote on last edited by
    #1

    Hi, Whenever a user modifies a customers data I create a log with only the id’s(int) and the datemodified(datetime). Then I want to view the 10 latest customers that the user modified, sorted by datemodyfied and with unique customernames. So I want to select Customers.customerid and Customers.customername sorted by entries in table CustomerLogs, sorted by CustomerLogs.datemodified. No doubles of customername allowed. I’ve tried some different joins but nothing works exactly as I want. In a WHERE clause I have author_userid=2 AND owner_companyid=1 Can somebody give some advice, please? I use SQL Server 2000 TABLE Customers Customerid | customername TABLE CustomerLogs customerlogid | customerid | author_userid | datemodified | owner_companyid

    M 1 Reply Last reply
    0
    • X xaphod

      Hi, Whenever a user modifies a customers data I create a log with only the id’s(int) and the datemodified(datetime). Then I want to view the 10 latest customers that the user modified, sorted by datemodyfied and with unique customernames. So I want to select Customers.customerid and Customers.customername sorted by entries in table CustomerLogs, sorted by CustomerLogs.datemodified. No doubles of customername allowed. I’ve tried some different joins but nothing works exactly as I want. In a WHERE clause I have author_userid=2 AND owner_companyid=1 Can somebody give some advice, please? I use SQL Server 2000 TABLE Customers Customerid | customername TABLE CustomerLogs customerlogid | customerid | author_userid | datemodified | owner_companyid

      M Offline
      M Offline
      Michael Potter
      wrote on last edited by
      #2

      I think it may look something like this:

      SELECT TOP 10
          Customers.customerid,
          Customers.customername,
          Log.LastModified
      FROM
          Customers
      INNER JOIN
          (SELECT customerId, Max(dateModified) AS LastModified
           FROM CustomerLogs
           GROUP BY customerId) as Log
          ON (Customers.customerId = Log.customerId)
      ORDER BY LastModified DESC
      

      If your log is large - you will want an index on (customerId, dateModified)

      X 1 Reply Last reply
      0
      • M Michael Potter

        I think it may look something like this:

        SELECT TOP 10
            Customers.customerid,
            Customers.customername,
            Log.LastModified
        FROM
            Customers
        INNER JOIN
            (SELECT customerId, Max(dateModified) AS LastModified
             FROM CustomerLogs
             GROUP BY customerId) as Log
            ON (Customers.customerId = Log.customerId)
        ORDER BY LastModified DESC
        

        If your log is large - you will want an index on (customerId, dateModified)

        X Offline
        X Offline
        xaphod
        wrote on last edited by
        #3

        Thanks Michael! You really made my day! Your solution works excellent!:-D Have a nice day!

        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