How to retrive the effective dated empid from PS_JOB table - sql

I am new to peoplesoft. I can understand the effective date at a conceptual level, but I am still struggling with it in SQR. How do I retrieve the effective dated empid from PS_JOB table? How do I retrieve the valid empname from ps_names for the retrieved empid? Thanks in advance!

the current name - from the ps_names_vw view
you can use a similar where clause for the ps_job table.
select a.emplid,
a.name_type,
a.effdt,
a.last_name,
a.first_name,
a.middle_name,
a.name_prefix,
a.name_suffix,
a.last_name_srch,
a.first_name_srch,
a.name_display
from ps_names a
where a.effdt = (select max (b.effdt)
from ps_names b
where b.emplid = a.emplid and
b.name_type = a.name_type and
b.effdt <= to_date (to_char (sysdate, 'YYYY-MM-DD'), 'YYYY-MM-DD'));

Join names and job table with their respective effective date. here i have taken A, L, P, S employees assuming you need active employees' names only. if not , you can remove empl_status criterion. Also you need to specify the name_type in the main criterion , otherwise multiple rows may be returned for one emplid.
SELECT * FROM PS_NAMES N, PS_JOB J
WHERE N.EMPLID = J.EMPLID
AND N.EFFDT = (SELECT MAX(N1.EFFDT) FROM PS_NAMES N1
WHERE N1.EMPLID= N.EMPLID
AND N1.NAME_TYPE = N.NAME_TYPE
AND N1.EFFDT <= SYSDATE)
AND J.EFFDT = (SELECT MAX(J1.EFFDT) FROM PS_JOB J1
WHERE J1.EMPLID = J.EMPLID
AND J1.EMPL_RCD = J.EMPL_RCD
AND J1.EFFDT <= SYSDATE)
AND J.EFFSEQ = (SELECT MAX(J2.EFFSEQ) FROM PS_JOB J2
WHERE J2.EMPLID = J.EMPLID
AND J2.EMPL_RCD = J.EMPL_RCD
AND J2.EFFDT = J.EFFDT)
AND J.EMPL_STATUS IN ('A','L','P','S')

The below query will fetch you the most recent data of an employee with Emplid 'XXX' from PS_JOB
SELECT * FROM PS_JOB J
WHERE J.EMPLID = 'XXX'
AND J.EFFDT = (SELECT MAX(J1.EFFDT)
FROM PS_JOB J1
WHERE J1.EMPLID =J.EMPLID
AND J1.EMPL_RCD = J.EMPL_RCD
AND J1.EFFDT < TRUNC(SYSDATE))
AND J.EMPL_RCD = ( SELECT MAX(J2.EMPL_RCD)
FROM PS_JOB J2
WHERE J2.EMPLID = J.EMPLID
AND J2.EFFDT = J.EFFDT )
AND J.EFFSEQ = ( SELECT MAX(J3.EFFSEQ)
FROM PS_JOB J3
WHERE J3.EMPLID = J.EMPLID
AND J3.EMPL_RCD = J.EMPL_RCD
AND J3.EFFDT = J.EFFDT)
ORDER BY J.EMPLID , J.EMPL_RCD, J.EFFDT, J.EFFSEQ

Related

Unsupported subquery with table in join predicate

I am tried to run the next query but the next message appearance "Unsupported subquery with table in join predicate." Do you have some idea? I appreciate your comments.
WITH
D1 AS (
SELECT
SETID,
CUST_ID,
MAX(EFFDT) AS EFFDT
FROM
`g4s-data-reporting.g4s_data.PS_CUST_CREDIT`
WHERE
EFFDT <= CURRENT_TIMESTAMP()
GROUP BY
1,
2 ),
LEFT OUTER JOIN
`g4s-data-reporting.g4s_data.PS_CUST_CREDIT` D
ON
A.SETID = D.SETID
AND A.CUST_ID = D.CUST_ID
AND D.EFFDT = (
SELECT
MAX(EFFDT) --select 2
FROM
`g4s-data-reporting.g4s_data.PS_CUST_CREDIT` D1
WHERE
D1.SETID = D.SETID
AND D1.CUST_ID = D.CUST_ID
AND D1.EFFDT <= CURRENT_TIMESTAMP() )
WHERE
B.BUSINESS_UNIT IN ('00001',
'00048')
AND B.ITEM_STATUS = 'O'
AND B.ENTRY_TYPE NOT IN ('AO',
'CNVBC',
'MC',
'OA')

