How to group and uniqe each row with ORACLE? - oracle

Anyone please help me, how to uniqe each row
SELECT T3.ID_JS,T5.DIVISI_AREA,T4.NAME_METHODE_REPAIR, T1.URUTAN AS SORT, TO_DATE(T1.TIME_FINISHED_WORK,'DD/MM/YYYY') AS DATE_FINISHED
FROM TB_WORK T1
JOIN TB_INSPECTION T2 ON T1.ID_INSPECTION=T2.ID_INSPECTION
JOIN TB_JOBSHEET T3 ON T2.ID_JS = T3.ID_JS
JOIN TB_METHODE_REPAIR T4 ON T1.ID_METHODE_REPAIR=T4.ID_METHODE_REPAIR
JOIN TB_DIVISI T5 ON T1.ID_DIVISI=T5.ID_DIVISI
where t3.id_js=142414
GROUP BY T3.ID_JS,T5.DIVISI_AREA,T4.NAME_METHODE_REPAIR, T1.URUTAN,TO_DATE(T1.TIME_FINISHED_WORK,'DD/MM/YYYY')
ORDER BY T3.ID_JS, TO_DATE(T1.TIME_FINISHED_WORK,'DD/MM/YYYY') desc
[Result] (http://prntscr.com/sfpuuq)

Those rows are unique, so I'll guess: there are two rows with sort = 3 and you'd want to have only one. If that's so, regarding the fact that date value is the only thing that makes difference, a simple way is to use aggregate function (such as min or max). I used max, but you can change it if you want:
SELECT t3.id_js,
t5.divisi_area,
t4.name_methode_repair,
t1.urutan AS sort,
MAX (TO_DATE (t1.time_finished_work, 'DD/MM/YYYY')) AS date_finished
FROM tb_work t1
JOIN tb_inspection t2 ON t1.id_inspection = t2.id_inspection
JOIN tb_jobsheet t3 ON t2.id_js = t3.id_js
JOIN tb_methode_repair t4
ON t1.id_methode_repair = t4.id_methode_repair
JOIN tb_divisi t5 ON t1.id_divisi = t5.id_divisi
WHERE t3.id_js = 142414
GROUP BY t3.id_js,
t5.divisi_area,
t4.name_methode_repair,
t1.urutan
ORDER BY t3.id_js, date_finished DESC
You'd remove date column from GROUP BY; besides, if you wanted to select distinct rows, why didn't you apply select distinct instead of using group by? Result is the same, but - group by is generally used with aggregates, not to return distinct result.

Related

How to tuning is not null to range scan index in Oracle explain plan?

I m building a search query right now. I used the Oracle 19c database.
sample query:
select * from t1
inner join t2 on t2.id = t1.id and t2.name <> 'AKA'
left outer join t3 Buyer on Buyer.id = t1.id and Buyer.userId is null
left outer join t3 Seller on Seller.id = t1.id and Seller.userId = t1.userId
where rownum < 500;
This query runs super slow without indexes. It took like 14s to feed the result.
I checked the explain plan and give some indexes to the database. The query time down to 3s! Yeah. BUT,BUT,BUTBBBBBUT. It's not enough. I need it to react even faster like 1s.
I rechecked the autotrace. There still two place use full scan event I gave them the index.
The first is
INDEX t2.name
- Filter Predicates
- t2.NAME<>'AKA'
Is there a way to do <> index in Oracle? I tried build index in multiple ways. For example,
create index t2_name_idx on t2 (case when name <> 'AKA' then name end);, not work. Also the sample way like create index t2_name_idx on t2 (name); not works too.
The second is
HASH JOIN RIGHT OUTER 890639 556473 400400 2906406
Access Predicates
AND
T3.ID=T1.ID
T3.USERID=T1.USERID
TABLE ACCESS
ADDRESSES BY INDEX ROWID BATCHED 1225069 314696 384117 596570
INDEX
T3_UERID FULL SCAN 1225069 4708 4678 121216
Filter Predicates
T3.USERID IS NOT NULL
Above means that when doing left outer join t3 Seller on Seller.id = t1.id and Seller.userId = t1.userId it will check the t3.userid is not null first. The index T3_USERID is just a simple non-unique index on table T3 col T3.userid.
Any help or hint will be appreciated.

join three tables with count and sum in oracle

I have three tables and trying to join these tables with count(working_days) and sum(Overtime) functions but overtime is not showing correct i am trying the query is.
SELECT E.EMP_CODE,E.EMP_NAME,
COALESCE(SUM(O.OVERTIME),0) AS OVERTIME,COALESCE(COUNT(C.EMP_ATT),0) WORKED_DAYS
FROM EMP E
LEFT JOIN OVERTIME O
ON E.EMP_CODE = O.EMP_CODE
LEFT JOIN ATT_REG C
ON E.EMP_CODE=C.EMP_CODE
GROUP BY E.EMP_CODE,E.EMP_NAME
ORDER BY EMP_CODE
Yes, You will get multiple records of the overtime for employees as shift date (or similar in your table) is missing in the join.
SELECT E.EMP_CODE,E.EMP_NAME,
COALESCE(SUM(O.OVERTIME),0) AS OVERTIME,
COALESCE(COUNT(C.EMP_ATT),0) WORKED_DAYS
FROM EMP E
LEFT JOIN ATT_REG C
ON E.EMP_CODE=C.EMP_CODE
LEFT JOIN OVERTIME O
ON E.EMP_CODE = O.EMP_CODE
AND C.SHIFTDATE = O.SHIFDATE -- YOU NEED SOMETHING LIKE THIS
GROUP BY E.EMP_CODE,E.EMP_NAME
ORDER BY EMP_CODE

Oracle 11g - Selecting multiple records from a table column

I was just wondering how you select multiple records from a table column. Please see below the query.
SELECT DISTINCT DEPARTMENT_NAME, CITY, COUNTRY_NAME
FROM OEHR_DEPARTMENTS
NATURAL JOIN OEHR_EMPLOYEES
NATURAL JOIN OEHR_LOCATIONS
NATURAL JOIN OEHR_COUNTRIES
WHERE JOB_ID = 'SA_MAN' AND JOB_ID = 'SA_REP'
;
Basically, I want to be able to select records from the table column I have, however when you use AND it only displays SA_MAN and not SA_REP. I have also tried to use OR and it displays no rows selected. How would I actually be able to select both Job ID's without it just displaying one or the other.
Sorry this may sound like a stupid question (and probably not worded right), but I am pretty new to Oracle 11g SQL.
For your own comfort while debugging, I suggest you to use inner joins instead of natual joins.
That where clause is confusing, if not utterly wrong, because you don't make clear which tables' JOB_ID should be filtered. Use inner joins, give aliases to tables, and refer to those aliases in the where clause.
select distinct DEPARTMENT_NAME, CITY, COUNTRY_NAME
from OEHR_DEPARTMENTS t1
join OEHR_EMPLOYEES t2
on ...
join OEHR_LOCATIONS t3
on ...
join OEHR_COUNTRIES t4
on ...
where tn.JOB_ID = 'SA_MAN' AND tm.JOB_ID = 'SA_REP'
After rephrasing your query somehow like this, you'll have a clearer view on the logical operator you'll have to use in the where clause, which I bet will be an OR.
EDIT (after more details were given)
To list the departments that employ staff with both 'SA_MAN' and 'SA_REP' job_id, you have to join the departments table with the employees twice, once with the filter job_id='SA_MAN' and once with job_id='SA_REP'
select distinct DEPARTMENT_NAME, CITY, COUNTRY_NAME
from OEHR_DEPARTMENTS t1
join OEHR_EMPLOYEES t2
on t1.department_id = t2.department_id --guessing column names
join OEHR_EMPLOYEES t3
on t1.department_id = t3.department_id --guessing column names
join OEHR_LOCATIONS t4
on t1.location_id = t4.location_id --guessing column names
join OEHR_COUNTRIES t5
on t4.country_id = t5.country_id --guessing column names
where t2.job_id = 'SA_MAN' and t3.job_id = 'SA_REP'
order by 1, 2, 3

how to get the count(id) from the multiple tables using join query

I want to display count based on the id from multiple tables. for two tables it is working fine but for three tables it is not displaying data
this is my query for three tables it is not working
select r.req_id
, r.no_of_positions
, count(j.cand_id) as no_of_closure
, count(cis.cand_id)
from requirement r
join joined_candidates j
on r.req_id=j.req_id
join candidate_interview_schedule cis
on cis.req_id=r.req_id
where cis.interview_status='Interview Scheduled'
group by r.req_id, r.no_of_positions;
Changed to left joins incase value doens't exist in a table
Changed count to use an window function so counts are not artificially inflated by joins
moved where clause to join criteria as on a left join, it would negate the null values, making it operate like a inner join.
..MAYBE...
SELECt r.req_id
, r.no_of_positions
, count(j.cand_id) over (partition by J.cand_ID) as no_of_closure
, count(cis.cand_id) over (partition by cis.cand_id) as no_of_CIS_CNT
FROM requirement r
LEFT join joined_candidates j
on r.req_id=j.req_id
LEFT join candidate_interview_schedule cis
on cis.req_id=r.req_id
and cis.interview_status='Interview Scheduled'
GROUP BY r.req_id, r.no_of_positions;
or perhaps... (if I can assume j.cand_ID and cis.cand_ID are unique) also to eliminate artificial count increase due to 1:M joins
SELECt r.req_id
, r.no_of_positions
, count(distinct j.cand_id) as no_of_closure
, count(distinct cis.cand_id) as no_of_CIS_CNT
FROM requirement r
LEFT join joined_candidates j
on r.req_id=j.req_id
LEFT join candidate_interview_schedule cis
on cis.req_id=r.req_id
and cis.interview_status='Interview Scheduled'
GROUP BY r.req_id, r.no_of_positions;

LINQ vb.net GROUP BY - SELECT JOINED column

I have a very basic SQL query I am trying to reproduce:
SELECT t1.id, t1.name, count(*), min(t2.inserted) as inserted_first, max(t2.inserted) as inserted_last
FROM tbl1 t1
LEFT JOIN tbl2 t2 ON t1.id=t2.tbl1_id
WHERE t2.search=15
GROUP BY t1.id, t1.name
This works perfect for me. It allows me to group by the unique items of t1.id and t1.name, but also get the amount of times this pair appears, and also the min and max value of t2.inserted for the linked table. Problem is, now when I turn this into LINQ I get:
Dim query =
From t1 In ent.TBL1
Join t2 In ent.TBL2 On t1.id Equals t2.tbl1_id
Where (t2.search=15)
Group t1 By t1.id, t1.name Into Group
Select New With {
.id = id,
.name = name,
.count = Group.Count,
.min_value = ???,
.max_Value = ???
}
I am lost as to what i can do to select the min and max. Group.Min would work if it was of the same table as the grouping, however since it is in t2, I am not able to reference it. Also I can not add it to my group by, since its different.
Note that tbl2 links to tbl1 on tbl2.tbl1_id -> tbl1.id. Also this is a dumbed down example of my problem, not the real live schema.
I appreciate any help on this matter
I'm not a VB person, but I think you want:
Group t1, t2 By t1.id, t1.name Into Group
Select New With {
.id = id,
.name = name,
.count = Group.Count,
.min_value = Group.Min(Function(x) x.t2.Inserted),
.max_Value = Group.Max(Function(x) x.t2.Inserted)
}
Note how here each group element contains both t1 and t2, which is how you're able to get to the Inserted property. It's not actually clear whether you even need t1 or any other parts of t2, so you may be able to use:
Group t2.Inserted By t1.id, t1.name Into Group
Select New With {
.id = id,
.name = name,
.count = Group.Count,
.min_value = Group.Min(),
.max_Value = Group.Max()
}
Unfortunately my VB LINQ skills aren't up to the challenge of knowing whether that's right for you. If you'd be happy with it in C# (which you could then convert) I could help you more easily...

Resources