SQL Query works different than expected - sql

I have problems with understanding sql statement.
When I execute the it(see below), I'm getting only people with sgtn_typ 'L'.
SELECT DISTINCT kun_id kid
,dep_id
,ddg_rang_g RANG
,dep_perf_g
,TO_CHAR(dep_buchwert + dep_kasse, '999G999G990D99') WERT
,kun_name1
,Kun_vorname
,kun_cust_id
,kun_strasse
,kun_plz
,kun_ort
,kun_e_mail
,kun_pseudonym
,kka_value
FROM ddg, dep, kun, kat, kka, sgtn
WHERE dep_spi_id = 245
AND dep_abr_id = 24
AND ddg_dgr_id = 3
AND ddg_abr_id = dep_abr_id
AND ddg_spi_id = dep_spi_id
AND ddg_dep_id = dep_id
AND dep_kun_id = kun_id
AND kun_aktiv = 'J'
AND dep_spi_id = kun_spi_id
AND dep_id IS NOT NULL
AND dep_id NOT IN (SELECT ddg_dep_id
FROM ddg
WHERE ddg_spi_id = 245
AND ddg_abr_id = 0
AND ddg_dgr_id = 1)
AND kun_id = kka_kun_id
AND kka_spi_id = 245
AND kat_spi_id = 245
AND kka_kat_kuerzel = kat_kuerzel
AND kat_kuerzel = 'NSCH'
AND kka_spi_id = sgtn_spi_id
AND kka_kun_id = sgtn_kun_id
AND sgtn_spi_id = 245
AND sgtn_e_mail = kun_e_mail
AND (sgtn_typ = 'L'
OR (sgtn_typ = 'T'
AND sgtn_e_mail NOT IN (SELECT sgtn_e_mail
FROM sgtn
WHERE sgtn_spi_id = 245
AND sgtn_typ = 'L'))
)
ORDER BY kun_id, sgtn_typ;
But when I execute this, I'm getting people with sgtn_typ 'T':
SELECT *
FROM sgtn
WHERE sgtn_spi_id = 245
AND (sgtn_typ = 'L'
OR (sgtn_typ = 'T'
AND sgtn_e_mail NOT IN (SELECT sgtn_e_mail
FROM sgtn
WHERE sgtn_spi_id = 245
AND sgtn_typ = 'L'))
)
ORDER BY sgtn_kun_id;
I don't understand why? Could someone give me any clue? I was expecting, that I substitute all people with type 'L' and will get people with type 'T':
AND (sgtn_typ = 'L'
OR (sgtn_typ = 'T'
AND sgtn_e_mail NOT IN (SELECT sgtn_e_mail
FROM sgtn
WHERE sgtn_spi_id = 245
AND sgtn_typ = 'L'))
)
There are 'duplicates' in sgtn table(some people are entered with 'T' and 'L'). And apart from that the whole record is the same.
Table scheme are like the folowing:
SGTN:
SGTN_ID NUMBER(22)
SGTN_SPI_ID NUMBER(22)
SGTN_KUN_ID NUMBER(22)
SGTN_TYP VARCHAR2(4)
SGTN_GESCHLECHT VARCHAR2(4)
SGTN_VORNAME VARCHAR2(160)
SGTN_NAME1 VARCHAR2(240)
SGTN_STRASSE VARCHAR2(240)
SGTN_PLZ VARCHAR2(60)
SGTN_ORT VARCHAR2(240)
SGTN_E_MAIL VARCHAR2(240)
SGTN_GEB_DAT DATE(7)
SGTN_KLASSE VARCHAR2(200)
SGTN_SCHULE VARCHAR2(200)
SGTN_TELEFON VARCHAR2(120)
SGTN_KONTONUMMER VARCHAR2(120)
KUN:
KUN_ID NUMBER(22)
KUN_SPI_ID NUMBER(22)
KUN_LAN_ID NUMBER(22)
KUN_MED_KBEZ VARCHAR2(40)
KUN_NAME1 VARCHAR2(240)
KUN_MKZ VARCHAR2(80)
KUN_TS DATE(7)
KUN_AKTIV VARCHAR2(12)
KUN_KTOINH VARCHAR2(240)
KUN_BLZ VARCHAR2(40)
KUN_BANK VARCHAR2(240)
KUN_KTONR VARCHAR2(80)
KUN_ZAHLART VARCHAR2(4)
KUN_NAME2 VARCHAR2(240)
KUN_VORNAME VARCHAR2(160)
KUN_STRASSE VARCHAR2(240)
KUN_PLZ VARCHAR2(60)
KUN_ORT VARCHAR2(240)
KUN_TEL1 VARCHAR2(120)
KUN_TEL2 VARCHAR2(120)
KUN_FAX VARCHAR2(120)
KUN_E_MAIL VARCHAR2(240)
KUN_PSEUDONYM VARCHAR2(120)
KUN_PWD VARCHAR2(60)
KUN_CUST_ID VARCHAR2(240)
KUN_GESCHLECHT VARCHAR2(4)
KUN_K_NAME VARCHAR2(80)
KUN_K_NR NUMBER(22)
KUN_K_VALID VARCHAR2(40)
KUN_ZUSATZ_1 VARCHAR2(400)
KUN_ZUSATZ_DATUM DATE(7)
KUN_COMMENT VARCHAR2(4000)
KUN_EVT_SMS VARCHAR2(80)
KUN_EVT_E_MAIL VARCHAR2(240)
KUN_COMMENT2 VARCHAR2(4000)
KUN_AKTIV_BIS DATE(7)
KUN_FLG_AGB VARCHAR2(4)
KUN_TITEL VARCHAR2(200)
Thank you in advance for clues.
Greetings
R. Bada

Related

Group by on multiple subqueries

I'm new to Oracle SQL and am still learning, I'm trying to work out what GROUP BY I need to use.
The subquery works by itself:
SELECT TO_CHAR(CREATE_DATE_TIME, 'DD-MON-YYYY') "DTTM"
, CASE_NBR
, COALESCE(PT.REF_FIELD_1, LPN.TC_ASN_ID) "REF_FIELD_1"
, COALESCE(PT.REF_FIELD_2, LPN.ASN_ID || LPN.ITEM_ID) "REF_FIELD_2"
FROM PIX_TRAN PT, LPN
WHERE ( ( PT.TRAN_TYPE = '300'
AND PT.TRAN_CODE = '01'
AND PT.ACTN_CODE = '20' )
OR ( PT.TRAN_TYPE = '300'
AND PT.TRAN_CODE = '04'
AND PT.ACTN_CODE = '21' ) )
AND SUBSTR(COALESCE(PT.REF_FIELD_1, LPN.TC_ASN_ID), 1, INSTR(COALESCE(PT.REF_FIELD_1, LPN.TC_ASN_ID), '_', 1)) != 'Return_'
AND PT.CASE_NBR = LPN.TC_LPN_ID (+)
AND PT.WHSE = 'DCV'
AND TRUNC(CREATE_DATE_TIME) = TRUNC(SYSDATE)
But when I try to add it as a subquery with a GROUP BY, I can't seem to work out what the correct GROUP BY should be?
SELECT 'PO Lines/LPNs Putaway' AS "FACILITY_ACTIVITY"
, TRUNC DTTM AS "CREATED"
, COUNT(DISTINCT REF_FIELD_1 || REF_FIELD_2)|| '/'|| COUNT(DISTINCT CASE_NBR) "Total"
FROM (
SELECT TO_CHAR(CREATE_DATE_TIME, 'DD-MON-YYYY') "DTTM"
, CASE_NBR
, COALESCE(PT.REF_FIELD_1, LPN.TC_ASN_ID) "REF_FIELD_1"
, COALESCE(PT.REF_FIELD_2, LPN.ASN_ID || LPN.ITEM_ID) "REF_FIELD_2"
FROM PIX_TRAN PT, LPN
WHERE ( ( PT.TRAN_TYPE = '300'
AND PT.TRAN_CODE = '01'
AND PT.ACTN_CODE = '20' )
OR ( PT.TRAN_TYPE = '300'
AND PT.TRAN_CODE = '04'
AND PT.ACTN_CODE = '21' ) )
AND SUBSTR(COALESCE(PT.REF_FIELD_1, LPN.TC_ASN_ID), 1, INSTR(COALESCE(PT.REF_FIELD_1, LPN.TC_ASN_ID), '_', 1)) != 'Return_'
AND PT.CASE_NBR = LPN.TC_LPN_ID (+)
AND PT.WHSE = 'DCV'
AND TRUNC(CREATE_DATE_TIME) = TRUNC(SYSDATE)
)
GROUP BY TRUNC(DTTM);
I've tried the following GROUP BY's
GROUP BY TRUNC(DTTM)
ERROR - "FROM Keyword not found where expected"
GROUP BY TRUNC(TO_CHAR(CREATE_DATE_TIME, 'DD-MON-YYYY'))
with changing the select clause to
TRUNC(TO_CHAR(CREATE_DATE_TIME, 'DD-MON-YYYY')) AS "CREATED"
ERROR - "CREATE_DATE_TIME" invalid identifier
GROUP BY TRUNC(CREATE_DATE_TIME)
with changing the select clause to
TRUNC(CREATE_DATE_TIME) AS "CREATED"
ERROR - "CREATE_DATE_TIME" invalid identifier
Can someone please point out what I'm missing?
I formatted your queries which makes it easy to see the issue
Original
SELECT
to_char(create_date_time, 'DD-MON-YYYY') "DTTM",
case_nbr,
coalesce(pt.ref_field_1, lpn.tc_asn_id) "REF_FIELD_1",
coalesce(pt.ref_field_2, lpn.asn_id || lpn.item_id) "REF_FIELD_2"
FROM
pix_tran pt,
lpn
WHERE
( ( pt.tran_type = '300'
AND pt.tran_code = '01'
AND pt.actn_code = '20' )
OR ( pt.tran_type = '300'
AND pt.tran_code = '04'
AND pt.actn_code = '21' ) )
AND substr(coalesce(pt.ref_field_1, lpn.tc_asn_id),
1,
instr(coalesce(pt.ref_field_1, lpn.tc_asn_id),
'',
1)) != 'Return'
AND pt.case_nbr = lpn.tc_lpn_id (+)
AND pt.whse = 'DCV'
AND trunc(create_date_time) = trunc(sysdate)
Inline view
select 'PO Lines/LPNs Putaway' as "FACILITY_ACTIVITY",
trunc dttm AS "CREATED" , COUNT(DISTINCT REF_FIELD_1 || REF_FIELD_2)|| '/'|| COUNT(DISTINCT CASE_NBR) "Total" FROM
(
select to_char(
create_date_time,
'DD-MON-YYYY'
) "DTTM",
case_nbr,
coalesce(
pt.ref_field_1,
lpn.tc_asn_id
) "REF_FIELD_1",
coalesce(
pt.ref_field_2,
lpn.asn_id || lpn.item_id
) "REF_FIELD_2"
from pix_tran pt,
lpn
where ( ( pt.tran_type = '300'
and pt.tran_code = '01'
and pt.actn_code = '20' )
or ( pt.tran_type = '300'
and pt.tran_code = '04'
and pt.actn_code = '21' ) )
and substr(
coalesce(
pt.ref_field_1,
lpn.tc_asn_id
),
1,
instr(
coalesce(
pt.ref_field_1,
lpn.tc_asn_id
),
'',
1
)
) != 'Return'
and pt.case_nbr = lpn.tc_lpn_id (+)
and pt.whse = 'DCV'
and trunc(create_date_time) = trunc(sysdate)
)
group by trunc(dttm);
You are missing the brackets on your TRUNC, and since your DTTM is a string, the use of trunc at all is probably not appropriate.
I would move the TRUNC inside the subquery (it will reduce the datetime to a date) and then just group by DTTM

