INNER JOIN IN MULTIPLE COLUMNS - sql

img
Hi guys.
I'm developing a clinic management program, I had to join two tables TB_DOCTORS, and TB_SPECIALTY. the problem is; every doctor have more 4 specialties. How can I join the TB_DOCTORS table with more than a specialty

SELECT TB_DOCTORS.*,
S1.SPECIALITY_NAME AS SPECIALITY_NAME1,
S2.SPECIALITY_NAME AS SPECIALITY_NAME2,
S3.SPECIALITY_NAME AS SPECIALITY_NAME3,
S4.SPECIALITY_NAME AS SPECIALITY_NAME4
FROM TB_DOCTORS
LEFT OUTER JOIN TB_SPECIALTY S1 ON TB_DOCTORS.SPEC_ID1 = S1.SPECIALITY_ID
LEFT OUTER JOIN TB_SPECIALTY S2 ON TB_DOCTORS.SPEC_ID2 = S2.SPECIALITY_ID
LEFT OUTER JOIN TB_SPECIALTY S3 ON TB_DOCTORS.SPEC_ID3 = S3.SPECIALITY_ID
LEFT OUTER JOIN TB_SPECIALTY S4 ON TB_DOCTORS.SPEC_ID4 = S4.SPECIALITY_ID

Related

Stored Procedure (T-SQL) Join multiple tables

I'm currently working on a t-sql query in Microsoft SQL Server Management Studio (SQL Server), which should gather data over several tables. In the end I'll need [OfferId] and [Label]. Do you have an idea on how to write those 4 query statements into 1?
SELECT a.OfferId AS [OfferId], a.OfferDataId AS [OfferDataId], b.DeliveryModelPoolId AS [DeliveryModelPoolId]
FROM [Offer].[Offer] a
INNER JOIN [Offer].[OfferData] b
ON a.OfferDataId = b.OfferDataId
OfferId | OfferDataId | DeliveryModelPoolId
1..........| 1..................| 4
SELECT a.DeliveryModelPoolId AS [DeliveryModelPoolId], b.PoolId AS [PoolId]
FROM [Offer].[OfferData] a
INNER JOIN [Offer].[Pool] b
ON a.DeliveryModelPoolId = b.PoolId
DeliveryModelPoolId | PoolId
4................................| 4
SELECT a.DeliveryModelId AS [DeliveryModelId]
FROM [Offer].[Delivery] a
INNER JOIN [Offer].[Pool] b
ON a.DeliveryModelPoolId = b.PoolId
DeliveryModelId
2
6
SELECT a.Label AS [Label]
FROM [Offer].[DeliveryModel] a
INNER JOIN [Offer].[DeliveryLabels] b
ON a.DeliveryModelId = b.DeliveryModelId
Label
Service Center
Delivery By Car
Thanks a lot! :)
If you are planning to reuse the query, I would put it into a view:
CREATE VIEW vWOfferData
AS
SELECT a.offerID, dl.label
FROM [Offer].[Offer] a
INNER JOIN [Offer].[OfferData] b
ON a.OfferDataId = b.OfferDataId
INNER JOIN [Offer].[Pool] p2
ON b.DeliveryModelPoolId = p2.PoolId
INNER JOIN [Offer].[Delivery] d3
ON d3.DeliveryModelPoolId = p2.PoolId
INNER JOIN [Offer].[DeliveryModel] dl
ON dl.DeliveryModelId = d3.DeliveryModelId
You can then use it as a table. For example:
SELECT * FROM vWOfferData
You can join more than once in a query
select a.offerID, dl.label
FROM [Offer].[Offer] a
INNER JOIN [Offer].[OfferData] b
ON a.OfferDataId = b.OfferDataId
inner join [Offer].[Pool] p2
ON b.DeliveryModelPoolId = p2.PoolId
inner join [Offer].[Delivery] d3
ON d3.DeliveryModelPoolId = p2.PoolId
inner join [Offer].[DeliveryModel] dl
on dl.DeliveryModelId = d3.DeliveryModelId

How can I INNER JOIN another two columns to a table?

