Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am not a PRO with SQL, so guys.. I need you help.
I might be laking in basic knowledge here.. but plz help..

I have 2 tables, Master(users) and Transaction(logEntries).

Linked with usrSrNo (index for Users). Log Entries have Transaction logs.

Now I want to Display Distinct Users, based on the logDateTime Desendingly.

As per My Knowledge if I query,

SELECT DISTINCT u.usrName,l.logDateTime FROM users u
JOIN logEntries l on u.usrSrNo = l.usrSrNo
ORDER BY l.logDateTime DESC

Returns me user listing Sorted according to LogDateTime descending, but they are not distict. How can I Get distinct listing?

Plz Help.

Nik
Posted

I think you need to select the top date, to make it return one date per user. I dont know if max works for dates ? A group by clause is tge other thing, select the highest date Nd group by user.
 
Share this answer
 
Comments
ImNAM 28-May-10 15:23pm    
I tried selecting max(logDate) (and aslo TOP 1) ..
When I try to implement order by on logDate, it fails and gives me some multiple user listing.
Also when I want to implement WHERE clasuse on logDate, it fails..
again, I want to describe the table structure..

User Table
-----------
usrSrNo INT, usrName VARCHAR

logTable
----------
logSrNo INT, logType, logDateTime, usrSrNo

logTable can have multiple entries for same user on same date.
I want to show list of users based on selection like, today, this week, this Month (they should be sorted by logDate DESC)

I also tried with creating View with joint on both tables, It works when I dont have to mention date range, but when I mentione date range in WHERE clause, it fails. as I want to order by, I need to mention logDate in select.. and once I mention it into select.. it returns multiple values.
You are setting DISTINCT on username and then on logDateTime.

You can group by username and then put a distinct on logDateTime.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900