Getting data based on Priority - sql

I have a table with structure as below:
'Type' column has three type of values that is 1,2 and 3.
Each Type has certain Rule (mentioned in RULE column)
Same rule can be associated with multiple Type records.
Now I want to get only those records as output which has higher value.
For example:
'Bench Type' is a rule associated with Type 1,2 and 3 so we will pick Rule with Value with Type 3, that is Single.
'Complexity' is with Type 1 & 2 so we will pick rule Value 5.
If a Rule is present in just one Type then it will be picked as it is.
I tried following query but it is working with Type 1 & 2.
select *
FROM CX_ROSTER_RULES_VIEW v
INNER JOIN
(
select RULE, MAX("Type") AS MAX_TYPE
FROM CX_ROSTER_RULES_VIEW
GROUP BY RULE
) v1
ON v.RULE = v1.RULE
AND v."Type" = v1.MAX_TYPE
WHERE ( branch = 'Civil' AND category = 'C.M. (Civil)' AND sub_category = 'Pauper' )
OR ( branch = 'Civil' AND category = 'C.M. (Civil)' AND sub_category IS NULL )
OR ( branch = 'Civil' AND category IS NULL AND sub_category IS NULL )

Use the ROW_NUMBER() analytic function then you will not need to do a self-join:
SELECT *
FROM (
SELECT v.*,
ROW_NUMBER() OVER ( PARTITION BY Rule ORDER BY "Type" DESC ) AS rn
FROM CX_ROSTER_RULES_VIEW v
WHERE branch = 'Civil'
AND ( category IS NULL
OR ( category = 'C.M. (Civil)'
AND ( sub_category IS NULL OR sub_category = 'Pauper' )
)
)
)
WHERE rn = 1;

i would do this.
(select rule, max(type) as type from yourtable
group by rule) --this will give you the rule with the highest priority
with that do this
select m.* from CX_ROSTER_RULES_VIEW as m inner join (select rule, max(type) as type from CX_ROSTER_RULES_VIEW
group by rule) p on m.rule = p.rule and m.type = p.type
don't sql on this machine, if the above don't work try this. I will be able to test tomorrow if you don't have it complete.
select m.* from (select rule, max(type) as type from CX_ROSTER_RULES_VIEW
group by rule) as p left join CX_ROSTER_RULES_VIEW as m on p.rule = m.rule and p.type = m.type
it's pretty much the same, just want to see if a left join will do anything different.

Try this:
select v.* from
CX_ROSTER_RULES_VIEW v join
(select rule, max(type) as maxType from CX_ROSTER_RULES_VIEW group by rule) M
on v.Rule = M.Rule and v.type = M.maxType

Related

Display multiple records in one 1 row with multiple columns

I have a table that stores user info such as:
I need to write a query that returns the results in the following format:
I tried doing a LEFT JOIN for each status but that didn't work, any thoughts on how to get the expected results?
if number of statuses is fixed you can do this
Select
id user_id,
open_status,
open_status_date,
inprogress_status,
inprogress_status_date,
complete_status,
complete_status_date
from
(select user_id id from yourTable group by user_id) U left join
(select user_id id, status open_status, status_date open_status_date
from yourTable where status = 'Open') O on U.id = O.id left join
(select user_id id, status inprogress_status, status_date inprogress_status_date
from yourTable where status = 'InProgress') P on U.id = P.id left join
(select user_id id, status complete_status, status_date complete_status_date
from yourTable where status = 'Complete') C on U.id = C.id
Order by id
Break into inline views and join. But this may be not the most efficient way.
ALSO NOTE: if each user definitely has at least "Open" status, you can skip first U inline view and start with O
You want to use a pivot, like this:
select * from test
PIVOT(
max(status_date)
FOR status
IN (
'Open',
'In Progress',
'Complete'
)
)
order by user_id
Suppose every user has a "Open" status
With
open as (select * from table where status = 'Open'),
inp as (select * from table where status = 'In Progress'),
comp as (select * from table where status = 'Complete')
select o.user_id,o.status open_status, o.status_date open_status_date,i.status InProgress_status, i.status_date InProgress_status_date,c.status complete_status, c.status_date complete_status_date
from open o, inp i, comp c
where o.user_id=i.user_id(+)
and o.user_id=c.user_id(+)

Left outer join and necessary subquery with date in Oracle