I have an SQL statement:
SELECT * FROM newsTable INNER JOIN picTable on picTable.picID = newsTable.newsPicID
This links the selected image from one table to a news story in another table.
This only works for one image, but if I was to have up to 3 images that could be added for one story, what would my new SQL statement look like?
I can't quite get my head round the syntax of the new SQL statement.
Any guidance much appreciated.
newsTable
newsID
newsTitle
newsBody
newsPicID
newsPicIDTwo
newsPicIDThree
picTable
picID
picFileName
picPath
You can join two more times to bring in the picture information:
SELECT t1.*,
COALESCE(t2.picFileName, 'NA') AS picFileName1,
COALESCE(t3.picFileName, 'NA') AS picFileName2,
COALESCE(t4.picFileName, 'NA') AS picFileName3
FROM newsTable t1
LEFT JOIN picTable t2
ON t1.newsPicID = t2.picID
LEFT JOIN picTable t3
ON t1.newsPicIDTwo = t3.picID
LEFT JOIN picTable t4
ON t1.newsPicIDThree = t4.picID
You can add more columns here from the picTable as you need, and possibly use COALESCE() again to provide a default value should a news item not have any picture information.
Maybe like this:
SELECT newsTable.*, pic.*, picTwo.*, picThree.*
FROM newsTable
INNER JOIN picTable AS pic on picTable.picID = newsTable.newsPicID
INNER JOIN picTable AS picTwo on picTable.picID = newsTable.newsPicIDTwo
INNER JOIN picTable AS picThree on picTable.picID = newsTable.newsPicIDThree
if newsPicIDTwo or newsPicIDThree are NULL you schould use an LEFT OUTER JOIN
Just left join the picTable three times:
select *
from newsTable t
left join picTable p1 on p1.picID = t.newsPicID
left join picTable p2 on p2.picID = t.newsPicIDTwo
left join picTable p3 on p3.picID = t.newsPicIDThree
Use LEFT OUTER JOIN to get three pictures :
SELECT * FROM newsTable
LEFT OUTER JOIN picTable PIC1 on PIC1.picID = newsTable.newsPicID
LEFT OUTER JOIN picTable PIC2 on PIC2.picID = newsTable.newsPicIDTwo
LEFT OUTER JOIN picTable PIC3 on PIC3.picID = newsTable.newsPicIDThree
As you describe your problem, you have 1 to many relationship from news to images. That means that each news id can have multiple picds.
Show you don't need newsPicIDTwo and newsPicIDThree fields on newsTable and you need a field newsid on table picTable. Simply, then
SELECT * FROM newsTable INNER JOIN picTable on picTable.newsID = newsTable.newsID
Will bring you the correct result if you have records on picTable that have same newsID but different picID
In other words, a classic 1-Many relationship is resolved correctly when the foreign key lies on the 'Many' table.
In your case the 'Many' table is picTable that owes to have a foreign key newsid.
Just join again on the appropraite columns. I've used Left joins in case one of the pic columns is empty
SELECT NT.*, p1.picFileName as P1File, p2.picFileName as P2File, p3.picFileName as P3File
FROM newsTable NT
LEFT JOIN picTable P1
on P1.picID = NT.newsPicID
LEFT JOIN picTable P2
on P2.picID = NT.newsPicIDtwo
LEFT JOIN picTable P3
on P3.picID = NT.newsPicIDthree

Reduce query complexity

