Thank you for your reply. We are getting the data from no of tables. Because of that I am unable to post the structure. I need to know how to write the condition. Please guide me on this.
And we need to know your tables to help you accurately. From what you said, well,
SELECT *
FROM Employees
WHERE (SELECT SUM(datediff(minute, O.fromtime, O.totime))
FROM Orders O
WHERE O.EmployeeID = E.EmployeeID
AND O.fromtime >= E.fromtime
AND O.totime <= E.totime) >= 240
But if this is anywhere remotely close to your tables, I would be highly surprised.
For this type of problem, you help yourself immensel if you post:
1) CREATE TABLE statements for your tables, preferrably simplified for the pertinent parts of the problem.
2) INSERT statements with sample data.
3) The desired result given the sample.
4) A short description of the business rules.
5) Which version of SQL Server.