Getting duplicate value in sql - sql

Query is below:
select
C.ngo_seq_no, B.member_name, B.member_pan_no, B.member_aadhar_no,
C.dept_name, C.source, C.fin_year, C.amt_sanctioned, C.comment,
R.member_designation_desc
from
member_designation R, ngo_member_detail B
inner join
ngo_source_fund C on B.ngo_seq_no = C.ngo_seq_no
where
mem_designation = member_designation_id::text
and C.ngo_seq_no = '15' ;

15;"Sanjay";"43537459874";"346555378895";"Nic";"2";"29922";"Nothing"
15;"Sanjay";"43537459874";"346555378895";"Nic";"2";"29922";"Nothing"
15;"Sanjay";"43537459874";"346555378895";"Nic";"2";"29922";"Nothing"
15;"Ajay ";"587592894832";"646738972456";"Nic";"2";"29922";"Nothing"
15;"Ajay ";"587592894832";"646738972456";"Nic";"2";"29922";"Nothing"
15;"Ajay ";"587592894832";"646738972456";"Nic";"2";"29922";"Nothing"
15;"Rahul";"4353745983";"346732856575";"Nic";"2";"29922";"Nothing"
15;"Rahul";"4353745983";"346732856575";"Nic";"2";"29922";"Nothing"
15;"Rahul";"4353745983";"346732856575";"Nic";"2";"29922";"Nothing"

Related

Syntax select in SQL

I have 2 tables with data like below :
TransM
TransD
The tables can see in the below
When I use syntax like below :
SELECT M.Kode_Project, M.Nama_Project, M.Kode_Panel,
M.Nama_Panel,D.Kode_Stater,
D.Nama_Stater1,
D.Tipe, D.Komponen, D.Pole, D.Amps, D.Merek, D.Qty,
D.IsTitipan
FROM TransM M
INNER JOIN TransD D
ON M.Kode_Project = D.Kode_Project
AND M.Kode_Panel = D.Kode_Panel
WHERE (M.Kode_Project = '17-0002')
AND (M.Kode_Panel = 'A')
Tables And Result
What's the wrong in my syntax? Pls Help Me
could be you need the distinct result (this should remove duplicated rows)
SELECT DISTINCT
M.Kode_Project, M.Nama_Project, M.Kode_Panel,
M.Nama_Panel,D.Kode_Stater,
D.Nama_Stater1,
D.Tipe, D.Komponen, D.Pole, D.Amps, D.Merek, D.Qty,
D.IsTitipan
FROM TransM M
INNER JOIN TransD D
ON M.Kode_Project = D.Kode_Project
AND M.Kode_Panel = D.Kode_Panel
WHERE (M.Kode_Project = '17-0002')
AND (M.Kode_Panel = 'A')

How to Use Case statement in Left outer Join using Operator +?

