Left join with subquery

SELECT A.EmpId,A.IncidentDate FROM EmployeePoints1 as A WHERE IncidentDate= (SELECT MAX(IncidentDate) FROM EmployeePoints1 WHERE EmpId = A.EmpId) AND (DATEADD(day,28,DATEADD(WEEK, DATEDIFF(WEEK, 0,A.IncidentDate), 0)) < DATEADD(WEEK, DATEDIFF(WEEK, 0,GetDate()), 0)) AND (A.IncidentCode = 'I' OR A.IncidentCode = 'A') LEFT JOIN EmployeeTotalPoints1 ON EmployeeTotalPoints1.EmpId = A.EmpId

I am trying to left join another table but  I got

Incorrect syntax near the keyword 'LEFT'.

August 24th, 2015 1:17pm

Can you post the structure of your tables, some input data and desired result? It is not clear what you're trying to do from the incorrectly written query.
Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 1:24pm

The JOIN is part of the FROM clause.

SELECT
    A.EmpId,A.IncidentDate
FROM
    EmployeePoints1 as A
    LEFT JOIN 
    EmployeeTotalPoints1 
    ON EmployeeTotalPoints1.EmpId = A.EmpId
WHERE
    A.IncidentDate = (
    SELECT MAX(IncidentDate)
    FROM EmployeePoints1
    WHERE EmpId = A.EmpId
    ) 
    AND (DATEADD(day,28,DATEADD(WEEK, DATEDIFF(WEEK, 0,A.IncidentDate), 0)) < DATEADD(WEEK, DATEDIFF(WEEK, 0,GetDate()), 0)) 
    AND (A.IncidentCode = 'I' OR A.IncidentCode = 'A');

August 24th, 2015 1:45pm

How does the date manipulations translate? Also, what is the point of joining with the extra table if we're not selecting any columns from it?
Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 1:55pm

SELECT A.EmpId, A.IncidentDate
FROM EmployeePoints1 AS A
LEFT JOIN EmployeeTotalPoints1 ON EmployeeTotalPoints1.EmpId = A.EmpId
WHERE A.IncidentDate =
	(
		SELECT MAX(IncidentDate)
		FROM EmployeePoints1
		WHERE EmpId = A.EmpId
	) 
AND DATEADD(day, 28, DATEADD(WEEK, DATEDIFF(WEEK, 0, A.IncidentDate), 0)) < DATEADD(WEEK, DATEDIFF(WEEK, 0, GetDate()), 0)
AND (A.IncidentCode = 'I' OR A.IncidentCode = 'A')

August 24th, 2015 2:11pm

Please follow basic Netiquette and post the DDL we need to answer this. We need to know the data types, keys and constraints on the table. Avoid dialect in favor of ANSI/ISO Standard SQL. 

What are you trying to do? We have no specs to go with the lack of DDL! If your boss treats you like this, it is time to quit. 
My guess would use a Calendar table that includes the ISO week_date. Here is a skeleton: 

SELECT EP.emp_id, MAX(EP.incident_date) AS report_incident_date
  FROM Employee_Points_1 AS EP,
       Calendar AS C 
 WHERE EP.incident_code IN ('I', 'A') 
   AND << something with calendar>>;

Want to be polite, follow forum rules and try again? 

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 2:24pm

SELECT A.EmpId,A.IncidentDate
FROM EmployeePoints1 as A
  LEFT JOIN EmployeeTotalPoints1 ETP
         ON ETP.EmpId = A.EmpId
WHERE IncidentDate= (SELECT MAX(IncidentDate) FROM EmployeePoints1 WHERE EmpId = A.EmpId) 
  AND (DATEADD(day,28,DATEADD(WEEK, DATEDIFF(WEEK, 0,A.IncidentDate), 0)) < DATEADD(WEEK, DATEDIFF(WEEK, 0,GetDate()), 0)) 
  AND (A.IncidentCode = 'I' OR A.IncidentCode = 'A')

Please re-arrange your query. JOIN comes right after the MainTable and before the where condition.

found some online article for your ref: http://www.sql-join.com/ 

August 24th, 2015 2:35pm

Sorry,

These are my tables

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[EmployeePoints1](
	[EmpId] [varchar](10) NOT NULL,
	[IncidentDate] [datetime] NOT NULL,
	[IncidentCode] [varchar](10) NOT NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO




SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[EmployeeTotalPoints1](
	[EmpId] [varchar](50) NULL,
	[TotalPoints] [float] NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


  • Edited by Lio1972 10 hours 7 minutes ago
Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 5:00pm

Can you also post some data and desired output? What did you want to achieve with your query?

Also, I see in the tables you posted that EmpId is defined as varchar(10) in the first table and varchar(50) NULL in the second. I think you need to have the same EmpId varchar(10) in the EmployeeTotalPoints and also both columns should be not null. You need to add a primary key to your second table (it could be identity field). You also need an Employees table and relate both of your Points tables to that table (e.g. EmpId will be a foreign key).

August 24th, 2015 5:32pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics