Union two select statements with NOT IN - Oracle - sql

I have two select statements and i need to output 1st select records and the other should receive the records that not output by select 1. 2nd select should not contain the data of select 1. here is my code
select b.tcl_tcserno, b.tcl_clmcode, b.tcl_clname, c.prd_desc, e.eqt_desc,
b.tcl_conamount, b.tcl_intrate, f.numirp_minrate, f.dblirp_intrate,
b.tcl_prdcode, e.eqt_type
FROM leaseinfo.trn_ira_intreductapproval a,
leaseinfo.tbltrialcalculation b,
corpinfo.tblproduct c,
leaseinfo.tbltrialequipment d,
leaseinfo.tblequipmenttype e,
leaseinfo.ref_irp_inerestratepara f
WHERE a.numira_tcserno = b.tcl_tcserno
AND b.tcl_prdcode = c.prd_code
AND b.tcl_tcserno = d.teq_tcserno
AND b.tcl_prdcode = e.eqt_prdcode
AND d.teq_eqttype = e.eqt_type
AND b.tcl_prdcode = f.strirp_productcode
AND d.teq_eqttype = f.strirp_eqpttype
AND a.strira_status = 'E'
AND (f.numirp_minrate - b.tcl_intrate)<
(SELECT g.intds_uplim
FROM glinfo.ref_tblintratefordesignation g
WHERE g.intds_designation IN (
SELECT s.str_off_type
FROM dpg.inf_responsible_maildetails s
WHERE s.str_user_code = '10020336'))
and (b.tcl_prdcode='LE' OR b.tcl_prdcode='UV' OR b.tcl_prdcode='HP')
and (e.EQT_TYPE='2' OR e.EQT_TYPE='9' OR e.EQT_TYPE='15' OR e.EQT_TYPE='17' OR e.EQT_TYPE='21' OR e.EQT_TYPE='23' OR e.EQT_TYPE='25' OR e.EQT_TYPE='28' OR e.EQT_TYPE='30')
ORDER BY b.tcl_tcserno
union all
SELECT b.tcl_tcserno, b.tcl_clmcode, b.tcl_clname, c.prd_desc, e.eqt_desc,
b.tcl_conamount, b.tcl_intrate, f.numirp_minrate, f.dblirp_intrate,
b.tcl_prdcode, e.eqt_type
FROM leaseinfo.trn_ira_intreductapproval a,
leaseinfo.tbltrialcalculation b,
corpinfo.tblproduct c,
leaseinfo.tbltrialequipment d,
leaseinfo.tblequipmenttype e,
leaseinfo.ref_irp_inerestratepara f
WHERE a.numira_tcserno = b.tcl_tcserno
AND b.tcl_prdcode = c.prd_code
AND b.tcl_tcserno = d.teq_tcserno
AND b.tcl_prdcode = e.eqt_prdcode
AND d.teq_eqttype = e.eqt_type
AND b.tcl_prdcode = f.strirp_productcode
AND d.teq_eqttype = f.strirp_eqpttype
AND a.strira_status = 'E'
ORDER BY b.tcl_tcserno
i think not in will work with this. but i don't have an idea to do with it. any help would be appreciated

This will do job using set notation. Assumes same columns in both queries to work
With dat1 as (....),
Dat2 as (....)
Select * from dat1 Union all
(
Select * from dat2 except select * from dat1
)

Related

How to select twice the same column on join query?

How Can I to count the gender when is M or F, somehting like
SELECT count(N.gender)
FROM `DATABASE_T` as T
LEFT JOIN `DATABASE_N` as N
ON
T.ENCUESTA = N.ENCUESTA AND
T.P_DEPTO = N.P_DEPTO AND
T.P_MUNIC = N.P_MUNIC AND
T.COD_VEREDA = N.COD_VEREDA AND
T.PAIS = N.PAIS and
T.UC_UO = N.UC_UO
WHERE N.ID_PROD=1 and N.gender="M"
SELECT countif(N.gender = 'M') as M, countif(N.gender = 'F') as F
FROM `DATABASE_T` as T
LEFT JOIN `DATABASE_N` as N
ON
T.ENCUESTA = N.ENCUESTA AND
T.P_DEPTO = N.P_DEPTO AND
T.P_MUNIC = N.P_MUNIC AND
T.COD_VEREDA = N.COD_VEREDA AND
T.PAIS = N.PAIS and
T.UC_UO = N.UC_UO
WHERE N.ID_PROD=1
Since you did not describe the structure of your table and use Spanish-looking identifiers, I will use a clearer example with my own schema:
SELECT
SUM( CASE WHEN Sex = 'M' THEN 1 ELSE 0 END ) AS M,
SUM( CASE WHEN Sex = 'F' THEN 1 ELSE 0 END ) AS F
FROM People
WHERE People.Dept = 5

