The application is Loan Assignment System. I have list of users. Based on Skills and Assignment Status, I need to fetch eligible users list. There are three user related tables - 'user_info', 'user_skill' & 'assignment_details'. One loan related table which says to which user loan has been assigned i.e., 'loan_mapping' table. To fetch eligible list, I have two main conditions.
- The skill_id from the user_skill table should be 156.
- The assignment_status for each user should be 'Open' OR the assignment_status = 'Limited' along with 'assignment_limited_threshold' VALUE should be GREATER than the total number of loans assigned to that user. (In other words, total loans assignment count for each user shouldnot exceed this assignment_limited_threshold value)
user_id is the joining condition for user related tables.
The table definitions are mentioned below:
CREATE TABLE public.user_info(
user_id integer NOT NULL DEFAULT
nextval('lsa_user_info_user_id_seq'::regclass),
email_id character varying(255),
user_name character varying(255),
lob_id integer NOT NULL,
status_id integer,
CONSTRAINT user_info_pkey PRIMARY KEY (user_id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.lsa_user_info
OWNER TO postgres;
CREATE TABLE public.user_skill
(
user_id integer NOT NULL,
skill_id integer NOT NULL,
CONSTRAINT user_skill_pkey PRIMARY KEY (user_id, skill_id),
CONSTRAINT fk_user_skill_skill_id FOREIGN KEY (skill_id)
REFERENCES public.skill (skill_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_user_skill_user_id FOREIGN KEY (user_id)
REFERENCES public.user_info (user_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION )
WITH (
OIDS=FALSE
);
ALTER TABLE public.user_skill
OWNER TO postgres;
CREATE TABLE public.assignment_details
(
user_id integer NOT NULL,
role_id integer NOT NULL,
lob_id integer NOT NULL,
assignment_status character varying,
assignment_limited_threshold integer,
CONSTRAINT lsa_assignment_details_pkey PRIMARY KEY (user_id,
role_id, lob_id )
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.assignment_details
OWNER TO postgres;
CREATE TABLE public.loan_mapping
(
loan_number bigint NOT NULL,
spoc_id integer NOT NULL,
loan_spoc_map_id integer NOT NULL DEFAULT nextval
('lsa_loan_spoc_mapping_loan_spoc_map_id_seq'::regclass),
lob_id integer,
CONSTRAINT loan_spoc_mapping_pk PRIMARY KEY (loan_spoc_map_id),
CONSTRAINT fk_loan_spoc_loan_number FOREIGN KEY (loan_number)
REFERENCES public.loan_details (loan_number) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_loan_spoc_spoc_id FOREIGN KEY (spoc_id)
REFERENCES public.user_info (user_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.loan_mapping
OWNER TO postgres;
The table records for example are as below,
-----user_info ----
user_id | user_name | lob_id | email_id | status_id
111 abc 5 abc@gmail.com 2
222 def 5 def@gmail.com 6
333 ghi 5 ghi@gmail.com 2
444 jkl 5 jkl@hotmail.com 2
-----------user_skill ----------------
user_id | skill_id
111 156
222 156
333 156
444 156
-----assignment_details----------------
user_id | role_id | lob_id | assignment_status| assignment_limited_threshold
111 11 5 Open
222 11 5 No
333 11 5 Limited 2
444 11 5 Limited 10
------------loan_mapping-----------------
loan_number | spoc_id | loan_spoc_map_id | lob_id | appName
222333 111 23 5 SS
222444 333 24 5 SS
222555 333 25 5 SS
333452 444 26 5 SS
777445 444 27 5 SS
Expected Output is :
user_id
---------
111
444
The Query I tried is not giving the expected result
select ui.user_id from user_info ui
inner join user_skill lus on lus.skill_id = 156 and lus.user_id =
ui.user_id
inner join assignment_details ad on
ui.lob_id=ad.lob_id and ui.user_id=ad.user_id and
ui.lob_id=5 and ui.status_id=2 where ad.lob_id=5 and
ad.role_id=11 and ad.assignment_status='Open' OR
( ad.assignment_status='Limited'
and ad.assignment_limited_threshold > ( select count(loan_number)
from loan_mapping where lob_id=5 and
app_name ='SS'))
Can you please write query as per my expected output ? Thanks in advance
lus.skill_id = 156
. Then how can you expect 444 in your output?