How to check Not null value from Array in SQL - sql

I'm facing the problem while checking null value from array, I tried by adding the AND condition in where clause "AND "txt" IS not null" but it's not working.
Below is my query please help me out from this.
SELECT %s
"module-id" AS module_id,
"per-num" AS per_num,
RTRIM ("txt"[1] || "txt"[2] || "txt"[3] || "txt"[4] || "txt"[5] ||
"txt"[6] || "txt"[7] || "txt"[8] || "txt"[9] || "txt"[10])
AS note_text,
"page-no" AS page_number,
"upd-dat" AS updated_on,
"upd-tim" AS updated_time,
"upd-usr-id" AS updated_by,
CASE WHEN "module-id" = 1 THEN 'Common'
WHEN "module-id" = 5 THEN 'Rents'
WHEN "module-id" = 8 THEN 'Arrears'
END AS category
FROM pub."perpad"
WHERE "module-id" IN (1, 5, 8)
AND ("per-num" > :perNumFrom
OR ("per-num" = :perNumFrom AND "module-id" > :moduleIdFrom)
OR ("per-num" = :perNumFrom AND "module-id" = :moduleIdFrom AND "page-no" >= :pageNumFrom))
AND "upd-dat" >= :fromDate %s
AND "txt" IS not null
Thanks.

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

update multiple columns with case/if statement?

Thank you in advance:
I have a table1:
id || batches || IC_chips
DRG001 || JHL001 || layer1
DRG001 || JHL001 || layer2
DRG001 || JHL001 || layer3
DRG001 || JHL001 || layer4
DRG001 || JHL001 || layer5
DRG001 || JHL001 || layer6
DRG001 || JHL002 || layer7
DRG001 || JHL002 || layer8
DRG001 || JHL002 || layer9
DRG001 || JHL002 || layer10
POQ001 || ADG001 || layer1
POQ001 || ADG001 || layer2
POQ001 || ADG001 || layer3
POQ001 || ADG001 || layer4
POQ001 || ADG001 || layer5
POQ001 || ADG001 || layer6
the output table is :
ID || print_batch_1 || Print_batch_2 || Count_print_batch_1 || Count_print_batch_2 || Batch_count
DRG001 || JHL001 || JHL002 || 06 || 04 || 02
POQ001 || ADG001 || Null || 06 || Null || 01
i tried it with an update statement but i am facing an issue when their are more than one Print batches.
this is the code i tried with :
update tab
set Count_name=b.Count_name
,batch_count=b.batch_count
from
table1 tab
inner join
(
select Name, count(batches) as Count_name, count(distinct batches) as
batch_count
from table1
group by batches) b
on tab.batches=b.batches
Please try this -
update tab B
set Count_name = a.batches, batch_count = batch_cnt
from (
select batches, count(*) batch_cnt
from table1 A
where a.batches = b.batches
)
where
exists (
select 1
from table1 A
where a.batches = b.batches
)

Using Multiple ANDs and ORs in ANSI SQL

I have a simple SQL query:
SELECT
w.fizz
FROM
widgets w
WHERE
w.special_id = 2394
AND w.buzz IS NOT NULL
AND w.foo = 12
In pseudo-code, this WHERE clause could be thought of as:
if(specialId == 2394 && buzz != null && foo == 12)
I now want to change this query so that it returns all widgets whose special_id is 2394, and whose buzz is not null, and whose foo is 12, OR whose special_id is 2394, and whose blah is 'YES', and whose num is 4. In pseudo-code:
if(specialId == 2394 && (buzz != null && foo == 12) || (blah == "YES" && num == 4))
I tried the following, only to get errors:
SELECT
w.fizz
FROM
widgets w
WHERE
w.special_id = 2394
AND
(
w.buzz IS NOT NULL
AND w.foo = 12
)
OR
(
w.blah = 'YES'
AND w.num = 4
)
Any ideas? Thanks in advance!
SELECT
w.fizz
FROM
widgets w
WHERE
w.special_id = 2394
AND
(
(
w.buzz != null
AND w.foo = 12
)
OR
(
w.blah = 'YES'
AND w.num = 4
)
)
Add additional brackets surrounding "OR", because "OR" has less priority than "AND".

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);

Invalid Identifier in Oracle Function

This is my SQL;
select a.hesap_no,
a.teklif_no1 || '/' || a.teklif_no2 as teklif,
a.mus_k_isim as musteri,
b.marka,
c.sasi_no,
c.sasi_durum,
d.tas_mar,
nvl(risk_sasi(a.teklif_no1, a.teklif_no2, c.urun_sira_no, c.sira_no), 0) as risk,
nvl(mv_sasi(a.teklif_no1, a.teklif_no2, c.sira_no, c.urun_sira_no, sysdate), 0) as mv
from s_teklif a,
s_urun b,
s_urun_detay c,
koc_ktmar_pr d
where a.teklif_no1 || a.teklif_no2 = b.teklif_no1 || b.teklif_no2
This is my MV_SASI Function;
create or replace
FUNCTION MV_SASI
(
TEK1 IN VARCHAR2,
TEK2 IN NUMBER,
SIRA IN NUMBER,
USIRA IN NUMBER,
DT IN DATE
)
RETURN NUMBER IS MV number;
fat number;
adet number;
pd number;
pds number;
kt date;
ktv number;
dtv number;
frk number;
yfrk number;
TKM VARCHAR2(10);
BEGIN
SELECT COUNT(*)
INTO adet
FROM S_URUN_DETAY
WHERE (SASI_DURUM IS NULL OR SASI_DURUM IN ('A','R'))
AND TEKLIF_NO1 = TEK1
AND TEKLIF_NO2 = TEK2;
SELECT SUM(CASE WHEN B.SASI_DURUM IS NULL OR B.SASI_DURUM IN ('A','R') THEN A.KDV_FIYAT ELSE 0 END)
INTO fat
FROM S_URUN A,S_URUN_DETAY B
WHERE A.TEKLIF_NO1 = tek1
AND A.TEKLIF_NO2 = tek2
AND A.TEKLIF_NO1 = B.TEKLIF_NO1
AND A.TEKLIF_NO2 = B.TEKLIF_NO2
AND A.SIRA_NO = B.URUN_SIRA_NO;
SELECT KULLAN_TARIH
INTO kt
FROM S_TEKLIF
WHERE TEKLIF_NO1 = tek1
AND TEKLIF_NO2 = tek2 ;
yfrk:= EXTRACT(YEAR FROM dt) - EXTRACT(YEAR FROM kt);
ktv := EXTRACT(MONTH FROM kt);
dtv := EXTRACT(MONTH FROM dt);
frk := yfrk * 12 + (dtv-ktv);
IF frk <= 0 THEN
pd := fat * 0.85;
ELSE
pd := fat*0.85 - (fat * 0.0101 * frk);
END IF;
SELECT NVL(ROUND((CASE WHEN SASI_DURUM IS NULL OR SASI_DURUM IN ('A','R') THEN pd / adet ELSE 0 END),2),0)
INTO pds
FROM S_URUN_DETAY
WHERE TEKLIF_NO1 = TEK1
AND TEKLIF_NO2 = TEK2
AND URUN_SIRA_NO = USIRA
AND SIRA_NO = SIRA;
RETURN pds;
END;
But in my page i getting an error with this line of code;
OracleDataReader dr = myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
Where am i doing wrong?
Best Regards, Soner
myCommand is;
select a.hesap_no,
a.teklif_no1 || '/' || a.teklif_no2 as teklif,
a.mus_k_isim as musteri,
b.marka,
c.sasi_no,
c.sasi_durum,
d.tas_mar,
nvl(risk_sasi(a.teklif_no1, a.teklif_no2, c.urun_sira_no, c.sira_no), 0) as risk,
nvl(mv_sasi(a.teklif_no1, a.teklif_no2, c.sira_no, c.urun_sira_no, sysdate), 0) as mv
from s_teklif a,
s_urun b,
s_urun_detay c,
koc_ktmar_pr d
where a.teklif_no1 || a.teklif_no2 = b.teklif_no1 || b.teklif_no2
and a.teklif_no1 || a.teklif_no2 = c.teklif_no1 || c.teklif_no2
and b.sira_no = c.urun_sira_no
and b.distributor = d.dist_kod
and b.marka = d.marka_kod
and b.urun_kod = d.tas_kod
and a.hesap_no in (select a.hesap_no
from s_teklif a
where a.mus_k_isim in (system.collections.arraylist)
)
This bit at the end of your SQL seems odd: WHERE A.MUS_K_ISIM IN (System.Collections.ArrayList). If that's really your actual SQL text, then I think this is your problem. Oracle has no idea what System.Collections.ArrayList is.
Moreover, if what you're trying to do is bind an actual arraylist into the SQL so that you can test against it with IN -- that won't work. Even if you successfully got it to bind to a known datatype in Oracle, the syntax of IN is such that the operand is compared to each element in a comma-delimited list of identifiers -- Oracle won't break the list apart for you.
You either need to bind each item of the list so you have a condition like A.MUS_K_ISIM IN (:bind1, :bind2, :bind3), or map your arraylist to a nested table or varray type in Oracle so you can use a subquery with IN, like A.MUS_K_ISIM IN (SELECT columnValue FROM TABLE(CAST(:bindlist AS someOracleType))).
I have come accross that kind of problem a short time ago. The problem was:
Stupid oracle has not a stacktrace type error handling mechanism so it only gives the top error, which prevents us to see the other ones.
Your function MV_SASI has no grant to be used by the oracle user. So Oracle could not find it and tried to run it like MV_SASI is a column name. so it gives the error.
Check the grant and visibility settings of the MV_SASI func.