Postgresql select in variable

I have two select
do $$
declare
uid text := 'b5974eec-7078-4ae7-923c-36af96905423';
my_output record;
Begin
-- Czas konwersji jednego ekstratku
SELECT sha256, (
SELECT timestamp
FROM public.tmask_mda02
WHERE uuid = uid
AND import = 'COMPLETED'
AND params = 'sha256 in')::timestamp - (
SELECT timestamp
FROM public.tmask_mda02
WHERE uuid = uid
AND export = 'COMPLETED'
AND params = 'sha256 out')::timestamp AS Interval
INTO my_output
FROM public.tmask_mda02
WHERE uuid = uid
AND params = 'sha256 out';
RAISE NOTICE 'Dlugosc_przetwarzania: %',my_output;
end $$;
Output is:
Dlugosc_przetwarzania:
(89b253f28435aea23fc68c042066e988d236f2f312583255c0352ea512701daf,-00:00:01.145)
SELECT DISTINCT uuid FROM public.tmask_mda02 WHERE export = 'COMPLETED' AND timestamp::text LIKE '2021-09-22%';
Output:
"02de41c7-488f-4dc9-bb33-b76b05426cde"
"0b82e409-e37f-4b34-a90a-c5b57d63d759"
"19d38112-efb0-4f67-97aa-493d0cf9a1a4"
"1e8e5745-6885-4e18-89a1-3c841efd48ac"
"223cfb85-b8eb-4ea1-8f14-fbd565f68344"
"27fcaadc-f487-4bfe-bee8-00b98375844b"
"311e0cc8-f398-4134-855f-b64631254af5"
"31f856fb-c814-4214-9612-1703f247de2b"
I need use select in variable uid:
do $$
declare
uid text := (SELECT DISTINCT uuid FROM public.tmask_mda02 WHERE export = 'COMPLETED' AND timestamp::text LIKE '2021-09-22%');
my_output record;
Begin
-- Czas konwersji jednego ekstratku
SELECT sha256, (
SELECT timestamp
FROM public.tmask_mda02
WHERE uuid = uid
AND import = 'COMPLETED'
AND params = 'sha256 in')::timestamp - (
SELECT timestamp
FROM public.tmask_mda02
WHERE uuid = uid
AND export = 'COMPLETED'
AND params = 'sha256 out')::timestamp AS Interval
INTO my_output
FROM public.tmask_mda02
WHERE uuid = uid
AND params = 'sha256 out';
RAISE NOTICE 'Dlugosc_przetwarzania: %',my_output;
end $$;

