SQL how to deal with no values - sql

When I run the query below
SELECT
COUNT(COALESCE(CASE PARTPPHY.TITRE
WHEN 1 THEN 'Monsieur '
WHEN 2 THEN 'Madame '
WHEN 3 THEN 'Madame '
ELSE ''
END + PARTPPHY.IDENTITE, PARTPPHY.IDENTITE)) AS ident,
RESPONSABLES.DIR_LOC,
PARTPPHY.IU_PART_PP,
COALESCE(CASE PARTPPHY.TITRE
WHEN 1 THEN 'Monsieur '
WHEN 2 THEN 'Madame '
WHEN 3 THEN 'Madame '
ELSE ''
END + PARTPPHY.IDENTITE,PARTPPHY.IDENTITE) AS IDENTITE,
FONCTIONS_ECO.LIBEL AS LIBEL_FONCTION
FROM
RESPONSABLES
INNER JOIN
ETABLISSEMENTS ON ETABLISSEMENTS.IU_ETS = RESPONSABLES.IU_ETS
LEFT OUTER JOIN
PARTPPHY ON RESPONSABLES.IU_PART_PP = PARTPPHY.IU_PART_PP
LEFT OUTER JOIN
FONCTIONS_ECO ON FONCTIONS_ECO.IU_FONC_ECO = RESPONSABLES.IU_FONC
WHERE
ETABLISSEMENTS.IU_ETS = 14783
AND RESPONSABLES.GESTDEL = 1
GROUP BY
RESPONSABLES.DIR_LOC,
PARTPPHY.IU_PART_PP,
COALESCE(CASE PARTPPHY.TITRE WHEN 1 THEN 'Monsieur ' WHEN 2 THEN 'Madame ' WHEN 3 THEN 'Madame ' ELSE '' END + PARTPPHY.IDENTITE,PARTPPHY.IDENTITE),
FONCTIONS_ECO.LIBEL
This is my result
Yet when I'm using this query
SELECT COUNT(COALESCE(CASE PARTPPHY.TITRE
WHEN 1 THEN 'Monsieur '
WHEN 2 THEN 'Madame '
WHEN 3 THEN 'Madame '
ELSE '' END + PARTPPHY.IDENTITE,PARTPPHY.IDENTITE)) AS ident,
RESPONSABLES.DIR_LOC,
PARTPPHY.IU_PART_PP,
COALESCE(CASE PARTPPHY.TITRE WHEN 1 THEN 'Monsieur ' WHEN 2 THEN 'Madame ' WHEN 3 THEN 'Madame ' ELSE '' END + PARTPPHY.IDENTITE,PARTPPHY.IDENTITE) AS IDENTITE,
FONCTIONS_ECO.LIBEL AS LIBEL_FONCTION
FROM RESPONSABLES
INNER JOIN ETABLISSEMENTS ON ETABLISSEMENTS.IU_ETS = RESPONSABLES.IU_ETS
LEFT OUTER JOIN PARTPPHY ON RESPONSABLES.IU_PART_PP = PARTPPHY.IU_PART_PP
LEFT OUTER JOIN FONCTIONS_ECO ON FONCTIONS_ECO.IU_FONC_ECO = RESPONSABLES.IU_FONC
WHERE ETABLISSEMENTS.IU_ETS = 1
AND RESPONSABLES.GESTDEL = 1
GROUP BY RESPONSABLES.DIR_LOC,
PARTPPHY.IU_PART_PP,
COALESCE(CASE PARTPPHY.TITRE WHEN 1 THEN 'Monsieur ' WHEN 2 THEN 'Madame ' WHEN 3 THEN 'Madame ' ELSE '' END + PARTPPHY.IDENTITE,PARTPPHY.IDENTITE),
FONCTIONS_ECO.LIBEL
I have this
My questions are the following:
It does not seem to be a null nor an empty space: What is it?
I want to have a value in my count: What should I do to have this?
Thanks