I'm struggling with a left outer join where the right portion, the one that could contain nulls, doesn't show me values. I think the situation is called an implicit inner join. The reason is that the subquery doesn't return nulls but rather nothing for what I'd like to have on my right part.
(This question differs from Left Outer Join Not Working? in that a subquery to find the maximum date is needed.)
Here is a simplified example:
Table Contracts:
customer_id, status
Table Values:
customer_id, value_code, value, begin_date
I want to display all customers with status = 'active' and the latest value for a certain value_code, lets say 'volume'. There are more value_codes and the values have a certain date from which they are valid. There can also be no value_code BUT then I want a NULL on the right side.
So here is what I tried to do:
SELECT * FROM CONTRACTS C
LEFT JOIN VALUES V ON C.CUSTOMER_ID = V.CUSTOMER_ID
AND VALUE_CODE = 'VOLUME'
WHERE C.STATUS = 'ACTIVE'
AND V.BEGIN_DATE = (
SELECT MAX(BEGIN_DATE) FROM VALUES V2
WHERE V2.CUSTOMER_ID = V.CUSTOMER_ID
AND V2.VALUE_CODE = 'VOLUME'
)
I can't put the subquery in the join clause, Oracle won't accept that. On the other hand my subquery makes it so that all the rows that have no entry for a value with code 'volume' are omitted. What I want is to have value = NULL instead with all the customers on the left.
Thanks for helping!
Filter the VALUE rows first and then LEFT JOIN:
SELECT *
FROM CONTRACTS C
LEFT JOIN
( SELECT *
FROM (
SELECT V.*,
ROW_NUMBER() OVER ( PARTITION BY CUSTOMER_ID ORDER BY BEGIN_DATE DESC )
AS rn
FROM VALUES V
)
WHERE rn = 1
) V
ON ( C.CUSTOMER_ID = V.CUSTOMER_ID
AND VALUE_CODE = 'VOLUME' )
WHERE C.STATUS = 'ACTIVE'
As MT0 suggested using partitioning and sorting by row number helped. Only had to include value_code in the partitioning for my purpose.
So this query finally did what I wanted:
SELECT *
FROM CONTRACTS C
LEFT JOIN
( SELECT *
FROM (
SELECT V.*,
ROW_NUMBER() OVER ( PARTITION BY CUSTOMER_ID, VALUE_CODE ORDER BY BEGIN_DATE DESC )
AS rn
FROM VALUES V
)
WHERE rn = 1
) V
ON ( C.CUSTOMER_ID = V.CUSTOMER_ID
AND VALUE_CODE = 'VOLUME' )
WHERE C.STATUS = 'ACTIVE'

Oracle SQL Correlated subquery - Returning count(*) in some columns