I have this multi join query, is there any way to reduce the number of joins? Or maybe to split the query to 2 parts but then still get the same result set?
Having too many joins makes the query execute very slowly and inefficently
SELECT
MD.EVENT_ID,
AUI.ATLAS_USER_ID,
EVENT_TIME,
EVENT_TYPE,
INTERACTION_TOKEN,
CC.COUNTRY_CODE,
AP.ADV_PROJECT_ID, AP.ADV_PROJECT_NAME,
AC.ADV_CAMPAIGN_ID,
ADV_CAMPAIGN_NAME,
PC.PRT_CAMPAIGN_ID, PC.PRT_CAMPAIGN_NAME,
IP.IP_ADDRESS,
OS.OS,
BR.BROWSER,
FU.FULL_USER_AGENT,
RAW_ACTION_ID,
SE.SELLER_NETWORK_ID,
RIURL.RAW_INPUT as URL,
RIREF.RAW_INPUT as REF_URL,
MVG1.MAP_VALUE as TABOOLA ,
MVG2.MAP_VALUE as APPNEXUS ,
MVG3.MAP_VALUE as ETAG ,
MVG4.MAP_VALUE as FACEBOOK ,
MVG5.MAP_VALUE as MEDIAMATH ,
MVG6.MAP_VALUE as COOKIEID ,
MVG7.MAP_VALUE as IDFA ,
MVG8.MAP_VALUE as ADVLOGIN ,
MVG9.MAP_VALUE as OPENX ,
MVG10.MAP_VALUE as ADTRUTH ,
MVG11.MAP_VALUE as GOOGLE ,
MVG12.MAP_VALUE as ANDROID_ADV_ID ,
MVG13.MAP_VALUE as SDGUPI ,
MVG15.MAP_VALUE as RMX
FROM
EVENT_124_2 BASE
INNER JOIN
atlas__atlas_events MD ON BASE.EVENT_ID = MD.EVENT_ID
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG1 ON MVG1.MAP_VALUE_GK = BASE.TABOOLA_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG2 ON MVG2.MAP_VALUE_GK = BASE.APPNEXUS_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG3 ON MVG3.MAP_VALUE_GK = BASE.ETAG_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG4 ON MVG4.MAP_VALUE_GK = BASE.FACEBOOK_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG5 ON MVG5.MAP_VALUE_GK = BASE.MEDIAMATH_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG6 ON MVG6.MAP_VALUE_GK = BASE.COOKIEID_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG7 ON MVG7.MAP_VALUE_GK = BASE.IDFA_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG8 ON MVG8.MAP_VALUE_GK = BASE.ADVLOGIN_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG9 ON MVG9.MAP_VALUE_GK = BASE.OPENX_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG10 ON MVG10.MAP_VALUE_GK = BASE.ADTRUTH_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG11 ON MVG11.MAP_VALUE_GK = BASE.GOOGLE_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG12 ON MVG12.MAP_VALUE_GK = BASE.ANDROID_ADV_ID_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG13 ON MVG13.MAP_VALUE_GK = BASE.SDGUPI_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG15 ON MVG15.MAP_VALUE_GK = BASE.RMX_HASH
LEFT JOIN
LOOKUP_STG__ATLAS_USER_ID AUI ON AUI.ATLAS_USER_ID_GK = MD.ATLAS_USER_ID
LEFT JOIN
GAYA__ADV_PROJECTS AP ON AP.ADV_PROJECT_ID = MD.ADV_PROJECT_ID
LEFT JOIN
GAYA__ADV_CAMPAIGNS AC ON AC.ADV_CAMPAIGN_ID = MD.ADV_CAMPAIGN_ID
LEFT JOIN
GAYA__PRT_CAMPAIGNS PC ON PC.PRT_CAMPAIGN_ID = MD.PRT_CAMPAIGN_ID
LEFT JOIN
LOOKUP_STG__RAW_INPUT RIURL ON RIURL.RAW_INPUT_GK = BASE.URL_HASH
LEFT JOIN
LOOKUP_STG__RAW_INPUT RIREF ON RIREF.RAW_INPUT_GK = BASE.REF_HASH
LEFT JOIN
LOOKUP_STG__OS OS ON OS.OS_GK = MD.COUNTRY_CODE
LEFT JOIN
LOOKUP_STG__COUNTRY_CODE CC ON CC.COUNTRY_CODE_GK = MD.COUNTRY_CODE
LEFT JOIN
LOOKUP_STG__BROWSER BR ON BR.BROWSER_GK = MD.BROWSER
LEFT JOIN
LOOKUP_STG__IP_ADDRESS IP ON IP.IP_ADDRESS_GK = MD.IP_ADDRESS
LEFT JOIN
LOOKUP_STG__SELLER_NETWORK_ID SE ON SE.SELLER_NETWORK_ID_GK = MD.SELLER_NETWORK_ID
LEFT JOIN
LOOKUP_STG__FULL_USER_AGENT FU ON FU.FULL_USER_AGENT_GK = MD.FULL_USER_AGENT
;
Do you have any indexes on your key columns? You might try to put bitmap indexes on each key column in the main fact table and also in the dimension tables. That should give you a considerable speedup.
You say that the number of joins makes the query inefficient. But what you should be looking at is whether the tables that are joined with have the proper indexes in them. You obviously need all those fields from the tables you join with, so you can't simply not join with them.
For every JOIN you make in the query, verify that there exists an INDEX on the field that is joined with. For instance:
INNER JOIN
atlas__atlas_events MD ON BASE.EVENT_ID = MD.EVENT_ID
Does the table atlas__atlas_events have an INDEX on the EVENT_ID column?
You need to verify this for every such JOIN in your query. If such an INDEX does not exist you should create one.
If you execute this query in SQL Server Management Studio and include the actual execution plan, you will probably already see indications that you are missing indexes.

