Question

My Employee Table

EMPLOYEEID, EMPLOYEENAME

My CallReportEmployee Table

ReportID, EMPLOYEEID

My Report Table

ReportID , ReportName

When I want to get is I want to show all the employee name, when those didnt choose ReportID 1

1 report can consist of many employees

How can I such outcome is I want to list out all the Employee with ReportID one and not 1?

EMPLOYEENAME    REPORTID
Roger              1
Peter              1
Shawn             NULL
Peter             NULL

I try this, it didn't work

SELECT rc.EmployeeID, rc.ReportID, e.FirstName
FROM employee e
LEFT JOIN callreportemployee rc ON e.ID = rc.EmployeeID

it just show all the employee

EMPLOYEENAME    REPORTID
Roger             Null
Peter              1
Peter              2
Shawn              3
Shawn              2
John             NULL

if i add in in a where rc.ReportID = 1 it become show Peter only

How can I achieve the format I want? I don't want duplicate Name

Était-ce utile?

La solution

How can I achieve the format I want? I don't want duplicate Name

Use the GROUP BY clause to aggregate the duplicate names, as in this query:

SELECT U.Id as [User Link], Count(Pt.Id) AS ANSWERS
FROM Users U, Posts P, PostHistory Ph, PostTypes Pt
WHERE
Pt.Id = Ph.PostHistoryTypeId
AND
U.Id = P.OwnerUserId
AND
P.Id = Ph.PostId
AND
Pt.Id = 2
GROUP BY U.Id
HAVING Count(Pt.Id) > 2000
ORDER BY ANSWERS DESC

References

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top