Display only 1 row in unioned query where they have rows certain criteria in another table

I couldn't think of a better title to this thread but I have the below Unioned query that is pulling in exam information per UserID that is unioned to pull in the total of the counts.
The problem is that when the UserID has two rows in PS_JOB AND one row has a PER_ORG value of 'CWR' and the other row has a PER_ORG value of 'EMP' then the query is returning 2 rows for this same UserID. In this scenario I only want to display the 'EMP' record, so that only 1 instance of their ID (that falls into this scenario) will count in my Totals (ZTOTAL).
I imagine I need some sort of WHERE condition with NOT EXISTS where it finds two rows with the same UserID, AND one row contains PER_ORG = 'EMP' and the other row contains PER_ORG = 'CWR', but am struggling as to how to code this. I don't want to exclude USERID's that have two rows in PS_JOB with any other combination of values for PER_ORG, other than 'EMP' and 'CWR'.
Current SQL:
SELECT
A.USERID,
A.IMMUN_CODE,
B.COMPANY,
COUNT(*) AS 'COUNT_OF_EXAMS'
FROM
PS_HS_IMMUN A,
PS_JOB B,
PS_EMPLMT_SRCH_QRY B1
WHERE
(B.USERID = B1.USERID
AND B.EMPL_RCD = B1.EMPL_RCD
AND B1.OPRID = 'GG125'
AND ( A.USERID = B.USERID
AND B.EFFDT = (SELECT MAX(B_ED.EFFDT)
FROM PS_JOB B_ED
WHERE B.USERID = B_ED.USERID
AND B.EMPL_RCD = B_ED.EMPL_RCD
AND B_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10))
AND B.EFFSEQ = (SELECT MAX(B_ES.EFFSEQ)
FROM PS_JOB B_ES
WHERE B.USERID = B_ES.USERID
AND B.EMPL_RCD = B_ES.EMPL_RCD
AND B.EFFDT = B_ES.EFFDT)
)
)
AND A.HISTORY_ONLY <> 'Y'
GROUP BY A.USERID, A.IMMUN_CODE, B.COMPANY
UNION
SELECT
'ZTOTAL',
'',
'',
COUNT ( D.USERID)
FROM
(PS_HS_IMMUN D
LEFT OUTER JOIN
(PS_JOB E
INNER JOIN PS_EMPLMT_SRCH_QRY E1 ON
(E.USERID = E1.USERID
AND E.EMPL_RCD = E1.EMPL_RCD
AND E1.OPRID = 'GG125' )) ON
D.USERID = E.USERID)
WHERE
(
(E.EFFDT = (SELECT MAX(E_ED.EFFDT)
FROM PS_JOB E_ED
WHERE E.USERID = E_ED.USERID
AND E.EMPL_RCD = E_ED.EMPL_RCD
AND E_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10))
AND E.EFFSEQ = (SELECT MAX(E_ES.EFFSEQ)
FROM PS_JOB E_ES
WHERE E.USERID = E_ES.USERID
AND E.EMPL_RCD = E_ES.EMPL_RCD
AND E.EFFDT = E_ES.EFFDT)
--AND D.EXAM_DT BETWEEN :1 AND :2
--AND E.COMPANY = :3
AND D.HISTORY_ONLY <> 'Y' )
)
Current Results From Above Query:
In this example, the two rows for UserID 4992 is duplicated because the UserID has two rows in the PS_JOBS table, hence why the USERID displays twice (and ultimately counts twice in the totals) in the query.
If I do a query from PS_JOBS for this USERID, you will see the two rows for this USERID AND the PER_ORG status of 'EMP' and 'CWR', respectfully.
SELECT *
FROM PS_JOB
WHERE EMPLID = '4992'
Results from above query:
I'm looking to avoid using Common Table Expressions as a solution. Thanks for any suggestions!

SQL Select only unique values in column

