April 3, 2010 at 4:49 am
Let say I have a table Employee which have 3 Fields
EmpName
EmpComments
EmpCommentsDate
and Have the following data
Nm CM Date
AA112/12/2009 12:05:00 AM
AA212/12/2009 12:04:00 AM
BB112/12/2009 12:02:00 AM
CC112/12/2009 12:00:00 AM
I want to execute this but getting error,
SELECT DISTINCT TOP (2) EmpName
FROM Employee
ORDER BY EmpCommentsDate
April 3, 2010 at 5:21 am
It's not exactly clear what output you expect here - especially since there aren't any duplicates in the test data.
The problem, though, is one of logic: you want the top 2 records, in date order, but you also want distinct employee names. Should that return 2 records always?
Anyway, to get the discussion going, here is your test data, with one solution:
DECLARE @Employee
TABLE (
EmpName VARCHAR(50) NOT NULL,
EmpComments VARCHAR(MAX) NULL,
EmpCommentsDate DATETIME NOT NULL
);
INSERT @Employee
(EmpName, EmpComments, EmpCommentsDate)
VALUES ('A', 'A1', '2009-12-12 12:05:00.000');
INSERT @Employee
(EmpName, EmpComments, EmpCommentsDate)
VALUES ('B', 'B1', '2009-12-12 12:02:00.000');
INSERT @Employee
(EmpName, EmpComments, EmpCommentsDate)
VALUES ('C', 'C1', '2009-12-12 12:00:00.000');
SELECT DISTINCT
TOP (2)
EmpName,
EmpCommentsDate
FROM @Employee
ORDER BY
EmpCommentsDate,
EmpName;
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
April 3, 2010 at 5:54 am
Thanks Paul,
I am expecting the output
A
B
Your Query is using Distinct Date which is not desired
I want the Query to Run is
SELECT DISTINCT TOP (2) EmpName
FROM Employee
ORDER BY EmpCommentsDate desc
I want to check Latest Comments from different Users
April 3, 2010 at 6:14 am
abcim (4/3/2010)
I want the Query to Run isSELECT DISTINCT TOP (2) EmpName
FROM Employee
ORDER BY EmpCommentsDate desc
I want to check Latest Comments from different Users
I'm happy to write SQL for you, but we do need to get the rules nailed down 😉
The SQL quoted above is not valid, because it is logically imprecise.
You say you want the latest comments, but that query returns only the employee name :blink:
Your expected output (A & B) also does not include comments.
I imagine what you want is:
The two employees that are associated with the most recent comments added. You always want to see two different employees. Is that correct?
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
April 3, 2010 at 6:18 am
If my guess is correct, try:
SELECT TOP (2)
EmpName,
MAX(EmpCommentsDate)
FROM @Employee
GROUP BY
EmpName
ORDER BY
MAX(EmpCommentsDate) DESC;
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
April 3, 2010 at 6:20 am
The two employees that are associated with the most recent comments added. You always want to see two different employees. Is that correct?
Exactly I want the two different Latest Employee Names.
April 3, 2010 at 6:31 am
abcim (4/3/2010)
The two employees that are associated with the most recent comments added. You always want to see two different employees. Is that correct?
Exactly I want the two different Latest Employee Names.
Oh good. Try the last query I posted then, hopefully that will work for you?
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
April 3, 2010 at 7:27 am
Thanks you very much Paul White NZ.
It works
April 3, 2010 at 7:29 am
Thanks for the feedback.
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply