Syntax select in SQL - 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')

Related

Getting duplicate value in 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"

Using a REPLACE function in a SELECT CASE query

I am writing a script where the cable_no is the foreign key to the other tables that I will be joining.
An example of the cable_no may look like this "A01234567B". However, the cable_no in other tables is "A01234567" (basically its the same record without the B at the end)
How do I construct my script such that I remove all the "B" in the cable_no before joining to other tables?
select cc.riser_fid as RISER_FID, cc.cbl_fid as ZH_CBL_FID, f.cable_no AS ZH_CBL_NAME, FM.FACILITY_ID as FACILITY_ID, FC.CHANNEL_ID AS CHANNEL_ID,
ft.FACILITY_TRAIL_ITM_ID AS FACILITY_TRAIL_ITM_ID, ft2.parent_fac_trl_itm_id as parent_fac_trl_itm_id, FT2.TRAIL_SEQ_NO, FT2.A_SITE_ID, ft2.asgn_channel_id,
CASE WHEN
F.CABLE_NO LIKE '%B' REPLACE('B', '')
,CASE
WHEN ft2.a_site_id = a.a_site_id then 1
else null
END EXIST_IN_GTECH
from sgtel10.connected_cables cc
JOIN gc_fcbl_temp f on f.g3e_fid = cc.cbl_fid AND f.ltt_id in (0,888888888)
JOIN FACILITY_MASTER_TEMP FM ON FM.FACILITY_NAME = f.cable_no
JOIN FACILITY_CHANNEL_TEMP FC ON FC.FACILITY_ID = FM.FACILITY_ID AND FC.CHANNEL_ID IS NOT NULL
JOIN FACILITY_TRAIL_ITEMS_TEMP ft on ft.facility_trail_itm_id = fc.facility_trail_itm_id
JOIN FACILITY_TRAIL_ITEMS_TEMP ft2 on ft2.parent_fac_trl_itm_id = ft.parent_fac_trl_itm_id and ft2.trail_seq_no = 1.0
LEFT JOIN A_SITE_ID A ON A.A_SITE_ID = FT2.A_SITE_ID)
;
You can do something like this:
t1 join
t2
on t1.cable_no = t2.cable_no || 'B'
|| is the standard operator for string concatenation.

Multiply two column on a inner Join

help needed.
i want to multiply two column and display result to a new column(totalQty).
TotalQty= (ITY00.CMPITQTY * MPOS_GP_InvTransaction.Quantity)
HOW CAN I ACHIEVE THIS ?
Select
MPOS_GP_InvTransaction.id,
MPOS_GP_InvTransaction.[Type],
MPOS_GP_InvTransaction.Vendor,
MPOS_GP_InvTransaction.Currency,ITY00.CMPTITNM,
MPOS_GP_InvTransaction.BatchId,
MPOS_GP_InvTransaction.UserId,
MPOS_GP_InvTransaction.ItemNo,
MPOS_GP_InvTransaction.SiteId,
ITY00.CMPITQTY,
MPOS_GP_InvTransaction.Quantity,
MPOS_GP_InvTransaction.IntegrationFlag
From DB_37788.dbo.MPOS_GP_InvTransaction
INNER JOIN TWCL.dbo.ITY00 ON ITY00.ITEMNMBR=MPOS_GP_InvTransaction.ItemNo
Where (MPOS_GP_InvTransaction.ItemNo like '%-GTYR%' )
OR (MPOS_GP_InvTransaction.ItemNo like '%-JKOP%' )
And (MPOS_GP_InvTransaction.SiteId IN('MM-DC-ZZQW','MM-DC-TTYR') )
And (MPOS_GP_InvTransaction.IntegrationFlag = 0 )
SELECT MPOS_GP_InvTransaction.id,
MPOS_GP_InvTransaction.Type,
MPOS_GP_InvTransaction.Vendor,
MPOS_GP_InvTransaction.Currency,
ITY00.CMPTITNM,
MPOS_GP_InvTransaction.BatchId,
MPOS_GP_InvTransaction.UserId,
MPOS_GP_InvTransaction.ItemNo,
MPOS_GP_InvTransaction.SiteId,
ITY00.CMPITQTY,
MPOS_GP_InvTransaction.Quantity,
MPOS_GP_InvTransaction.IntegrationFlag,
ITY00.CMPITQTY * MPOS_GP_InvTransaction.Quantity AS TotalQty
FROM DB_37788.dbo.MPOS_GP_InvTransaction
INNER JOIN TWCL.dbo.ITY00 ON ITY00.ITEMNMBR = MPOS_GP_InvTransaction.ItemNo
WHERE(MPOS_GP_InvTransaction.ItemNo LIKE '%-GTYR%')
OR (MPOS_GP_InvTransaction.ItemNo LIKE '%-JKOP%')
AND (MPOS_GP_InvTransaction.SiteId IN('MM-DC-ZZQW', 'MM-DC-TTYR'))
AND (MPOS_GP_InvTransaction.IntegrationFlag = 0);

Alias to a join query

( SELECT Vraboteni.v, Ulogi.p, Zarabotuva.honorar
FROM Vraboteni, Ulogi, Zarabotuva
WHERE Vraboteni.v = Ulogi.v
AND ima_uloga='sporedna'
AND Ulogi.p = Zarabotuva.p
) as F
JOIN
( SELECT Vraboteni.v, Ulogi.p, Zarabotuva.honorar
FROM Vraboteni, Ulogi, Zarabotuva
WHERE Vraboteni.v = Ulogi.v
AND ima_uloga='glavna'
AND Ulogi.p = Zarabotuva.p
) as S
ON (F.honorar > S.honorar)
Can anyone tell me what is wrong with the syntax that I am using above? I'm having the same issue over multiple queries and I'm not sure I quite understand how I am supposed to assign an alias when I use a join (having the same issue when trying to assign alliases to multiple nested joins)
The subselects you join should be considered as a normal table or view, so imagine they are, and your select statement looks like this:
SELECT1 as F
JOIN SELECT2 as S ON (F.honorar > S.honorar)
This statement is missing essential parts, like a SELECT and FROM clause.
So fix it, if you want to join two selects, you should encapsulate them in another select, so you get:
SELECT
S.*,
F.*
FROM
(SELECT ... ) AS F
JOIN (SELECT ...) AS S ON (F.honorar > S.honorar)
Alternatively, you could get rid of the two subselects, use normal joins for all your tables, and end up with a query like this:
SELECT
Vraboteni.v, Ulogi.p, Zarabotuva.honorar
FROM
Vraboteni AS v1
JOIN Ulogi AS u1 ON v1.v = u1.v
JOIN Zarabotuva AS z1 ON u1.p = z1.p
CROSS JOIN Vraboteni AS v2 -- Not sure if you would want/need a condition here
JOIN Ulogi AS u2 ON v2.v = u2.v
JOIN Zarabotuva AS z2 ON u2.p = z2.p
WHERE
v1.ima_uloga = 'sporedna' -- Not sure if this should be v1, u1 or z1
AND v2.ima_uloga = 'glavna'
AND z1.honorar > z2.honorar
this is what you need to do:
SELECT *
FROM
(SELECT Vraboteni.v
, Ulogi.p
, Zarabotuva.honorar
FROM Vraboteni
, Ulogi
, Zarabotuva
WHERE Vraboteni.v = Ulogi.v
AND ima_uloga = 'sporedna'
AND Ulogi.p = Zarabotuva.p) AS F
JOIN
(SELECT Vraboteni.v
, Ulogi.p
, Zarabotuva.honorar
FROM Vraboteni
, Ulogi
, Zarabotuva
WHERE Vraboteni.v = Ulogi.v
AND ima_uloga = 'glavna'
AND Ulogi.p = Zarabotuva.p) AS S ON F.honorar > S.honorar;

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.