Why can't I insert data to my table?

I created a table using this
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DimRegion]') AND type in (N'U'))
DROP TABLE [DimRegion]
Go
Create Table DimRegion
(RegionViewKey int NOT NULL identity Primary Key,
RegionView varchar(10),
RegionViewCode varchar(10),
ActiveYear smallint,
SublocationString varchar(7)NULL,
SubLocationCode char(10)NOT NULL,
LocationCode char(10)NULL,
RegionCode char(10)NULL,
RegionGrpCode char(10)NULL,
DivisionCode char(10)NOT NULL,
DivisionGrpCode char(10)NULL,
SubLocationDescription char(50)NULL,
LocationDescription char(50)NULL,
RegionDescription char(50)NULL,
RegionGrpDescription char(50)NULL,
DivisionDescription char(50)NULL,
DivisionGrpDescription char(50)NULL)
And I inserted data using this script
insert into DWResourceTask.dbo.DimRegion --(2013)
Select
'Region1' as RegionView,
'R1' as RegionViewCode,
'2013' as ActiveYear,
sl.sublocationstring,
sl.subLocationCode,
l.locationcode,
r.regioncode,
rg.RegionGrpCode,
d.DivisionCode,
dg.DivisionGrpCode,
sl.SubLocationDescription,
l.LocationDescription,
r.regiondescription,
rg.RegionGrpDescription,
d.divisionDescription,
dg.DivisionGrpDescription
from SCSubLocation sl,
SCLocation l,
SCRegion r,
SCRegionGrp rg,
SCDivision d,
SCDivisionGrp dg
where l.LocationCode = sl.LocationCode
and r.RegionCode = l.RegionCode
and r.RegionGrpCode = rg.RegionGrpCode
and d.divisioncode = rg.divisioncode
and d.divisiongrpcode = dg.divisiongrpcode
But when I created this script below where it would only insert new and latest data, it gives me an error such as
Msg 102, Level 15, State 1, Line 33
Incorrect syntax near 'R1'.
Script:
insert into DWResourceTask.dbo.DimRegion --(2013)
Select
'Region1' as RegionView,
'R1' as RegionViewCode,
'2013' as ActiveYear,
sl.sublocationstring,
sl.subLocationCode,
l.locationcode,
r.regioncode,
rg.RegionGrpCode,
d.DivisionCode,
dg.DivisionGrpCode,
sl.SubLocationDescription,
l.LocationDescription,
r.regiondescription,
rg.RegionGrpDescription,
d.divisionDescription,
dg.DivisionGrpDescription
from SCSubLocation sl,
SCLocation l,
SCRegion r,
SCRegionGrp rg,
SCDivision d,
SCDivisionGrp dg
where l.LocationCode = sl.LocationCode
and r.RegionCode = l.RegionCode
and r.RegionGrpCode = rg.RegionGrpCode
and d.divisioncode = rg.divisioncode
and d.divisiongrpcode = dg.divisiongrpcode
and not exists(select * from DWResourceTask.dbo.DimRegion x
where(Region1=x.RegionView
R1=x.RegionViewCode
2013=x.ActiveYear
sl.sublocationstring=x.sublocationstring
sl.subLocationCode=x.subLocationCode
l.locationcode=x.locationcode
r.regioncode=x.regioncode
rg.RegionGrpCode=x.RegionGrpCode
d.DivisionCode=x.DivisionCode
dg.DivisionGrpCode=x.DivisionGrpCode
sl.SubLocationDescription=x.SubLocationDescription
l.LocationDescription=x.LocationDescription
r.regiondescription=x.regiondescription
rg.RegionGrpDescription=x.RegionGrpDescription
d.divisionDescription=x.divisionDescription
dg.DivisionGrpDescription=x.DivisionGrpDescription)
)
How do I fix this script on top where it only inserts latest data
It looks like your WHERE clause is missing the ANDs between parts of the condition:
...
where(Region1=x.RegionView
AND R1=x.RegionViewCode
AND 2013=x.ActiveYear
AND sl.sublocationstring=x.sublocationstring
AND sl.subLocationCode=x.subLocationCode
AND l.locationcode=x.locationcode
AND r.regioncode=x.regioncode
AND rg.RegionGrpCode=x.RegionGrpCode
AND d.DivisionCode=x.DivisionCode
AND dg.DivisionGrpCode=x.DivisionGrpCode
AND sl.SubLocationDescription=x.SubLocationDescription
AND l.LocationDescription=x.LocationDescription
AND r.regiondescription=x.regiondescription
AND rg.RegionGrpDescription=x.RegionGrpDescription
AND d.divisionDescription=x.divisionDescription
AND dg.DivisionGrpDescription=x.DivisionGrpDescription)
)

execute sql query in zend framework 2

I need execute this SQL query in ZF2:
SELECT FA_VENTAS.EM_CODIGO,
FA_VENDED.VN_NOMBRE AS VENDEDOR ,
FA_CLIEN.CE_NOMBRE AS CLIENTE,
FA_TIPCLI.TC_DESCRI AS CANAL ,
MIN(FA_DETVEN.CL_NOMDEP) AS MARCA ,
IN_ITEM.IT_CODBAR AS CODBARRA ,
IN_ITEM.IT_NOMBRE AS PRODUCTO ,
TO_CHAR(FA_VENTAS.VE_FECHA,'MM')AS MESES ,
TO_CHAR(FA_VENTAS.VE_FECHA,'YYYY')AS ANIO ,
SUM(DV_SUBTOTAL-DV_VALIVA) AS VALOR ,
SUM(DV_CANPED) AS UNIDAD
FROM
DOUS.FA_VENTAS FA_VENTAS,
DOUS.FA_DETVEN FA_DETVEN,
DOUS.IN_ITEM IN_ITEM ,
DOUS.IN_CLASE IN_CLASE ,
DOUS.FA_VENDED FA_VENDED,
DOUS.FA_CLIEN FA_CLIEN,
DOUS.FA_TIPCLI FA_TIPCLI
WHERE
FA_TIPCLI.TC_CODIGO=FA_CLIEN.TC_CODIGO
AND FA_VENTAS.EM_CODIGO = FA_DETVEN.EM_CODIGO
AND FA_VENTAS.VE_CODIGO = FA_DETVEN.VE_CODIGO
AND FA_VENTAS.TP_CODIGO = FA_DETVEN.TP_CODIGO
AND FA_VENTAS.PE_CODIGO = FA_DETVEN.PE_CODIGO
AND FA_DETVEN.EM_CODIGO = IN_ITEM.EM_CODIGO
AND FA_DETVEN.IT_CODIGO = IN_ITEM.IT_CODIGO
AND IN_ITEM.EM_CODIGO = IN_CLASE.EM_CODIGO
AND IN_ITEM.CL_CODIGO = IN_CLASE.CL_CODIGO
AND FA_VENDED.VN_CODIGO=4
AND FA_VENTAS.CE_CODIGO=FA_CLIEN.CE_CODIGO
AND FA_VENTAS.EM_CODIGO = '4'
AND FA_VENTAS.TP_CODIGO = '2'
AND TRUNC(FA_VENTAS.VE_FECHA) between'01-jan-2009' and '31-jan-2009'
GROUP BY FA_VENTAS.EM_CODIGO,
FA_VENDED.VN_NOMBRE,
FA_CLIEN.CE_NOMBRE,
FA_DETVEN.CL_NOMDEP,
FA_VENTAS.VE_FECHA,
IN_ITEM.IT_CODBAR,
IN_ITEM.IT_NOMBRE,
FA_TIPCLI.TC_DESCRI
UNION ALL
SELECT FA_DEVOLU.EM_CODIGO,
FA_VENDED.VN_NOMBRE AS VENDEDOR ,
FA_CLIEN.CE_NOMBRE AS CLIENTE,
FA_TIPCLI.TC_DESCRI AS CANAL ,
MIN(FA_DETDEVOL.CL_NOMDEP)AS MARCA ,
IN_ITEM.IT_CODBAR AS CODBARRA ,
IN_ITEM.IT_NOMBRE AS PRODUCTO ,
TO_CHAR(FA_DEVOLU.DE_FECHA,'MM') AS MESES ,
TO_CHAR(FA_DEVOLU.DE_FECHA,'YYYY') AS ANIO ,
-SUM(FA_DETDEVOL.DD_SUBTOTAL- FA_DETDEVOL.DD_VALIVA) AS VALOR ,
-SUM(DD_CANTID) AS UNIDAD
FROM
DOUS.FA_DEVOLU,
DOUS.FA_DETDEVOL,
DOUS.IN_ITEM IN_ITEM ,
DOUS.IN_CLASE IN_CLASE ,
DOUS.FA_VENDED FA_VENDED,
DOUS.FA_CLIEN FA_CLIEN,
DOUS.FA_TIPCLI FA_TIPCLI
WHERE
FA_TIPCLI.TC_CODIGO=FA_CLIEN.TC_CODIGO
AND FA_DEVOLU.EM_CODIGO = FA_DETDEVOL.EM_CODIGO
AND FA_DEVOLU.DE_CODIGO = FA_DETDEVOL.DE_CODIGO
AND FA_DEVOLU.TP_CODIGO = FA_DETDEVOL.TP_CODIGO
AND FA_DETDEVOL.EM_CODIGO = IN_ITEM.EM_CODIGO
AND FA_DETDEVOL.IT_CODIGO = IN_ITEM.IT_CODIGO
AND IN_ITEM.EM_CODIGO = IN_CLASE.EM_CODIGO
AND IN_ITEM.CL_CODIGO = IN_CLASE.CL_CODIGO
AND FA_DETDEVOL.EM_CODIGO = 4
AND FA_DETDEVOL.TP_CODIGO = '2'
AND TRUNC(FA_DEVOLU.DE_FECHA) between '01-jan-2009' and '31-jan-2009'
AND FA_VENDED.VN_CODIGO=4
AND FA_DEVOLU.CE_CODIGO=FA_CLIEN.CE_CODIGO
GROUP BY FA_DEVOLU.EM_CODIGO,
FA_VENDED.VN_NOMBRE,
FA_CLIEN.CE_NOMBRE,
FA_DETDEVOL.CL_NOMDEP,
FA_DEVOLU.DE_FECHA,
IN_ITEM.IT_CODBAR,
IN_ITEM.IT_NOMBRE,
FA_TIPCLI.TC_DESCRI
but I don't understand what model, view and command use.
Please help me
With a query like this, you should use \Zend\Db\Adapter\Adapter
$sql = "SELECT SELECT FA_VENTAS.EM_CODIGO, ...";
$statement = $adapter->query($sql);
$result = $statement->execute();
hth

Oracle - SubQuery returning Multiple rows

I have the following table structure:
HSM
HSM_EXC_CODE Y VARCHAR2(60)
HSM_INSTR_CODE Y VARCHAR2(60)
HSM_ISIN Y VARCHAR2(60)
HSM_VWD_TICKERSYMBL Y VARCHAR2(80)
TENFORE_EXCHANGE_MAP
HS_MARKET Y VARCHAR2(40)
TF_EXCHANGE Y VARCHAR2(40)
TFV
TFE_ID Y NUMBER(22)
TFE_VSE_CODE Y VARCHAR2(1000)
Different TFE_ID can have same TFE_VSE_CODE! I think this is what I'm missing in the update query below.
VSD
VSD_ON Y VARCHAR2(160)
VSD_ISIN Y VARCHAR2(15)
The tables are connected like the following:
TENFORE_EXCHANGE_MAP.HS_MARKET = HSM.HSM_EXC_CODE
TENFORE_EXCHANGE_MAP.TF_EXCHANGE = TFV.TFE_ID
I'm trying to fill hsm_isin and hsm_on fields. To reach the goal I'm trying to generate the names from hsm.hsm_exc_code . tfv.tfe_vse_code. But I'm doing it wrong, cause I'm getting the error from the topic. This is what I have tried:
UPDATE hsm
SET hsm_isin =
(SELECT distinct vsd.vsd_isin
FROM vsd, tfv, TENFORE_EXCHANGE_MAP
WHERE vsd.vsd_on = hsm.hsm_instr_code || '.' || tfv.tfe_vse_code
AND hsm.hsm_exc_code = TENFORE_EXCHANGE_MAP.HS_MARKET
AND TENFORE_EXCHANGE_MAP.TF_EXCHANGE = tfv.tfe_id)
,hsm.hsm_vwd_tickersymbl =
(SELECT distinct vsd.vsd_on
FROM vsd, tfv, TENFORE_EXCHANGE_MAP
WHERE vsd.vsd_on = hsm.hsm_instr_code || '.' || tfv.tfe_vse_code
AND hsm.hsm_exc_code = TENFORE_EXCHANGE_MAP.HS_MARKET
AND TENFORE_EXCHANGE_MAP.TF_EXCHANGE = tfv.tfe_id);
There must be more than one line for key in either first or second subquery:
Try something like:
SELECT hsm.hsm_instr_code,
count( distinct( vsd.vsd_on ) ) cnt1,
count( distinct( vsd.vsd_isin ) ) cnt2
FROM vsd, tfv, TENFORE_EXCHANGE_MAP, hsm
WHERE vsd.vsd_on = hsm.hsm_instr_code || '.' || tfv.tfe_vse_code
AND hsm.hsm_exc_code = TENFORE_EXCHANGE_MAP.HS_MARKET
AND TENFORE_EXCHANGE_MAP.TF_EXCHANGE = tfv.tfe_id
GROUP BY hsm.hsm_instr_code
HAVING count( distinct( vsd.vsd_on ) ) > 1 OR count( distinct( vsd.vsd_isin ) ) > 1
NOTE: Once you fix multiline problem, you can simplify two subqueries in one, like below:
UPDATE hsm SET ( hsm_isin, hsm.hsm_vwd_tickersymbl ) =
(SELECT distinct vsd.vsd_isin, vsd.vsd_on
FROM vsd, tfv, TENFORE_EXCHANGE_MAP
WHERE vsd.vsd_on = hsm.hsm_instr_code || '.' || tfv.tfe_vse_code
AND hsm.hsm_exc_code = TENFORE_EXCHANGE_MAP.HS_MARKET
AND TENFORE_EXCHANGE_MAP.TF_EXCHANGE = tfv.tfe_id);