I have my initial statement which is :
SELECT TEAM.ID PKEY_SRC_OBJECT,
TEAM.MODF_DAT UPDATE_DATE,
TEAM.MODF_USR UPDATED_BY,
PERSO.FIRST_NAM FISRT_NAME
FROM TEAM
LEFT OUTER JOIN PERSO ON (TEAM.ID=PERSO.TEAM_ID)
I want to calculate some "flags" and return them in my initial statement.
There are 3 flags which can be calculated like this :
1) Flag ISMASTER:
SELECT Count(*)
FROM TEAM_TEAM_REL A, TEAM B
WHERE B.PARTY_PTY_ID = A.RLTD_TEAM_ID
AND CODE = 'Double';
2) Flag ISAGENT:
SELECT Count(*)
FROM TEAM_ROL_REL A, TEAM B
WHERE B.PARTY_PTY_ID = A.TEAM_ID;
3) Flag NUMPACTS:
SELECT Count(*)
FROM TEAM_ROL_REL A,
TEAM_ROL_POL_REL B,
PERSO_POL_STA_REL C,
TEAM D
WHERE A.ROL_CD IN ('1','2')
AND A.T_ROL_REL_ID = B.P_ROL_REL_ID
AND B.P_POL_ID = C.P_POL_ID
AND C.STA_CD = 'A'
AND D.PARTY_PTY_ID = A.TEAM_ID;
To try to achieve this, I've updated my initial statement like this :
WITH ABC AS (
SELECT TEAM.ID PKEY_SRC_OBJECT,
TEAM.MODF_DAT UPDATE_DATE,
TEAM.MODF_USR UPDATED_BY,
PERSO.FIRST_NAM FISRT_NAME
FROM TEAM
LEFT OUTER JOIN PERSO ON (TEAM.ID=PERSO.TEAM_ID)
)
SELECT ABC.*, MAST.ISMASTER, AGENT.ISAGENT, PACTS.NUMPACTS FROM ABC
LEFT OUTER JOIN (
select
RLTD_TEAM_ID,
Count(RLTD_TEAM_ID) OVER (PARTITION BY RLTD_TEAM_ID) as ISMASTER
FROM TEAM_TEAM_REL
WHERE CODE = 'Double'
) MAST
ON ABC.PKEY_SRC_OBJECT = MAST.RLTD_TEAM_ID
LEFT OUTER JOIN (
select
TEAM_ID,
Count(TEAM_ID) OVER (PARTITION BY TEAM_ID) as ISAGENT
FROM TEAM_ROL_REL
) AGENT
ON ABC.PKEY_SRC_OBJECT = AGENT.TEAM_ID
LEFT OUTER JOIN (
select
TEAM_ID,
Count(TEAM_ID) OVER (PARTITION BY TEAM_ID) as NUMPACTS
FROM TEAM_ROL_REL, TEAM_ROL_POL_REL, PERSO_POL_STA_REL
WHERE TEAM_ROL_REL.ROL_CD IN ('1','2')
AND TEAM_ROL_REL.T_ROL_REL_ID = TEAM_ROL_POL_REL.P_ROL_REL_ID
AND TEAM_ROL_POL_REL.P_POL_ID = PERSO_POL_STA_REL.P_POL_ID
AND PERSO_POL_STA_REL.STA_CD = 'A'
) PACTS
ON ABC.PKEY_SRC_OBJECT = PACTS.TEAM_ID;
For the two first flags (ISMASTER and ISAGENT) I get the result in less than 1min, but for the last flag (NUMPACTS) it runs few minutes without provide any result.
I think my statement is too heavy, maybe I should do it in a totally different way.
I think you have perhaps over complicated things.
You could do this (assuming I have understood your requirements correctly) like so:
WITH ttr AS (SELECT rltd_team_id,
COUNT(*) is_master
FROM team_team_rel
AND CODE = 'Double'
GROUP BY rltd_team_id),
trr AS (SELECT team_id,
COUNT(*) is_agent
FROM team_rol_rel
GROUP BY team_id)
pacts AS (SELECT trr1.team_id,
COUNT(*) num_pacts
FROM team_rol_rel trr1
INNER JOIN team_rol_pol_rel trpr ON (trr1.t_rol_rel_id = trpr.p_rol_rel_id)
INNER JOIN perso_pol_sta_rel ppsr ON (trpr.p_pol_id = ppsr.p_pol_id
WHERE trr1.rol_cd IN ('1', '2')
AND ppsr.st_cd = 'A'
GROUP BY trr1.team_id)
SELECT t.id pkey_src_object,
t.modf_dat update_date,
t.modf_usr updated_by,
p.first_nam first_name,
ttr.is_master,
trr.is_agent,
pacts.num_pacts
FROM team t
LEFT OUTER JOIN perso p ON t.id = p.team_id
LEFT OUTER JOIN ttr ON t.party_pty_id = ttr.rltd_team_id
LEFT OUTER JOIN trr ON t.party_pty_id = trr.team_id
LEFT OUTER JOIN pacts ON t.pkey_src_object = pacts.team_id;
N.B. untested, since you didn't provide any test data.

How can i eliminate dups?

select C.Id as candidateId,C.Name, C.Phone, Status.ResultStatusText , Status.TimeStamp, Status.notes ,
(Select count(*) from CandidateCallHistory where CandiateId = candidateId) AS numbCalls,
(SELECT SUBSTRING((SELECT ',' + Name
FROM Jobs
WHERE Id in (select value from fn_Split(c.JobIds,','))
FOR XML PATH('')),2,200000)) AS jobsList
from Candidate2 C
outer APPLY (select top 1 CH.CandiateId, CH.ResultStatusText , CH.TimeStamp , CH.notes
from CandidateCallHistory CH
where CH.CandiateId = C.Id
order by TimeStamp desc) as Status
where Status.ResultStatusText <> 'completed' and Status.ResultStatusText <> 'canceled' and c.isactive = 1
I have multiple records in the CandidateCallHistory table and seems this is causing issue with the outer apply ( i may be wrong) as it should only get the most recent record in the table since it selects top 1.
Try to add distinct in first line after select:
select distinct [...]

SQL Get lowest level child and root node

I have database schema: [Id], [ParrentId], [some more tables]
I have hierarchy like:
1. a
2. aa
3. aaa_1
3. aaa_2
1. b
2. bb
1. c
2. cc
3. ccc_1
4. cccc
3. ccc_2
I want a (select * where X) => [X, lowest leve child] like:
[a, aaa_1] [a, aaa_2]; [cc, cccc] etc.
I can get lowest child with
SELECT t1.name FROM
category AS t1 LEFT JOIN category as t2
ON t1.category_id = t2.parent
WHERE t2.category_id IS NULL;
but I don't know how to join it with root node.
Given:
The DBMS is SQL Server;
The highest level nodes of the tree have parent = NULL;
You want all the lowest leaves for all levels of the trees, not just the roots;
You want to have all the nodes at a lowest level, not just one;
This query would do it:
WITH r ( category_id, name, root, depth )
-- finds the root relationship
AS (
SELECT category_id, name, category_id, 0
FROM category
-- WHERE parent IS NULL -- this would only look at root nodes
UNION ALL
SELECT c.category_id, c.name, r.root, r.depth + 1
FROM r
JOIN category c
ON c.parent = r.category_id
), s ( category_id, name, root, window_id )
-- finds the lowest leaves
AS (
SELECT category_id, name, root, RANK() OVER(partition by root order by depth DESC)
FROM r
)
SELECT c.name AS NodeName, s.Name AS DeepLeafName
FROM category c
JOIN s
ON c.category_id = s.root
WHERE s.window_id = 1;
Here is the result set:
With SQL Server, you can try this :
With CTE as
(
Select ID as Child, lev = 1
from category
where ID = X
UNION ALL
Select category.ID, CTE.lev + 1
from category
inner join CTE ON category.ParentID = CTE.Child
)
select CTE_1.Child, CTE_2.Child
from CTE as CTE_1
inner join CTE as CTE_2
where CTE_1.lev = 1 AND CTE_2.lev = (select MAX(CTE.lev) from CTE)