I have two tables:
AppWindowsEvent:
CREATE TABLE [AppWindowsEvent]
(
[idAppWindowEvent] INT IDENTITY(1,1)
, [idAppWindow] INT
, [idMill] INT
, [idEventType] INT
, [Order] INT
, CONSTRAINT PK_idAppWindowEvent PRIMARY KEY ([idAppWindowEvent])
, CONSTRAINT FK_idAppWindowEvent_AppWindow FOREIGN KEY ([idAppWindow]) REFERENCES [AppWindow]([idAppWindow])
, CONSTRAINT FK_idAppWindowEvent_EventType FOREIGN KEY ([idEventType]) REFERENCES [EventType]([idEventType])
)
Event:
CREATE TABLE [Event]
(
[idEvent] [INT] IDENTITY(1,1) NOT NULL
, [idEventType] [INT] NOT NULL
, [idEntity] [INT] NOT NULL
, CONSTRAINT PK_IdEvent PRIMARY KEY([idEvent])
, CONSTRAINT [FK_Event_EventType] FOREIGN KEY([idEventType]) REFERENCES [EventType] ([idEventType])
)
THE PROBLEM:
In the table I have sets of Events for certain filters ( idAppWindow
and idMill
), for each combination, the data set can vary, and in Event
, I have N number of Event records (of which some can be like no in the AppWindowEvent table), in the Event table it is identified by idEntity.
So, if for a idAppWindow
and a idMill
, I want to know what events I have with value (and those that have no value) in the Event
table, this would be solved easily with a LEFT JOIN
, but I do not get the result I want.
In the first case it is as if I made a INNER JOIN
, it does not return NULLS values, and in the second case (when the ON
of JOIN
I add a filter of idEntity
works perfect, returns the amount of Events that are configured for idMill
and idAppWindow
, and the values corresponded in Event
, those that do not, is because they are null records).
When I execute this query:
SELECT
*
FROM
AppWindowsEvent AWE
LEFT JOIN Event E ON AWE.idEventType = E.idEventType
WHERE
AWE.idMill = 1
AND AWE.idAppWindow = 1
ORDER BY
AWE.[Order] ASC
The result does not return null values.
But when I execute this other query:
SELECT
*
FROM
AppWindowsEvent AWE
LEFT JOIN Event E ON AWE.idEventType = E.idEventType
AND E.[idEntity] = 1234
WHERE
AWE.idMill = 1
AND AWE.idAppWindow = 1
ORDER BY
AWE.[Order] ASC
The result returns null records.
NOTE: Although the second query is running, it does so for a idEntity
specific, in my case I need all idEntity
.