Query:
select /*+ full(c)parallel(c,4) */ a.*
from ioct_inv_item_all a,
(select udac_group_name, function_order , db_region, workflow_type, change_level
from ioct_function_target
where udac_group_name = 'Banner'
and function_name = 'emptyParam'
and workflow_type = 'CHGFCLTR'
and ('emptyParam' = 'emptyParam' or ('CHGFCLTR' != 'NEWITM' AND change_level = 'emptyParam') or ('CHGFCLTR' = 'NEWITM' and change_level = 'Simple') )
and sysdate between effective_from_date and effective_to_date ) b , item_ca c ,product_ca d,assignment_ca ac ,branches b
where a.udac_group_name = 'Banner'
and a.workflow_type = 'CHGFCLTR'
and a.DB_REGION = b.db_region (+)
and a.workflow_type = b.workflow_type (+)
and a.udac_group_name = b.udac_group_name (+)
and a.change_level = b.change_level (+)
and a.product_code = c.product_code(+)
and a.product_issue_num = c.product_issue_num(+)
and a.item_id = c.item_id(+)
and a.customer_id = c.customer_id(+)
and c.product_code = d.product_code(+)
and c.product_issue_num = d.product_issue_num(+)
and c.product_issue_year = d.product_issue_year(+)
and c.customer_id = d.customer_id(+)
and (case when c.contract_assignment_id IS NOT NULL
AND c.contract_assignment_id > 0
THEN c.contract_assignment_id
ELSE d.regular_assign_id
END) = ac.assignment_id(+)
and ('emptyParam' = 'emptyParam' OR ('CHGFCLTR' = 'NEWITM' AND a.eue_normal_ind = 'emptyParam') OR ('CHGFCLTR' != 'NEWITM' AND a.change_level = 'emptyParam') )
and (('emptyParam') IN ('emptyParam') OR a.region IN ('emptyParam'))
and ('emptyParam' = 'emptyParam'
OR (b.function_order is not null and a.assignee_group_seq = b.function_order)
OR (b.function_order is null and 'emptyParam' = 'emptyParam')
Error:
error:
ORA-01417: a table may be outer joined to at most one other table
01417. 00000 - "a table may be outer joined to at most one other table"
*Cause: a.b (+) = b.b and a.c (+) = c.c is not allowed
*Action: Check that this is really what you want, then join b and c first
in a view.
Error at Line: 28 Column: 20
Does any body suggest any alternative to do same thing without using keyword LEFT OUTER JOIN because i do not want to change the structure of sql query already Written?
The basic idea is that you start with a driving table and then outer join it to other tables. If a is your driving table, then
select x
from a,b,c
where a.x = b.x(+)
and a.y = c.y(+)
is the sort of query oracle expects in the join.
If you really want outer join a table with multiple "driving" tables, you can to use a syntax like this, as pointed out in the error description
Assuming you want to outer join "a" to both "b" and "c"
select *
from a,
(select *
from a,c
where c.y = a.y(+)
)
where b.x = a.x(+)
;

Oracle SQL - using the coalesce function

I really can't get my head around the coalesce function ... or if this is even the best way to get the result I'm trying to achieve.
I have three dates in the following script (iv.dated, iv1.dated, dh.actshpdate). When I run the following script the dates are in separate columns (as expected);
select unique li.catnr, li.av_part_no, li.artist||' / '||li.title description, li.cust_catnr pallet_ref,
trunc(iv.dated), trunc(iv1.dated), trunc(dh.actshpdate)
from leos_item li
left join invtran_view_oes iv
on li.av_part_no = iv.part_no
and (iv.transaction = 'NREC' and iv.location_no = ' RETURNS W')
left join invtran_view_oes iv1
on li.av_part_no = iv1.part_no
and (iv1.transaction = 'CORR+' and iv1.remark like 'STOCK FROM SP PALLET%')
left join oes_delsegview od
on od.catnr = li.catnr
and od.prodtyp = li.prodtyp
and od.packtyp = li.packtyp
left join oes_dpos dp
on od.ordnr = dp.ordnr
and od.posnr = dp.posnr
and od.segnr = dp.segnr
left join oes_dhead dh
on dp.dheadnr = dh.dheadnr
where li.cunr = '816900'
and substr(li.catnr,1,5) in ('RGMCD','RGJCD')
and li.item_type = 'FP'
and li.catnr = 'RGJCD221'
What I would like to achieve is one column with all dates in date order.
I tried replacing my dates with ...
trunc(coalesce(iv.dated, iv1.dated, dh.actshpdate)) transaction_date
... but, I lose some of the dates;
How can I achieve the following result?
You could use UNION in the following way -
WITH DATA AS(
<your query goes here>
)
SELECT A, b, c, d, e FROM DATA
UNION
SELECT A,b,c,d,f FROM DATA
UNION
SELECT A,b,c,d,g FROM DATA
where a, b, c, d, e, f, g are the column alias of the select list in your original query. You can give your own column alias in the UNION query.

SQL - Avoiding repetitive columns

This is my code where I want to display the A.SITES_CODE and A.SITES_NAME just once where they are exactly the same. Here is my code where I tried the HAVING function but it does not meat my criteria.
SELECT A.SITES_CODE, A.SITES_NAME, D.DESCRIPTION
FROM ZZZ_SITES_AVAILABLE A, ZZZ_DIFFICULTY_RATINGS D, ZZZ_SITES_VISITED V, ZZZ_HOLIDAY_DETAILS H
WHERE A.SITES_CODE = V.SITES_CODE
AND V.CODE_OF_HOLIDAY = H.CODE_OF_HOLIDAY
AND H.DIFFICULTY_RATING = D.HOLIDAY_DIFFICULTY
AND LENGTH(D.DESCRIPTION) > 6
GROUP BY A.SITES_CODE, A.SITES_NAME, D.DESCRIPTION
HAVING COUNT(*) >= 1
ORDER BY A.SITES_CODE;
You need to remove description from the group by. Along the way, you should also learn to use proper ANSI standard JOIN syntax:
SELECT A.SITES_CODE, A.SITES_NAME
FROM ZZZ_SITES_AVAILABLE A JOIN
ZZZ_SITES_VISITED V
ON A.SITES_CODE = V.SITES_CODE JOIN
ZZZ_HOLIDAY_DETAILS H
ON V.CODE_OF_HOLIDAY = H.CODE_OF_HOLIDAY JOIN
ZZZ_DIFFICULTY_RATINGS D
ON H.DIFFICULTY_RATING = D.HOLIDAY_DIFFICULTY
WHERE LENGTH(D.DESCRIPTION) > 6
GROUP BY A.SITES_CODE, A.SITES_NAME
HAVING COUNT(*) >= 1
ORDER BY A.SITES_CODE;

How To Combine These 2 Queries into one Query without using union

SELECT b.KPCNO
,b.KPC_FULL_NAME
FROM xxkpc_hr_personnel_v2 b
WHERE b.type(+) = 'KPC Employee'
AND b.DESIGNATION is null
AND b.kpcno IS NOT NULL
AND b.ORGANIZATION_ID=(select g.ORGANIZATION_ID from xxkpc_fn_web_personnel_v g where g.kpcno = :kpcno)
GROUP BY b.KPCNO
,b.kpc_full_name
UNION
SELECT b.KPCNO
,b.KPC_FULL_NAME
FROM xxkpc_hr_pos_struct_code_comb a
,xxkpc_hr_personnel_v2 b
WHERE b.position_id(+) = a.child_position_id
AND b.type(+) = 'KPC Employee'
AND b.kpcno IS NOT NULL
AND a.PERANT_POSITION_ID = (select g.POSITION_ID from xxkpc_fn_web_personnel_v g where g.kpcno = :kpcno)
GROUP BY b.KPCNO
,b.kpc_full_name
Please I need to combine these 2 queries into one query without using UNION
I think you can write it like:
select distinct b.kpcno, b.kpc_full_name
from xxxkpc_hr_personnel_v2 b
where b.type(+) = 'KPC Employee'
and b.kpcno is not null
and (
(b.designation is null and
b.organization_id in (select g.organization_id
from xxkpc_fn_web_personnel_v g
where g.kpcno = :kpcno))
or
(b.position_id in (select a.child_position_id
from xxkpc_hr_pos_struct_code_comb a
where a.parent_position_id = (select g.position_id
from xxkpc_fn_web_personnel_v g
where g.kpcno = :kpcno)))
)
as far as I see you are using the group by to make the result distinct, so I used the proper key word. And a and b are not left joined, because in the next where anded clause you are saying a.parent_position_id = .....
SELECT
b.KPCNO,
b.KPC_FULL_NAME
FROM
xxkpc_hr_personnel_v2 b
LEFT OUTER JOIN xxkpc_hr_pos_struct_code_comb a ON b.position_id = a.child_position_id
INNER JOIN xxkpc_fn_web_personnel_v g ON g.kpcno = :kpcno
WHERE
b.type = 'KPC Employee'
AND b.kpcno IS NOT NULL
AND ((b.DESIGNATION IS NULL AND g.ORGANIZATION_ID = b.ORGANIZATION_ID)
OR (a.PERANT_POSITION_ID = g.POSITION_ID))
Do you have anyway to join the two tables? It looks like you can join on b.KPCNO or b.KPC_FULL_NAME.