While using a COUNT without a GROUP BY clause you will always return a row, even if the WHERE clause is not equal to any of the records in your table. But while using a GROUP BY clause , it will return the records only if the WHERE clause condition is true.
Please check the following test scripts
DECLARE #TEST TABLE(ID INT)
INSERT INTO #TEST VALUES(10)
--1.
SELECT COUNT(*) FROM #TEST WHERE ID = 11 --Result is 0
--2.
SELECT COUNT(*),ID FROM #TEST WHERE ID = 11 GROUP BY ID --No Result
Here the first query always return a result
(No column name)
0
But second query not.
You can modify your second query something like the below script to get a '0' result
SELECT ISNULL(( SELECT COUNT(*) FROM #TEST WHERE ID = 11 GROUP BY ID ),0)

There is one more valid option. In both cases you are using the INNER JOIN, which is exclusive join meaning, if the join condition is not satisfied then no data is return, or more precise your result is an empty set.
Your INNER JOIN condition is on ETABLISSEMENTS.IU_ETS = RESPONSABLES.IU_ETS
In the first query your where statement says
WHERE ETABLISSEMENTS.IU_ETS = 14783 AND RESPONSABLES.GESTDEL = 1
This is where you have an empty data set
And in the second query your where statement is
WHERE ETABLISSEMENTS.IU_ETS = 1 AND RESPONSABLES.GESTDEL = 1
So to answer your first query:
Possibly there is no matching record, which has the ETABLISSEMENTS.IU_ETS = 14783 AND RESPONSABLES.GESTDEL = 1 therefore we are getting an empty set
In the second case the where statement is satisfied and there is a corresponding entry in both tables therefore we have a result.
Depending which side of the Join you are interested in, i.e. in which table yo will expect the row to be present you can switch the
JOIN to OUTER JOIN (LEFT or RIGHT)
The outer join is inclusive, meaning that it will return all rows from the table left or right to the join even if there is no record in corresponding table.
Then apply count on the corresponding table, i.e. id column. If there is no record then you will get 0
Example to illustrate
CREATE TABLE #Left
(
id INT
,col1 CHAR(3)
)
CREATE TABLE #Right
(
id INT
,col2 CHAR(3)
)
INSERT INTO #Right
( id, col2)
VALUES
(1,'c1')
,(2,'c2')
,(3,'c4')
,(4,'c5')
INSERT INTO #Left
( id, col1)
VALUES
(1,'c1')
,(2,'c2')
,(3,'c3')
,(4,'c5')
-- empty set
SELECT
*
FROM #Left L
INNER JOIN #Right R
ON L.col1 = R.col2
WHERE col1 = 'c3'
-- matching rows
SELECT
*
FROM #Left L
INNER JOIN #Right R
ON L.col1 = R.col2
WHERE col1 = 'c2'
-- Left Join
SELECT
L.*
,COUNT(R.col2)
FROM #Left L
LEFT JOIN #Right R
ON L.col1 = R.col2
WHERE col1 = 'c3'
GROUP BY L.id, L.col1

Related

What is a better alternative to procedural approach to produce a string based on columns?

I am working on making string based on column values. Example:
SELECT #Registered = a.hasRegistered
,#Subscribed = a.hasSubscribed
FROM AuthorData a
WHERE a.authorId = 10 --#AUTHORID
IF (#Registered = 1)
SET #ReturnString = #ReturnString + '0,'
IF (#Subscribed = 1)
SET #ReturnString = #ReturnString + '1,'
IF EXISTS (
SELECT TOP 1 name
FROM AUTHORDATA AS A
INNER JOIN AUHTORPROFILE B on A.AUTHORID=B.AUTHORID
INNER JOIN AUTHORHISTORY C ON B.AUTHORPROFILEID=C.AUTHORPROFILEID
WHERE ISNULL(AUTHORDATA.authorId, 0) = 10 --#AUTHORID
)
BEGIN
SET #ReturnString = #ReturnString + '10,'
END
select CONVERT(NVARCHAR(50), STUFF(#ReturnString, LEN(#ReturnString), 1, ''))
This performs the calculation for 1 author (based on WHERE clause IN 2 places -> authorId=10)
I can make this into a function and then call from a select query as follows:
SELECT *,FN_CALCUALTE_OPTIONS(AUTHORID)
FROM AUTHORDATA
I want to ask if there is any way so I can do the calculations in the above SELECT query rather than create the function?
I have tried:
SELECT *,CASE WHEN A.hasRegistered=1 THEN '0,' ELSE '' END +
CASE WHEN A.hasSubscribed=1 THEN '1,' ELSE '' END
FROM AUTHORDATA as A
How can I have the exists part of select?
Pretty sure you can put them all together into a single query as follows. If you didn't need the final stuff you would just have a regular query, but because you need to use the results twice in the stuff CROSS APPLY is a convenient way to calculate it once and use it twice. Also you can correlate your sub-query since you are using the same AuthorData record.
SELECT CONVERT(NVARCHAR(50), STUFF(X.ReturnString, LEN(X.ReturnString), 1, ''))
FROM AuthorData a
CROSS APPLY (
VALUES
(
CASE WHEN a.hasRegistered = 1 THEN '0,' ELSE '' END
+ CASE WHEN a.hasSubscribed = 1 THEN '1,' ELSE '' END
+ CASE WHEN EXISTS (
SELECT 1
FROM AUHTORPROFILE B
INNER JOIN AUTHORHISTORY C ON B.AUTHORPROFILEID = C.AUTHORPROFILEID
WHERE B.AUTHORID = a.AuthorId
)
THEN '10,' else '' end
)
) AS X (ReturnString)
WHERE a.AuthorId = 10 --#AUTHORID
The easiest way IMO is to move your criteria being found via EXISTS to an OUTER APPLY. Like so:
SELECT
CONVERT(NVARCHAR(50), STUFF(calc.ReturnString, LEN(calc.ReturnString), 1, ''))
FROM
AUTHORDATA AS A
OUTER APPLY (SELECT TOP (1)
1 AS Found
FROM
AUHTORPROFILE AS B
INNER JOIN AUTHORHISTORY AS C ON B.AUTHORPROFILEID = C.AUTHORPROFILEID
WHERE
B.authorId = A.authorId) AS lookup_author
/*outer apply here just for readibility in final select*/
OUTER APPLY (SELECT
CONCAT(CASE WHEN A.hasRegistered = 1 THEN '0,' ELSE '' END
,CASE WHEN A.hasRegistered = 1 THEN '1,' ELSE '' END
,CASE WHEN lookup_author.Found = 1 THEN '10,' ELSE '' END) AS ReturnString) AS calc;
Then you can use lookup_author.Found = 1 to determine that it was found in your lookup. From there, you just have to apply the rest of your conditions correctly via CASE statements and then use your final SELECT over the result.
You can put a single CASE statement and get data as given below. Make sure that you are covering every possible condition.
SELECT *, CASE WHEN a.Registered =1 AND a.Subscribed =1
AND EXISTS (SELECT TOP 1 1
FROM AUHTORPROFILE B on
INNER JOIN AUTHORHISTORY C ON B.AUTHORPROFILEID=C.AUTHORPROFILEID
WHERE B.AUTHORID =A.AUTHORID) THEN '0,1,10'
WHEN a.Registered =1 AND a.Subscribed =0
AND EXISTS (SELECT TOP 1 1
FROM AUHTORPROFILE B on
INNER JOIN AUTHORHISTORY C ON B.AUTHORPROFILEID=C.AUTHORPROFILEID
WHERE B.AUTHORID =A.AUTHORID) THEN '0,10'
WHEN a.Registered =0 AND a.Subscribed =1
AND EXISTS (SELECT TOP 1 1
FROM AUHTORPROFILE B on
INNER JOIN AUTHORHISTORY C ON B.AUTHORPROFILEID=C.AUTHORPROFILEID
WHERE B.AUTHORID =A.AUTHORID) THEN '1,10'
WHEN a.Registered =0 AND a.Subscribed =0
AND EXISTS (SELECT TOP 1 1
FROM AUHTORPROFILE B on
INNER JOIN AUTHORHISTORY C ON B.AUTHORPROFILEID=C.AUTHORPROFILEID
WHERE B.AUTHORID =A.AUTHORID) THEN '10'
END AS CalculateOptions
FROM AUTHORDATA AS a

How to use keyset pagination?

Suppose I have total 800 eligible rows in database which is ordered by a column requisitionid in descending order. I want to display the records in 80 pages each page having 10 rows. We are using requisitionid as seek predicate. So the predicate should be less than or greater than? As the query will progress from UI (Angular + primeNG), I want to send just one parameter - requisitionid. If it is less than query, then the query will be SELECT ... FROM ... where requisitionid < ?, so here we are talking about first row. If we go for greater than query i.e. SELECT ... FROM ... where requisitionid > ?, here we are talking about last row of the page.
Please refer to Life without offset
EDIT
Actual code:
with topten as (SELECT DISTINCT TOP 10
REQN.CASE_ID
,userContact2.BV_First_Name + ' ' + userContact2.BV_Last_Name ReQCreater
,REQN.BV_Internal_Job_Title
,REQN.BV_Posted_Job_Title as postedJobTitle
,REQN.BV_Status
,REQN.BV_Taleo_Id
,REQN.BV_WD_PositionID
,jobcode.BV_Job_Code
,loc.BV_LocationCode
,loc.BV_LocationName
,D.BV_Division_Code AS 'divCode',
ISNULL(loc.BV_Address1,'') + CASE WHEN ISNULL(loc.BV_Address1,'') = '' THEN '' ELSE ', ' END + ISNULL(loc.BV_Address2,'') + CASE WHEN ISNULL(loc.BV_Address2,'') = '' THEN '' ELSE ', ' END
+ ISNULL(loc.BV_City,'') + CASE WHEN ISNULL(loc.BV_City,'') = '' THEN '' ELSE ', ' END + ISNULL(loc.BV_State,'') + CASE WHEN ISNULL(loc.BV_State,'') = '' THEN '' ELSE (case when ISNULL(loc.BV_ZipCode,'') = '' THEN '' ELSE ', ' END) END
+ ISNULL(loc.BV_ZipCode,'') AS locationAddress
from dbo.CW_V_REQN as REQN
INNER JOIN dbo.CW_TL_Requisition__Location_Master as reqLocLink on REQN.CASE_ID = reqLocLink.FROM_ID
INNER JOIN dbo.CW_V_LOCTMAST as loc on loc.CASE_ID = reqLocLink.TO_ID
INNER JOIN dbo.CW_TL_UserContactInfo__Location_Master as locUserLink on locUserLink.TO_ID = loc.CASE_ID
INNER JOIN dbo.CW_V_USERCONT as userContact on userContact.CASE_ID = locUserLink.FROM_ID
LEFT JOIN dbo.CW_TL_Requisition__Department as reqDeptLink on REQN.CASE_ID = reqDeptLink.FROM_ID
INNER JOIN dbo.CW_V_DEPARTME as dept on dept.CASE_ID = reqDeptLink.TO_ID
LEFT JOIN dbo.CW_TL_UserContactInfo__Department_Master as deptUserLink on dept.CASE_ID=deptUserLink.TO_ID
INNER JOIN dbo.CW_TL_Requisition__Job_Code as reqJobLink on REQN.CASE_ID = reqJobLink.FROM_ID
LEFT JOIN dbo.CW_V_JOBCODE as jobcode on jobcode.CASE_ID = reqJobLink.TO_ID
LEFT JOIN dbo.CW_V_USERCONT as userContact2 on (userContact2.BV_Login_Name = REQN.CREATED_BY)
LEFT JOIN CW_TL_LocationMaster__Division_Master LD ON (LD.FROM_ID = loc.CASE_ID)
LEFT JOIN CW_V_DIVISION D ON (D.CASE_ID = LD.TO_ID)
WHERE userContact.BV_Login_Name = #LOGINNAME
AND REQN.CASE_ID < #MINCASEIDPREVPAGE
ORDER BY REQN.CASE_ID DESC
)
select topten.*
, T.*
from topten
cross join (select min(case_id) as min from topten) as T
For key based pagination on a descending key, the WHERE clause predicate should be < for the next page and > for the previous page. Also, the ORDER BY clause for the previous page needs to be ASC (for the TOP predicate) along an outer DESC (for the descending key display sequence). Below is an example.
--create test table with sample data
CREATE TABLE dbo.YourTable(
requisitionid int PRIMARY KEY
);
WITH
t10 AS (SELECT n FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) t(n))
,t1000 AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS num FROM t10 AS a CROSS JOIN t10 AS b CROSS JOIN t10 AS c)
INSERT INTO dbo.YourTable WITH(TABLOCKX)
SELECT num
FROM t1000
WHERE num <= 800;
GO
--query for first page (page 1) on descending key (returns 800-791)
SELECT TOP(10) requisitionid
FROM YourTable
ORDER BY requisitionid DESC;
GO
--query for next page (page 2) on descending key (returns 790-781)
DECLARE #LastRequisitionIdOnPage int = 791;
SELECT TOP(10) requisitionid
FROM YourTable
WHERE requisitionid < #LastRequisitionIdOnPage
ORDER BY requisitionid DESC;
GO
--query for previous page (page 1) on descending key (returns 800-791)
DECLARE #FirstRequisitionIdOnPage int = 790;
SELECT requisitionid
FROM (
SELECT TOP(10) requisitionid
FROM YourTable
WHERE requisitionid > #FirstRequisitionIdOnPage
ORDER BY requisitionid ASC
) AS prev_page
ORDER BY requisitionid DESC;;
GO
If I understand your question, you should be able to use the follow clause at the end of your SELECT query:
OFFSET (#Page * 10) ROWS FETCH NEXT 10 ROWS ONLY
If this is not what you are looking for, please post your current query so we can see what you are doing now.

Sql error in manage a select to search for sequential hexadecimal number with hole after post join filter

I want to insert a hexadecimal sequential number in ad_code char(5) field and handle / fill its holes.
In order to speed up the query from initial table table1
Column name Type Nulls
ad_code char(5) yes
ad_value smallint yes
I have create a view:
create view v_table1 (vacr_ad_code) as select
ad_code
from table1 where (NVL(ad_code, ' ') != ' ');
then I have tried to use this query to obtain a new value or eventually an hole:
select HEX('0x'||NVL( l.vacr_ad_code, '0'))::INT + 1
from v_table1 as l
left outer join v_table1 as r on
HEX('0x'||NVL( l.vacr_ad_code, '0'))::INT + 1 =
HEX('0x'||NVL( r.vacr_ad_code, '0'))::INT
where
r.vacr_ad_code is null ORDER BY 1 ASC;
but with the following values in the db
vacr_ad_code
1
2
4
instead of obtain 3 and 5 as I expected, I have:
(expression)
2
3
5
Why the number 2 is present ? Where I'm wrong ?
Update: If I modify the query in this way:
select HEX('0x'||NVL( l.vacr_ad_code, '0'))::INT + 1,
HEX('0x'||NVL( r.vacr_ad_code, '0'))::INT
from v_table1 as l
left outer join v_table1 as r on
HEX('0x'||NVL( l.vacr_ad_code, '0'))::INT + 1 =
HEX('0x'||NVL( r.vacr_ad_code, '0'))::INT
ORDER BY 1 ASC;
I obtain:
(expression) (expression)
2 2
3 null
5 null
If I add filter: where r.vacr_ad_code is null I obtain
(expression) (expression)
2 null
3 null
5 null
It seems that the post join filter alter the result of outer join
First, why doesn't your view just do:
create view v_table1 (vacr_ad_code) as
select ad_code
from table1
where ad_code is not null;
I don't see how this would improve the speed of any query.
If you want "next" numbers that don't exist, I would first try:
select cast(t.ad_code as int) + 1
from table1 t
where not exists (select 1
from table1 t1
where cast(t1.ad_code as int) = cast(t.ad_code as int) + 1
);
I would suggest that you store ad_code as a number, because that is how you are using the value.

Link tables based on column value

Is it possible to pull values from 2 different tables based on the value of a column? For example, I have a table with a boolean column that either returns 0 or 1 depending on what the end user selects in our program. 0 means that I should pull in the default values. 1 means to use the user's data.
If my table Table1 looked like this:
Case ID Boolean
====================
1 0
2 1
3 1
4 0
5 0
Then I would need to pull Case IDs 1,4,and 5's corresponding data from table Default and Case IDs 3 and 4's corresponding data from table UserDef. Then I would have to take these values, combine them, and reorder them by Case ID so I can preserve the order in the resulting table.
I am fairly inexperienced with SQL but I am trying to learn. Any help or suggestions are greatly appreciated. Thank you in advance for your help.
Something like this:
SELECT
t1.CaseID
,CASE WHEN t1.Boolean = 1 THEN dt.Col1 ELSE ut.Col1 END AS Col1
,CASE WHEN t1.Boolean = 1 THEN dt.Col2 ELSE ut.Col2 END AS Col2
FROM Table1 t1
LEFT JOIN DefaultTable dt ON dt.CaseID = t1.CaseID
LEFT JOIN UserDefTable ut ON ut.CaseID = t1.CaseID
ORDER BY t1.CaseID
You join on both tables and then use CASE in SELECT to choose from which one to display data.
Option B:
WITH CTE_Combo AS
(
SELECT 0 as Boolean, * FROM Default --replace * with needed columns
UNION ALL
SELECT 1 AS Boolean, * FROM UserDef --replace * with needed columns
)
SELECT * FROM Table1 t
LEFT JOIN CTE_Combo c ON t.CaseID = c.CaseID AND t.Boolean = c.Boolean
ORDER BY t.CaseID
This might be even simpler - using CTE make a union of both tables adding artificial column, and then join CTE and your Table using both ID and flag column.
SELECT t1.CaseID,
ISNULL(td.data, tu.data) userData -- pick data from table_default
-- if not null else from table_user
FROM table1 t1
LEFT JOIN table_default td ON t1.CaseID = td.CaseID -- left join with table_default
AND t1.Boolean = 0 -- when boolean = 0
LEFT JOIN table_user tu ON t1.CaseID = tu.CaseID -- left join with table_user
AND t1.Boolean = 1 -- when boolean = 1
ORDER BY t1.CaseID

SQL JOIN and WHERE statement

I have a problem getting this sql statemen to return what I want:
I want it to return a list of properties both the employee or Job_Profile. If one of them do not have the property it should return NULL in that row/column
Now the sql looks like:
SELECT Parameter.Patameter_Description ParamName,
Job_Profile.Title, Job_Property.Mark JobMark,
Emp_Property.Mark EmpMark,
Emp_Id--, (Employee.First_Name + ' ' + Employee.Last_Name) EmpName
FROM Job_Property
INNER JOIN Job_Profile ON Job_Profile.Title = Job_Property.Job_Title
INNER JOIN Parameter ON Job_Property.Parameter_Id = Parameter.Id
RIGHT JOIN Emp_Property ON Emp_Property.Parameter_Id = Job_Property.Parameter_Id
INNER JOIN Employee ON Emp_Property.Emp_Id = Employee.Enterprise_Staff_Id
WHERE Employee.Enterprise_Staff_Id = 22
AND Job_Profile.Title =
(SELECT
Employee.Job_Profile_Name
FROM Employee WHERE Employee.Enterprise_Staff_Id = 22)
The result is:
Analyse test 1 3 22
And I would like it to be something like this:
Analyse test 1 3 22
Data test 3 NULL NULL or 22
economic test 4 NULL NULL or 22
Service test 2 NULL NULL or 22
I know there is a problem when I:
- join Emp_Property
- Make the WHERE statement
Try LEFT OUTER JOIN when joining Emp_Property
I found a solution, I had to make temp tables and join them:
CREATE TABLE #CompareJob
(Parameter_Id INT
,Parameter_Name VARCHAR(MAX)
,Jobprofile VARCHAR(30)
,Job_Mark INT
)
INSERT INTO #CompareJob(Parameter_Id,Parameter_Name, Jobprofile ,Job_Mark)
SELECT Parameter.Id, Parameter.Patameter_Description, Job_Profile.Title, Job_Property.Mark
FROM Job_Property
INNER JOIN Job_Profile ON Job_Profile.Title = Job_Property.Job_Title
INNER JOIN Parameter ON Job_Property.Parameter_Id = Parameter.Id
WHERE Job_Profile.Title = (SELECT Employee.Job_Profile_Name FROM Employee WHERE Employee.Enterprise_Staff_Id = 22)
CREATE TABLE #CompareEmp
(Parameter_Id INT
,Parameter_Name VARCHAR(MAX)
,Emp_Id INT
,Emp_Name VARCHAR(100)
,Emp_Mark INT
)
INSERT INTO #CompareEmp(Parameter_Id,Parameter_Name, Emp_Id , Emp_Name ,Emp_Mark)
SELECT Parameter.Id, Parameter.Patameter_Description, Employee.Enterprise_Staff_Id, (Employee.First_Name + ' ' + Employee.Last_Name) empname, Emp_Property.Mark
FROM Emp_Property
INNER JOIN Employee ON Employee.Enterprise_Staff_Id = Emp_Property.Emp_Id
INNER JOIN Parameter ON Parameter.Id = Emp_Property.Parameter_Id
WHERE Employee.Enterprise_Staff_Id = 22
SELECT * FROM #CompareJob
FULL OUTER JOIN #CompareEmp ON #CompareJob.Parameter_Id = #CompareEmp.Parameter_Id
Agree with Danny, use the 'LEFT OUTER JOIN' method instead of 'INNER JOIN' as this will only return rows where an entry is found in both tables.