SQL with left outer join and 3 tables

I would like to add an ADD at the end of my code. Please have a look on my code and thanks for your support:
SELECT Area.org,
Supervisors.NomSup,
Supervisors.PrenomSup,
Employees.NomEmp,
Employees.PrenomEmp,
Employees.NoIdAlcanEmp,
Competencies.CodeCompetencies,
Competencies.CompetencyName,
LinkResultComp.AssNote,
LinkResultComp.AssDate
FROM ((((((
Area INNER JOIN Supervisors ON Area.IdArea = Supervisors.IdArea
)
INNER JOIN Employees ON Supervisors.IdSupervisor = Employees.IdSupervisor
)
INNER JOIN LinkProfilesEmployees ON Employees.IdEmp = LinkProfilesEmployees.IdEmp
)
INNER JOIN Profiles ON Profiles.IdProfiles = LinkProfilesEmployees.IdProfiles
)
INNER JOIN LinkProfComp ON Profiles.IdProfiles = LinkProfComp.IdProfiles
)
INNER JOIN Competencies ON Competencies.IdCompetencies = LinkProfComp.IdCompetencies
)
LEFT OUTER JOIN LinkResultComp ON (Competencies.IdCompetencies = LinkResultComp.IdCompetencies AND ON Competencies.IdCompetencies = LinkResultComp.IdCompetencies)
WHERE Area.org LIKE "*20*" AND Competencies.CodeCompetencies LIKE "khse2010-05"
ORDER BY Supervisors.NomSup, Employees.NomEmp;
Just remove the extra ON that you added
So change this
LEFT OUTER JOIN LinkResultComp
ON (Competencies.IdCompetencies = LinkResultComp.IdCompetencies
AND ON Competencies.IdCompetencies = LinkResultComp.IdCompetencies)
------^^ This one
to this
LEFT OUTER JOIN LinkResultComp
ON (Competencies.IdCompetencies = LinkResultComp.IdCompetencies
AND Competencies.IdCompetencies = LinkResultComp.IdCompetencies)
Of course I assume you meant different fields for the second condition

Oracle and Left Outer Join

I am confused about this Hibernate generated Oracle Sql. There is one user in the database, but they don't have any badges, but I am doing a left outer join on everything. So the user should come back everytime, regardless of them having a badge. If I remove these lines, then it pulls back the user. Isn't left outer join suppose to bring someone back no matter what?
AND b4_.ACTIVE=1
AND B4_.STATUS='A'
AND UB2_.VISIBLE=1
and bl3_.ACTIVE=1
Hibernate Sql Ran In Sql Developer
select
this_.ID as ID0_11_,
this_.BIOGRAPHY as BIOGRAPHY0_11_,
this_.DATECREATED as DATECREA3_0_11_,
this_.EMAIL as EMAIL0_11_,
this_.ENABLED as ENABLED0_11_,
this_.FIRSTNAME as FIRSTNAME0_11_,
this_.HIDECONNECTORS as HIDECONN7_0_11_,
this_.HIDEEMAIL as HIDEEMAIL0_11_,
this_.HIDENAME as HIDENAME0_11_,
this_.LASTNAME as LASTNAME0_11_,
this_.PASSWORD as PASSWORD0_11_,
this_.SALT as SALT0_11_,
this_.TITLE as TITLE0_11_,
this_.USERNAME as USERNAME0_11_,
this_.WARNINGS as WARNINGS0_11_,
(SELECT
COUNT(*)
FROM
Followers f
WHERE
f.followerid = this_.Id) as formula0_11_,
assets6_.USERID as USERID13_,
asset7_.ID as ASSETID13_,
asset7_.ID as ID2_0_,
asset7_.ACTIVE as ACTIVE2_0_,
asset7_.DATECREATED as DATECREA3_2_0_,
asset7_.DATEMODIFIED as DATEMODI4_2_0_,
asset7_.DESCRIPTION as DESCRIPT5_2_0_,
asset7_.FILENAME as FILENAME2_0_,
asset7_.FILEPATH as FILEPATH2_0_,
asset7_.TITLE as TITLE2_0_,
asset7_.TYPE as TYPE2_0_,
roles8_.USERID as USERID14_,
role9_.ID as ROLEID14_,
role9_.ID as ID1_1_,
role9_.DISPLAYNAME as DISPLAYN2_1_1_,
role9_.NAME as NAME1_1_,
ub2_.USERID as USERID15_,
ub2_.ID as ID15_,
ub2_.ID as ID12_2_,
ub2_.BADGELEVELID as BADGELEV5_12_2_,
ub2_.DATECREATED as DATECREA2_12_2_,
ub2_.ISMANUAL as ISMANUAL12_2_,
ub2_.USERID as USERID12_2_,
ub2_.VISIBLE as VISIBLE12_2_,
bl3_.ID as ID9_3_,
bl3_.ACTIVE as ACTIVE9_3_,
bl3_.ASSETID as ASSETID9_3_,
bl3_.BADGEID as BADGEID9_3_,
bl3_.DATECREATED as DATECREA3_9_3_,
bl3_.DATEMODIFIED as DATEMODI4_9_3_,
bl3_.DESCRIPTION as DESCRIPT5_9_3_,
bl3_.FILTERS as FILTERS9_3_,
bl3_."ORDER" as ORDER7_9_3_,
(SELECT
COUNT(*)
FROM
USERBADGES ub
WHERE
ub.badgeLevelId = bl3_.Id) as formula1_3_,
(bl3_."ORDER" - 1) as formula2_3_,
asset12_.ID as ID2_4_,
asset12_.ACTIVE as ACTIVE2_4_,
asset12_.DATECREATED as DATECREA3_2_4_,
asset12_.DATEMODIFIED as DATEMODI4_2_4_,
asset12_.DESCRIPTION as DESCRIPT5_2_4_,
asset12_.FILENAME as FILENAME2_4_,
asset12_.FILEPATH as FILEPATH2_4_,
asset12_.TITLE as TITLE2_4_,
asset12_.TYPE as TYPE2_4_,
b4_.ID as ID10_5_,
b4_.ACTIVE as ACTIVE10_5_,
b4_.DATECREATED as DATECREA3_10_5_,
b4_.DATEMODIFIED as DATEMODI4_10_5_,
b4_.DESCRIPTION as DESCRIPT5_10_5_,
b4_.ENDDATE as ENDDATE10_5_,
b4_.NAME as NAME10_5_,
b4_.PUBLISHDETAILS as PUBLISHD8_10_5_,
b4_.STARTDATE as STARTDATE10_5_,
b4_.STATUS as STATUS10_5_,
b4_.UPDATEOWNERID as UPDATEO11_10_5_,
b4_.OWNERID as OWNERID10_5_,
(SELECT
COUNT(*)
FROM
BadgeLevels bl
WHERE
bl.badgeId = b4_.Id) as formula3_5_,
(CASE
WHEN (SELECT
COUNT(*)
FROM
BadgeLevels bl
WHERE
bl.badgeId = b4_.Id) > 1 THEN 'Ladder'
ELSE 'Single'
END) as formula4_5_,
user14_.ID as ID0_6_,
user14_.BIOGRAPHY as BIOGRAPHY0_6_,
user14_.DATECREATED as DATECREA3_0_6_,
user14_.EMAIL as EMAIL0_6_,
user14_.ENABLED as ENABLED0_6_,
user14_.FIRSTNAME as FIRSTNAME0_6_,
user14_.HIDECONNECTORS as HIDECONN7_0_6_,
user14_.HIDEEMAIL as HIDEEMAIL0_6_,
user14_.HIDENAME as HIDENAME0_6_,
user14_.LASTNAME as LASTNAME0_6_,
user14_.PASSWORD as PASSWORD0_6_,
user14_.SALT as SALT0_6_,
user14_.TITLE as TITLE0_6_,
user14_.USERNAME as USERNAME0_6_,
user14_.WARNINGS as WARNINGS0_6_,
(SELECT
COUNT(*)
FROM
Followers f
WHERE
f.followerid = user14_.Id) as formula0_6_,
websites15_.USERID as USERID16_,
website16_.ID as WEBSITEID16_,
website16_.ID as ID6_7_,
website16_.ACTIVE as ACTIVE6_7_,
website16_.DATECREATED as DATECREA3_6_7_,
website16_.DATEMODIFIED as DATEMODI4_6_7_,
website16_.DESCRIPTION as DESCRIPT5_6_7_,
website16_.NAME as NAME6_7_,
website16_.URL as URL6_7_,
uc1_.USERID as USERID17_,
uc1_.ID as ID17_,
uc1_.ID as ID17_8_,
uc1_.ACTIVE as ACTIVE17_8_,
uc1_.CONNECTORID as CONNECTO6_17_8_,
uc1_.dateCreated as dateCrea3_17_8_,
uc1_.dateModified as dateModi4_17_8_,
uc1_.META as META17_8_,
uc1_.USERID as USERID17_8_,
connector18_.ID as ID18_9_,
connector18_.ACTIVE as ACTIVE18_9_,
connector18_.DATECREATED as DATECREA3_18_9_,
connector18_.DISPLAYNAME as DISPLAYN4_18_9_,
connector18_.NAME as NAME18_9_,
user19_.ID as ID0_10_,
user19_.BIOGRAPHY as BIOGRAPHY0_10_,
user19_.DATECREATED as DATECREA3_0_10_,
user19_.EMAIL as EMAIL0_10_,
user19_.ENABLED as ENABLED0_10_,
user19_.FIRSTNAME as FIRSTNAME0_10_,
user19_.HIDECONNECTORS as HIDECONN7_0_10_,
user19_.HIDEEMAIL as HIDEEMAIL0_10_,
user19_.HIDENAME as HIDENAME0_10_,
user19_.LASTNAME as LASTNAME0_10_,
user19_.PASSWORD as PASSWORD0_10_,
user19_.SALT as SALT0_10_,
user19_.TITLE as TITLE0_10_,
user19_.USERNAME as USERNAME0_10_,
user19_.WARNINGS as WARNINGS0_10_,
(SELECT
COUNT(*)
FROM
Followers f
WHERE
f.followerid = user19_.Id) as formula0_10_
from
REWARD.USERS this_
left outer join
UserAssets assets6_
on this_.ID=assets6_.USERID
left outer join
REWARD.ASSETS asset7_
on assets6_.ASSETID=asset7_.ID
left outer join
UserRoles roles8_
on this_.ID=roles8_.USERID
left outer join
REWARD.ROLES role9_
on roles8_.ROLEID=role9_.ID
left outer join
REWARD.USERBADGES ub2_
on this_.ID=ub2_.USERID
left outer join
REWARD.BADGELEVELS bl3_
on ub2_.BADGELEVELID=bl3_.ID
left outer join
REWARD.ASSETS asset12_
on bl3_.ASSETID=asset12_.ID
left outer join
REWARD.BADGES b4_
on bl3_.BADGEID=b4_.ID
left outer join
REWARD.USERS user14_
on ub2_.USERID=user14_.ID
left outer join
UserWebsites websites15_
on user14_.ID=websites15_.USERID
left outer join
REWARD.WEBSITES website16_
on websites15_.WEBSITEID=website16_.ID
left outer join
REWARD.USERCONNECTORS uc1_
on this_.ID=uc1_.USERID
left outer join
REWARD.CONNECTORS connector18_
on uc1_.CONNECTORID=connector18_.ID
left outer join
REWARD.USERS USER19_
on uc1_.USERID=user19_.ID
WHERE
this_.ID=10100
and this_.ENABLED=1
AND UC1_.ACTIVE=1
AND UB2_.VISIBLE=1
and bl3_.ACTIVE=1
AND b4_.ACTIVE=1
AND B4_.STATUS='A'
Those users do come back but they come back as null for all the columns returned in the tables where the left join didn't find a proper join.
Due to how ANSI nulls work
b4_.ACTIVE=1 will be invalid for these records because null <> 1
Try restructuring your where block as follows:
AND (b4_.ACTIVE=1 or b4_.ACTIVE is null)
AND (B4_.STATUS='A' or B4_.STATUS is null)
AND (UB2_.VISIBLE=1 or UB2_.VISIBLE is null)
and (bl3_.ACTIVE=1 or bl3_.ACTIVE is null)
Another way to tackle this is add the prerequisites to your left joins. You can do like I did below and it will exclude the badges where ACTIVE <> 1 excluding bad badges and still return all users.
left outer join
REWARD.BADGES b4_
on bl3_.BADGEID=b4_.ID
AND b4_.ACTIVE=1