I have the below query that is pulling from a table where a record (Employee ID) can have more than 1 row of data. Because of this I have some rows with the highest (Max) EXAM_DATE. I have added in the MAX function into the SELECT Statement, however I am still retrieving rows with 2 or more dates per empl. This must be something simply that I am overlooking. Any help is appreciated.
SQL Query:
SELECT A.EMPLID, A.BUSINESS_TITLE, A.DEPTID, A.LOCATION, A.LAST_NAME,
A.FIRST_NAME, A.MIDDLE_NAME,
MAX(CONVERT(CHAR(10),D.EXAM_DT,121))AS EXAM_DATE, D.EXAM_TYPE_CD,
(CONVERT(CHAR(10),D.GH_EXPIRE_DATE,121)) AS EXPIRE_DATE, AA.NAME AS
Manager_Name
FROM ((PS_EMPLOYEES A INNER JOIN PS_EMPLMT_SRCH_QRY A1 ON (A.EMPLID =
A1.EMPLID AND A.EMPL_RCD = A1.EMPL_RCD AND A1.OPRID = 'HD065' ))
LEFT OUTER JOIN PS_GHS_HS_PPDSCRN D ON A.EMPLID = D.EMPLID)
--LEFT OUTER JOIN PS_GHS_REPORTS_TO H ON A.EMPLID = H.EMPLID
LEFT OUTER JOIN PS_EMPLOYEES AA ON AA.POSITION_NBR = A.REPORTS_TO
AND (( AA.EFFDT =
(SELECT MAX(A_ED.EFFDT) FROM PS_EMPLOYEES A_ED
WHERE AA.POSITION_NBR = A_ED.POSITION_NBR
AND A_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10)) ))
WHERE ( ( A.EFFDT =
(SELECT MAX(A_ED.EFFDT) FROM PS_EMPLOYEES A_ED
WHERE A.EMPLID = A_ED.EMPLID
AND A.EMPL_RCD = A_ED.EMPL_RCD
AND A_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10))
AND A.EFFSEQ =
(SELECT MAX(A_ES.EFFSEQ) FROM PS_EMPLOYEES A_ES
WHERE A.EMPLID = A_ES.EMPLID
AND A.EMPL_RCD = A_ES.EMPL_RCD
AND A.EFFDT = A_ES.EFFDT)
AND D.EXAM_TYPE_CD = 'PPD'))
AND A.EMPL_STATUS = 'A'
--AND NOT EXISTS (SELECT D.EMPLID, COUNT(*) AS 'TotalOccur' FROM PS_GHS_HS_PPDSCRN D GROUP BY D.EMPLID HAVING COUNT(*) > 1)
GROUP BY A.EMPLID, A.BUSINESS_TITLE, A.DEPTID, A.LOCATION, A.LAST_NAME,
A.FIRST_NAME, A.MIDDLE_NAME, D.EXAM_TYPE_CD, D.GH_EXPIRE_DATE, AA.NAME
ORDER BY 5
I tried running it with the Commented out code for NOT EXISTS, however when I run this I do not retrieve any results.
Sample Output:
The MAX in your WHERE should be fine.
Your EXPIRE_DATE is also different between the two records, and as the EXPIRE_DATE is included in the GROUP BY, it will cause the "duplicate".

Access query only return 1 max date result per select MAX statement

I've been beating my head for a while on this one...
I'm working with a legacy Access database (which I'm working on phasing out/replacing), and an issue came up where duplicate records are now showing from the query where we used to never see duplicate records.
We pull expiration dates from several tables, and the query returns the most future expiration date for each person, for each certificate they have.
The whole query is quite large, but the problem area, that is left join duplicated for each table of expirations, is this:
SELECT Courses.[id], Person.FirstName, Person.MiddleName, Person.LastName, Courses.Code, Codes.Name, Courses.Date, Courses.[P/F/I/C], Courses.ExpirationDate, Courses.Grade
FROM Codes INNER JOIN (Courses INNER JOIN Person ON Courses.[id] = Person.[id]) ON Codes.Code = Courses.Code
WHERE (
Courses.ExpirationDate = (SELECT MAX(ExpirationDate) FROM Courses b WHERE b.ExpirationDate >= Date() AND (b.[id] = Courses.[id]) AND b.Code = 'c0' AND ((b.[P/F/I/C])='P' Or (b.[P/F/I/C])='C'))
) OR (
Courses.ExpirationDate = (SELECT MAX(ExpirationDate) FROM Courses b WHERE b.ExpirationDate >= Date() AND (b.[id] = Courses.[id]) AND b.Code = 'c1' AND ((b.[P/F/I/C])='P' Or (b.[P/F/I/C])='C'))
) OR (
Courses.ExpirationDate = (SELECT MAX(ExpirationDate) FROM Courses b WHERE b.ExpirationDate >= Date() AND (b.[id] = Courses.[id]) AND b.Code = 'c2' AND ((b.[P/F/I/C])='P' Or (b.[P/F/I/C])='C'))
) OR (
Courses.ExpirationDate = (SELECT MAX(ExpirationDate) FROM Courses b WHERE b.ExpirationDate >= Date() AND (b.[id] = Courses.[id]) AND b.Code = 'c3' AND ((b.[P/F/I/C])='P' Or (b.[P/F/I/C])='C'))
) OR (
Courses.ExpirationDate = (SELECT MAX(ExpirationDate) FROM Courses b WHERE b.ExpirationDate >= Date() AND (b.[id] = Courses.[id]) AND b.Code = 'c4' AND ((b.[P/F/I/C])='P' Or (b.[P/F/I/C])='C'))
) OR (
Courses.ExpirationDate = (SELECT MAX(ExpirationDate) FROM Courses b WHERE b.ExpirationDate >= Date() AND (b.[id] = Courses.[id]) AND b.Code = 'c5' AND ((b.[P/F/I/C])='P' Or (b.[P/F/I/C])='C'))
) OR (
Courses.ExpirationDate = (SELECT MAX(ExpirationDate) FROM Courses b WHERE b.ExpirationDate >= Date() AND (b.[id] = Courses.[id]) AND b.Code = 'c6' AND ((b.[P/F/I/C])='P' Or (b.[P/F/I/C])='C'))
) OR (
Courses.ExpirationDate = (SELECT MAX(ExpirationDate) FROM Courses b WHERE b.ExpirationDate >= Date() AND (b.[id] = Courses.[id]) AND b.Code = 'c7' AND ((b.[P/F/I/C])='P' Or (b.[P/F/I/C])='C'))
)
ORDER BY Courses.[id], Courses.Code
If one of those OR clauses has two future expiration dates for a single person in a single class (they re-certified before they expired), the query returns both records. It should only return the MAX of those two records. Any ideas as to what I'm missing in the above query? I can post the rest of the query that this one is nested in if it helps, but I'm pretty sure this is where the issue is.
I am guessing that you really want this:
WHERE Courses.ExpirationDate = (SELECT MAX(b.ExpirationDate)
FROM Courses b
WHERE b.ExpirationDate >= Date() AND
b.[id] = Courses.[id] AND
b.Code IN ('c0', 'c1', . . .) AND
b.Code = Courses.Code AND
b.[P/F/I/C]) IN ('P', 'C')
)
This is a single condition with an additional correlation condition on Code.

Sql query - Oracle database

empid emplrcd effdt effsq
101 #1 2/1/99 0
101 #1 3/1/13 1
101 #1 23/3/13 1
101 #1 22/6/13 2
102 #2 20/6/91 1
I need to retrieve row 4, and I have written a partial code, please help me with the other half.
select a*
from Ps_Job a
where a.empid = '101'
and a.emprcd ='#1'
and a.effdt = (select max(a1.effdt) from Psjob1) where...............
and a.effseq = (Select max(a2.effseq) from Ps_job2)
where..............
Please help me with the where caluse which should be generic and not row specific. i think it should be filled with nth max concept but not sure.
In oracle
select *
from
(select a* from Ps_Job a
where a.empid = '101'
and a.emprcd ='#1'
and a.effdt = (select max(a1.effdt) from Psjob1) where ...
and a.effseq = (Select max(a2.effseq) from Ps_job2)
where ..... )
where ROWNUM == **The line number what you want to get**;
In sql
SELECT * from Ps_Job LIMIT 3,1where(
select a* from Ps_Job a
where a.empid = '101'
and a.emprcd ='#1'
and a.effdt = (select max(a1.effdt) from Psjob1) where ...
and a.effseq = (Select max(a2.effseq) from Ps_job2)
where ..... )