Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8087181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:41:30+00:00 2026-06-05T18:41:30+00:00

I’ve been trying to understate the best way to write an MS SQL 2005

  • 0

I’ve been trying to understate the best way to write an MS SQL 2005 query which pretty much does the following…

select distinct col1, col2, col3
from table1

Basically I want to perform Distinct on col1 but I dont want Dististinc on Col2/3, I just want there values.

I understand its not possible to write the query this way as I think I read the Distinct is applied to the row and not the col?

Could anyone please point me in the right direction? I did try right joing the values back in but this didnt work as I had to specify the extra joined cols in the distinct select which in turn performed the distinct on these, i.e..

select distinct t1.col1, t2.col2, t3.col3
from table1 t1
right join (select col1, col2, col3 from table1) t2
on t1.col1 = t2.col1

Edited to explain better..

select distinct t1.Hostname, t2.IP, t2.ActionDateTime, t2.Action
from tblUserActions t1
right join (select Hostname, IP, ActionDateTime from tblUserActions) t2
on t1.Hostname = t2.Hostname

Basically this table is a list of thousands of user actions and im trying to list distinct on the Hostname so I should only receive say 10 rows as thats how many different Hostnames there are. Then based on these hostnames I want to also join the most recent record data to the rows returned, so I’d like to return:

  Hostname, IP, ActionDateTime, Action
1 Host1, 165.123.123.1, 2012-06-14 02:07:08, Logon
2 Host2, 165.123.123.2, 2012-06-14 03:07:08, Logoff
3 Host3, 165.123.123.3, 2012-06-14 04:07:08, Logon
4 Host4, 165.123.123.4, 2012-06-14 05:07:08, Logoff
etc...

Any help/pointers would be great! Cheers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:41:32+00:00Added an answer on June 5, 2026 at 6:41 pm

    By the sounds of it I think this is what you are after:

    WITH CTE AS
    (   SELECT  HostName,
                IP,
                ActionDate,
                Action,
                ROW_NUMBER() OVER(PARTITION BY HostName ORDER BY ActionDate DESC) AS RowNumber
        FROM    Table
    )
    SELECT  HostName,
            IP,
            ActionDate,
            Action
    FROM    CTE
    WHERE   RowNumber = 1
    

    This will return only unique values for host name, then the values returned the other columns are based on the ORDER BY clause in the ROW_NUMBER() Windowed function.

    You may need to alter the ORDER BY to suit your exact reqirements, I assumed latest action was probably the most likely.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.