Error 1055 SQL - SELECT list is not in GROUP BY

I'm struggling with this SQL consult, the error message is:
1055 - Expression #10 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'ctrl2019.s.cgsc_cuenta' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by.
And here is the code:
SELECT c.cgcu_cuenta AS id,
g.ger_cuenta, g.ger_nombre,
p.cgp_cuenta, p.cgp_nombre,
r.rub_cuenta, r.rub_nombre,
c.cgcu_cuenta, c.cgcu_nombre,
s.cgsc_cuenta, s.cgsc_nombre,
SUM(IFNULL(a.debe, 0)) - SUM(IFNULL(a.haber, 0)) AS debe, 0 AS haber,
'D' AS nat_id,
c.cgcu_cuenta AS cuenta, c.cgcu_nombre AS nombre
FROM ctrl2019.cat_Genero g
INNER JOIN ctrl2019.cat_CgGrupo p USING(ger_id)
INNER JOIN ctrl2019.cat_Rubro r USING(ger_id, cgp_id)
INNER JOIN ctrl2019.cat_CgCuenta c USING(ger_id, cgp_id, rub_id)
INNER JOIN ctrl2019.cat_CgSubcuenta s USING(ger_id, cgp_id, rub_id, cgcu_id)
LEFT JOIN (
SELECT a.ger_id, a.grp_id, a.rub_id, a.cta_id, a.sct_id,
SUM(IFNULL(a.msl_debe, 0)) AS debe, SUM(IFNULL(a.msl_haber, 0)) AS haber
FROM ldf.vin_EntePublicoLDF e
INNER JOIN ldf.blz_Mensual_2019 a USING(gpo_id, ur_id)
WHERE e.ejr_id = 2019
AND a.ger_id = 1
AND e.ent_id = 12
AND a.msc_id IN (0, 1, 2, 3)
GROUP BY a.ger_id, a.grp_id, a.rub_id, a.cta_id, a.sct_id
)a ON s.ger_id = a.ger_id AND s.cgp_id = a.grp_id AND s.rub_id = a.rub_id AND s.cgcu_id = a.cta_id AND s.cgsc_id = a.sct_id
WHERE g.ger_id = 1
GROUP BY g.ger_id, p.cgp_id, r.rub_id, c.cgcu_id;
I have no idea why it gives me this error, i'm new in sql.
you just need to add all the columns in GROUP BY which are present in the SELECT statement.
Seems like simple aggregation here. I cleaned up the code and added some columns to the GROUP BY.
SELECT
id = c.cgcu_cuenta
,g.ger_cuenta
,g.ger_nombre
,p.cgp_cuenta
,p.cgp_nombre
,r.rub_cuenta
,r.rub_nombre
,c.cgcu_cuenta
,c.cgcu_nombre
,s.cgsc_cuenta
,s.cgsc_nombre
,debe = SUM(IFNULL(a.debe, 0)) - SUM(IFNULL(a.haber, 0))
,haber = 0
,nat_id = 'D'
,cuenta = c.cgcu_cuenta
,nombre = c.cgcu_nombre
FROM ctrl2019.cat_Genero g
INNER JOIN ctrl2019.cat_CgGrupo p ON USING(ger_id)
INNER JOIN ctrl2019.cat_Rubro r ON USING(ger_id, cgp_id)
INNER JOIN ctrl2019.cat_CgCuenta c USING(ger_id, cgp_id, rub_id)
INNER JOIN ctrl2019.cat_CgSubcuenta s USING(ger_id, cgp_id, rub_id, cgcu_id)
LEFT JOIN (
SELECT
a2.ger_id
,a2.grp_id
,a2.rub_id
,a2.cta_id
,a2.sct_id
,debe = SUM(IFNULL(a2.msl_debe, 0))
,haber = SUM(IFNULL(a2.msl_haber, 0))
FROM ldf.vin_EntePublicoLDF E
INNER JOIN ldf.blz_Mensual_2019 a2 USING(gpo_id, ur_id)
WHERE e.ejr_id = 2019
AND a2.ger_id = 1
AND e.ent_id = 12
AND a2.msc_id IN (0, 1, 2, 3)
GROUP BY
a2.ger_id
,a2.grp_id
,a2.rub_id
,a2.cta_id
,a2.sct_id
) A
ON s.ger_id = A.ger_id
AND s.cgp_id = A.grp_id
AND s.rub_id = A.rub_id
AND s.cgcu_id = A.cta_id
AND s.cgsc_id = A.sct_id
WHERE g.ger_id = 1
GROUP BY
g.ger_id
,p.cgp_id
,r.rub_id
,C.cgcu_id
,c.cgcu_cuenta
,g.ger_cuenta
,g.ger_nombre
,p.cgp_cuenta
,p.cgp_nombre
,r.rub_cuenta
,r.rub_nombre
,c.cgcu_nombre
,s.cgsc_cuenta
,s.cgsc_nombre

