0

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

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
Ryan
  • 155
  • 1
  • 5
  • 19
  • 1
    If I understand your question right you want the values with NULL and 1? If so, just change the WHERE clause to an extra condition in the ON – Jaloopa Jun 21 '13 at 09:00
  • can I get values of 3 & 2? can I make them appear as NULL? – Ryan Jun 21 '13 at 09:16

1 Answers1

0

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

Community
  • 1
  • 1
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265