Oracle: an elegant way to extract record which share a column value

I've found two way to take every record, which shares one column value, among some distinct record set identified each one by a set of WHERE conditions.
(The example query are very more clear...)
Do you know a third way more sinthetic? maybe using analytical function?
select a.grup_gruppo_id
FROM conf_gruppi_delim a, conf_gruppi_delim b, conf_gruppi_delim c
WHERE a.ASTG_ASSE_TIPO_GRUPPO_ID = 'BASE_TSC'
AND a.TGAS_TIPGRUPDETT_ASSI_ID = 'C5JqJeruozekiQtN'
AND a.DELI_VALORE1 = '1RWOoegWqEdL9Vch'
AND a.DELI_FLAG_ANN = 'N'
--
AND b.ASTG_ASSE_TIPO_GRUPPO_ID = 'TIPI_MERCATO'
AND b.TGAS_TIPGRUPDETT_ASSI_ID = '_tgas_time_f'
AND b.DELI_VALORE1 = 'ZFLIB'
AND b.DELI_FLAG_ANN = 'N'
--
AND c.ASTG_ASSE_TIPO_GRUPPO_ID = 'SEQUENZA'
AND c.TGAS_TIPGRUPDETT_ASSI_ID = '_tgas_sefm_f'
AND c.DELI_VALORE1 = 'LE8IZjuiOHVtxAwi'
AND c.DELI_FLAG_ANN = 'N'
--
AND A.GRUP_GRUPPO_ID = b.GRUP_GRUPPO_ID
AND b.GRUP_GRUPPO_ID = c.GRUP_GRUPPO_ID
SELECT GRUP_GRUPPO_ID
FROM conf_gruppi_delim a
WHERE ASTG_ASSE_TIPO_GRUPPO_ID = 'BASE_TSC'
AND TGAS_TIPGRUPDETT_ASSI_ID = 'C5JqJeruozekiQtN'
AND DELI_VALORE1 = '1RWOoegWqEdL9Vch'
AND DELI_FLAG_ANN = 'N'
INTERSECT
SELECT GRUP_GRUPPO_ID
FROM conf_gruppi_delim a
WHERE ASTG_ASSE_TIPO_GRUPPO_ID = 'TIPI_MERCATO'
AND TGAS_TIPGRUPDETT_ASSI_ID = '_tgas_time_f'
AND DELI_VALORE1 = 'ZFLIB'
AND DELI_FLAG_ANN = 'N'
INTERSECT
SELECT GRUP_GRUPPO_ID
FROM conf_gruppi_delim a
WHERE ASTG_ASSE_TIPO_GRUPPO_ID = 'SEQUENZA'
AND TGAS_TIPGRUPDETT_ASSI_ID = '_tgas_sefm_f'
AND DELI_VALORE1 = 'LE8IZjuiOHVtxAwi'
AND DELI_FLAG_ANN = 'N'
In this case where you're just filtering on 3 different sets of values
SELECT GRUP_GRUPPO_ID
FROM conf_gruppi_delim a
WHERE (ASTG_ASSE_TIPO_GRUPPO_ID,
TGAS_TIPGRUPDETT_ASSI_ID,
DELI_VALORE1,
DELI_FLAG_ANN ) IN
( ('SEQUENZA','_tgas_sefm_f','LE8IZjuiOHVtxAwi','N'),
('TIPI_MERCATO','_tgas_time_f','ZFLIB','N'),
('BASE_TSC','C5JqJeruozekiQtN','1RWOoegWqEdL9Vch','N') )
GROUP BY GRUP_GRUPPO_ID
HAVING COUNT( distinct ASTG_ASSE_TIPO_GRUPPO_ID ) = 3

oracle sql multiple sum function

I have a query with multiple SUMs that is giving me higher values than I expect. If I cut the query down to just one SUM everything works fine, but as soon as I add the other SUMs I get incorrect values. I've tried looking online and tried different approaches including changing the JOINs and nested SELECTs, but still no luck. All help will be appreciated.
SELECT
A.WR_NO WORK_ORDER, A.WR_TYPE_CODE WR_TYPE, B.NAME PLANNER, A.SCHEDULING_POLYGON_CODE AREA, A.FINAL_CLOSE_DATE COMPLETION_DATE, C.ACTUAL_ITEM_AMT EST_ENG,
SUM(D.AFTER_COND_FCT_AMT) EST_CREW, SUM(E.AFTER_COND_FCT_AMT) ASB_CREW, SUM(F.AFTER_COND_FCT_AMT) EST_CONTR, SUM(G.AFTER_COND_FCT_AMT) ASB_CONTR
FROM
WORK_REQUEST A, CC.ALL_PEOPLE B, WR_CONTRIBUTION_VOUCHER C, WR_COST_ESTIMATE D, WR_COST_ESTIMATE E, WR_COST_ESTIMATE F, WR_COST_ESTIMATE G
WHERE
(A.FINAL_CLOSE_DATE BETWEEN TO_DATE ('10/01/2012','MM/DD/YYYY') AND TO_DATE ('10/04/2012','MM/DD/YYYY'))
AND A.ENTRY_PERSON_NO = B.PERSON_NO(+)
AND A.WR_NO = C.WR_NO(+)
AND A.WR_NO = D.WR_NO(+)
AND A.WR_NO = E.WR_NO(+)
AND A.WR_NO = F.WR_NO(+)
AND A.WR_NO = G.WR_NO(+)
AND (C.VOUCHER_CNTRB_TYPE_CODE(+) = 'ENGT')
AND (D.COST_TYPE_CODE(+) = 'LAB' AND D.DESIGN_ASBUILT_DA(+) = 'D' AND D.PROVIDER(+) = 'CO')
AND (E.COST_TYPE_CODE(+) = 'LAB' AND E.DESIGN_ASBUILT_DA(+) = 'A' AND E.PROVIDER(+) = 'CO')
AND (F.COST_TYPE_CODE(+) = 'LAB' AND F.DESIGN_ASBUILT_DA(+) = 'F' AND F.PROVIDER(+) = 'CN')
AND (G.COST_TYPE_CODE(+) = 'LAB' AND G.DESIGN_ASBUILT_DA(+) = 'G' AND G.PROVIDER(+) = 'CN')
GROUP BY
A.WR_NO, A.WR_TYPE_CODE, B.NAME, A.SCHEDULING_POLYGON_CODE, A.FINAL_CLOSE_DATE, C.ACTUAL_ITEM_AMT
ORDER BY 1
You are getting higher values than you expect because the joins are multiplyinjg rows. Without understanding the data structures, it is hard for an outside person to tell you exactly where the problem lies.
The solution to the problem is to do the sum only on the table needed for calculating it. You can then join in additional tables/summaries after that point.
By the way, you really should be using proper JOIN syntax. Use of the comma to imply a join is quite outdated and makes it harder to understand the query.
SELECT
WO_ID WORK_ORDER,
WR_TYPE_CODE WR_TYPE,
NAME PLANNER,
SCHEDULING_POLYGON_CODE AREA,
FINAL_CLOSE_DATE COMPLETE_DATE,
SUM (EENG) AS "ENG_TIME",
SUM (ECREW) AS "EST_CREW",
SUM (ACREW) AS "ASB_CREW",
SUM (ECONT) AS "EST_CONTR",
SUM (ACONT) AS "ASB_CONTR"
FROM
(SELECT
A.WO_ID,
A.WR_TYPE_CODE,
B.NAME,
A. SCHEDULING_POLYGON_CODE,
A.FINAL_CLOSE_DATE,
C.ACTUAL_ITEM_AMT AS "EENG",
0 "ECREW",
0 "ACREW",
0 "ECONT",
0 "ACONT"
FROM
WORK_REQUEST A,
CC.ALL_PEOPLE B,
WR_CONTRIBUTION_VOUCHER C
WHERE
(A.FINAL_CLOSE_DATE BETWEEN TO_DATE ('10/01/2012','MM/DD/YYYY') AND TO_DATE ('10/31/2012','MM/DD/YYYY'))
AND A.ENTRY_PERSON_NO = B.PERSON_NO
AND A.WR_NO = C.WR_NO
AND (C.VOUCHER_CNTRB_TYPE_CODE = 'ENGT')
UNION
SELECT
A.WO_ID,
A.WR_TYPE_CODE,
B.NAME,
A. SCHEDULING_POLYGON_CODE,
A.FINAL_CLOSE_DATE,
0 "EENG",
D.AFTER_COND_FCT_AMT AS "ECREW",
0 "ACREW",
0 "ECONT",
0 "ACONT"
FROM
WORK_REQUEST A,
CC.ALL_PEOPLE B,
WR_COST_ESTIMATE D
WHERE
(A.FINAL_CLOSE_DATE BETWEEN TO_DATE ('10/01/2012','MM/DD/YYYY') AND TO_DATE ('10/31/2012','MM/DD/YYYY'))
AND A.ENTRY_PERSON_NO = B.PERSON_NO
AND A.WR_NO = D.WR_NO
AND (D.COST_TYPE_CODE = 'LAB' AND D.DESIGN_ASBUILT_DA = 'D' AND D.PROVIDER = 'CO')
UNION
SELECT
A.WO_ID,
A.WR_TYPE_CODE,
B.NAME,
A. SCHEDULING_POLYGON_CODE,
A.FINAL_CLOSE_DATE,
0 "EENG",
0 "ECREW",
E. AFTER_COND_FCT_AMT AS "ACREW",
0 "ECONT",
0 "ACONT"
FROM
WORK_REQUEST A,
CC.ALL_PEOPLE B,
WR_COST_ESTIMATE E
WHERE
(A.FINAL_CLOSE_DATE BETWEEN TO_DATE ('10/01/2012','MM/DD/YYYY') AND TO_DATE ('10/31/2012','MM/DD/YYYY'))
AND A.ENTRY_PERSON_NO = B.PERSON_NO
AND A.WR_NO = E.WR_NO
AND (E.COST_TYPE_CODE = 'LAB' AND E.DESIGN_ASBUILT_DA = 'A' AND E.PROVIDER = 'CO')
UNION
SELECT
A.WO_ID,
A.WR_TYPE_CODE,
B.NAME,
A. SCHEDULING_POLYGON_CODE,
A.FINAL_CLOSE_DATE,
0 "EENG",
0 "ECREW",
0 "ACREW",
F.AFTER_COND_FCT_AMT AS "ECONT",
0 "ACONT"
FROM
WORK_REQUEST A,
CC.ALL_PEOPLE B,
WR_COST_ESTIMATE F
WHERE
(A.FINAL_CLOSE_DATE BETWEEN TO_DATE ('10/01/2012','MM/DD/YYYY') AND TO_DATE ('10/31/2012','MM/DD/YYYY'))
AND A.ENTRY_PERSON_NO = B.PERSON_NO
AND A.WR_NO = F.WR_NO
AND (F.COST_TYPE_CODE = 'LAB' AND F.DESIGN_ASBUILT_DA = 'D' AND F.PROVIDER = 'CN')
UNION
SELECT
A.WO_ID,
A.WR_TYPE_CODE,
B.NAME,
A. SCHEDULING_POLYGON_CODE,
A.FINAL_CLOSE_DATE,
0 "EENG",
0 "ECREW",
0 "ACREW",
0 "ECONT",
G.AFTER_COND_FCT_AMT AS "ACONT"
FROM
WORK_REQUEST A,
CC.ALL_PEOPLE B,
WR_COST_ESTIMATE G
WHERE
(A.FINAL_CLOSE_DATE BETWEEN TO_DATE ('10/01/2012','MM/DD/YYYY') AND TO_DATE ('10/31/2012','MM/DD/YYYY'))
AND A.ENTRY_PERSON_NO = B.PERSON_NO
AND A.WR_NO = G.WR_NO
AND (G.COST_TYPE_CODE = 'LAB' AND G.DESIGN_ASBUILT_DA = 'A' AND G.PROVIDER = 'CN'))
GROUP BY
WO_ID, WR_TYPE_CODE, NAME, SCHEDULING_POLYGON_CODE, FINAL_CLOSE_DATE
ORDER BY
1 ASC

problem with sql syntax

The sql syntax is following:
SELECT y.NAME, y.EMAIL, k.DATE, k.WORK_NR, k.SCORE, k.FILENAME
FROM bas1.students y, bas1.assignments k, bas1.topics a
WHERE y.ID = k.student_id AND k.topic_id = a.id
WHERE a.NAME = 'History';
But I get some errors, any Ideas?
You're using WHERE twice
SELECT y.NAME, y.EMAIL, k.DATE, k.WORK_NR, k.SCORE, k.FILENAME
FROM bas1.students y, bas1.assignments k, bas1.topics a
WHERE y.ID = k.student_id AND k.topic_id = a.id
AND a.NAME = 'History';
change to this
Don't you need join statements? E.g.,
SELECT y.NAME, y.EMAIL, k.DATE, k.WORK_NR, k.SCORE, k.FILENAME
FROM bas1.students y JOIN bas1.assignments k ON y.ID = k.student_id
JOIN bas1.topics a ON k.topic_id = a.id
WHERE a.NAME = 'History';