Oracle - Add rows together in one field - sql

Using the following code. Keep getting the following error
Select distinct
ship_L.ship_ID,
ship_L.Item_Num,
C.sku,
C.ob_oid,
c.Container
From (
Select distinct
ob_oid,
sku,
substr((ltrim(sys_Connect_By_Path(trim(to_Cont),' / '))),2,(length(ltrim(sys_Connect_By_Path(trim(to_Cont),' / '))))) as Container
From (
Select distinct
Ob_oid,
sku,
To_Cont,
row_number() Over (Partition by sku order by to_Cont) -1 as seq
From (
Select distinct
ob_oid,
sku,
To_Cont
from elite_76_w1.ITH_f
--Where ob_oid = '237472'
-- and sku = '64154'
)
)
Where connect_By_Isleaf = 1
Connect by seq = Prior seq +1 and sku = Prior sku
Start with seq = 1
) C
Left Join elite_76_D.ship_L
on ship_L.ship_id = C.ob_oid
and ship_L.item_num = C.sku
WHere C.ob_oid = '237472'
and C.sku = '64154'
Getting this error:
ORA-01489: result of string concatenation is too long 01489. 00000 - " result of string concatenation is too long" *Cause: String concatenatin result is more than the maximun size. * Action: Make sure that the result is lees than the maximum size. Vendor Code 1489Error at line 3.
Start with:
SKU Location
64154 A153945
64154 A153943
64154 A153947
64154 A153946
64154 A153944
Need:
64154 A153944 / A153945 / A153946 / A153947
Thank you for all the help,
David

ORA-01489: result of string concatenation is too long means that part of your SQL is generating a string longer than 4000 characters, which is the limit of a VARCHAR2 in SQL.
You can't truncate the string in SQL (e.g. with SUBSTR), because SQL needs to construct the string before it sends it to the SUBSTR function; and it bails because it exceeds the limit.
To solve this you'll probably need to create your own PL/SQL function, in which you have a greater length limit (32k) for VARCHAR2s.

Related

Selecting with INT value against STR field

I'm trying to select records from a table using SQL Developer based on an INT value (in one file) to a STR (in the other file) and I'm using a nested(?) query:
select *
from proddta.FQ584871
where tdrscn in (select rmrscn from proddta.f48310)
and tdan8 > 1 and tdeqhr > 0
and tddoco not in (select glsbl from proddta.f0911)
In this example the TDDOCO is an INT and the GLSBL is a STR. The match would be TDDOCO = 456123 to the GLSBL = 00456123.
Thank you for any assistance
I've tried to CAST and the STR and VARCHAR but they don't appear to work when I'm trying to convert the TDDOCO.
I've tried:
tddoco not in (select to_number(ltrim(glsbl,'0')) from proddta.f0911
and I've tried:
to_char(tddoco) not in (select glsbl from proddta.f0911)
and I get an invalid number error.

ORA-01722: invalid number when a specific number 112 is used

WITH
TEST_RESULT_CTE AS
(SELECT R.DSDW_RESULT_ID,
R.PARAM_VALUE AS TEST_ID
FROM SLIMS_POC_RESULT_DETAIL R
JOIN SLIMS_POC_PARAMETER P ON P.PARAM_ID=R.PARAM_ID
WHERE P.PARAMETER_NAME='TEST_ID' AND P.CATEGORY = 'Result' )
SELECT * FROM
(
SELECT S.SAMPLE_ID, R.DSDW_RESULT_ID, PARA.PARAMETER_NAME as PNAME, R.PARAM_VALUE as PVALUE
FROM SLIMS_POC_RESULT_DETAIL R
JOIN TEST_RESULT_CTE TR ON TR.DSDW_RESULT_ID = R.DSDW_RESULT_ID
JOIN SLIMS_POC_TEST T ON T.TEST_ID = TR.TEST_ID
JOIN SLIMS_POC_SAMPLE S ON S.SAMPLE_ID = T.SAMPLE_ID --AND S.SAMPLE_ID = to_char(113)
JOIN SLIMS_POC_PARAMETER PARA ON PARA.PARAM_ID=R.PARAM_ID AND PARA.CATEGORY='Result'
)
Result_Data
PIVOT
(
MAX(PVALUE) FOR PNAME IN ( 'TEST_ID', 'RESULT_NAME', 'UNIT', 'RESULT_TEXT', 'VALUE', 'STATUS', 'ENTERED_ON', 'ENTERED_BY', 'RESULT_TYPE' )
) PIVOTED_TAB
WHERE SAMPLE_ID > 111
ORDER BY SAMPLE_ID;
The above sql Query provides an output, without any error.
However if I replace '111' with '112' in WHERE cluase, I get the following error:
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
This error is quite strange to me, and that's why tough to fix.
The error is most likely from the data that is being returned from your new sample_id. There is a string being converted to a number that is failing. Check your data for invalid numerical data in varchar columns. Note that it could be an implicit conversion, not necessarily one where you are doing a to_number() call.

How to use variable in an sql statement when the variables have special characters

I am attempting to select columns of data from several different tables in a for loop, and each of the table names begin with numbers, titled 0_training_market through 155_training_market.
To use a table beginning with a number in an sql statement, I have to use an extra pair of quotations around that title, which just turns my variable into a string and doesn't read the actual variable. Here is my code and error:
for j in list(range(156)):
generation = str(j)+'_training_market'
energy_source = db.query(" SELECT energy_source FROM " + generation + " WHERE id < 100 ORDER BY id ")
quantity = db.query(" SELECT quantity FROM " + generation + " WHERE id < 100 ORDER BY id ")
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.SyntaxError) syntax error at or near "0"
LINE 1: SELECT energy_source FROM 0_training_market WHERE id < 100 ...
^
[SQL: SELECT energy_source FROM 0_training_market WHERE id < 100 ORDER BY id ]
I'm not sure how to work around this, so any help is appreciated. Thanks
PostgreSQL needs to have doublequotes around badly named objects:
for j in list(range(156)):
generation = str(j)+'_training_market'
energy_source = db.query(""" SELECT energy_source FROM "{}" WHERE id < 100 ORDER BY id """.format(generation))
quantity = db.query(""" SELECT quantity FROM "{}" WHERE id < 100 ORDER BY id """.format(generation))

where statement not allowed with group by function

I'm still a newbie with oracle, here I have a query that returns data like :
0.52
0.01
1
12
Desired result :
1
12
i have tried to do something like this in where part but it returns group function is not allowed here:
to_char((Max(start_time_timestamp+ (2/24))- p.port_statusmoddat), 999.999) >1
the query I'm working with:
select to_char((Max(start_time_timestamp+ (2/24))- p.port_statusmoddat), 999.999) as Diff
from ZAJBIREJ.UDR_ST r,directory_number d, CONTR_SERVICES_CAP C, MPUSNTAB SN, unicam.vw_contr_imsi_sim x, port p
where reject_reason_code = 'ISUBS'
and r.s_p_port_address = p.port_num (+)
and c.co_id = x.co_id (+)
and s_p_number_address = d.dn_num (+)
and d.dn_id =c.dn_id
AND C.SNCODE = sn.SNCODE
and C.MAIN_DIRNUM = 'X'
and c.cs_deactiv_date is null
and p.port_status = 'd'
AND nvl(C.cs_deactiv_date,'01-jan-2300') = (SELECT MAX(nvl(CA.cs_deactiv_date,'01-jan-2300'))
FROM CONTR_SERVICES_CAP CA, MPUSNTAB SNT
WHERE CA.DN_ID = D.DN_ID
AND SNT.SHDES = SN.SHDES)
group by reject_reason_code ,c.co_id, s_p_number_address,r.s_p_port_address,x.IMSI,p.port_status, p.port_statusmoddat
You need to use the HAVING clause:
group by reject_reason_code ,c.co_id, s_p_number_address, r.s_p_port_address,
x.IMSI, p.port_status, p.port_statusmoddat
having Max(start_time_timestamp+ (2/24))- p.port_statusmoddat >1
I also removed the redundant TO_CHAR as you want to test the number exceeds 1, not that a string of characters exceeds 1.

Error creating function in DB2 with params

I have a problem with a function in db2
The function finds a record, and returns a number according to whether the first and second recorded by a user
The query within the function is this
SELECT
CASE
WHEN NUM IN (1,2) THEN 5
ELSE 2.58
END AS VAL
FROM (
select ROW_NUMBER() OVER() AS NUM ,s.POLLIFE
from LQD943DTA.CAQRTRML8 c
INNER JOIN LSMODXTA.SCSRET s ON c.MCCNTR = s.POLLIFE
WHERE s.NOEMP = ( SELECT NOEMP FROM LSMODDTA.LOLLM04 WHERE POLLIFE = '0010111003')
) AS T WHERE POLLIFE = '0010111003'
And works perfect
I create the function with this code
CREATE FUNCTION LIBWEB.BNOWPAPOL(POL CHAR)
RETURNS DECIMAL(7,2)
LANGUAGE SQL
NOT DETERMINISTIC
READS SQL DATA
RETURN (
SELECT
CASE
WHEN NUM IN (1,2) THEN 5
ELSE 2.58
END AS VAL
FROM (
select ROW_NUMBER() OVER() AS NUM ,s.POLLIFE
from LQD943DTA.CAQRTRML8 c
INNER JOIN LSMODXTA.SCSRET s ON c.MCCNTR = s.POLLIFE
WHERE s.NOEMP = ( SELECT NOEMP FROM LSMODDTA.LOLLM04 WHERE POLLIFE = POL)
) AS T WHERE POLLIFE = POL
)
The command runs executed properly
WARNING: 17:55:40 [CREATE - 0 row(s), 0.439 secs] Command processed.
No rows were affected
When I want execute the query a get a error
SELECT LIBWEB.BNOWPAPOL('0010111003') FROM DATAS.DUMMY -- dummy has only one row
I get
[Error Code: -204, SQL State: 42704] [SQL0204] BNOWPAPOL in LIBWEB
type *N not found.
I detect, when I remove the parameter the function works fine!
With this code
CREATE FUNCTION LIBWEB.BNOWPAPOL()
RETURNS DECIMAL(7,2)
LANGUAGE SQL
NOT DETERMINISTIC
READS SQL DATA
RETURN (
SELECT
CASE
WHEN NUM IN (1,2) THEN 5
ELSE 2.58
END AS VAL
FROM (
select ROW_NUMBER() OVER() AS NUM ,s.POLLIFE
from LQD943DTA.CAQRTRML8 c
INNER JOIN LSMODXTA.SCSRET s ON c.MCCNTR = s.POLLIFE
WHERE s.NOEMP = ( SELECT NOEMP FROM LSMODDTA.LOLLM04 WHERE POLLIFE = '0010111003')
) AS T WHERE POLLIFE = '0010111003'
)
Why??
This statement:
SELECT LIBWEB.BNOWPAPOL('0010111003') FROM DATAS.DUMMY
causes this error:
[Error Code: -204, SQL State: 42704] [SQL0204] BNOWPAPOL in LIBWEB
type *N not found.
The parm value passed into the BNOWPAPOL() function is supplied as a quoted string with no definition (no CAST). The SELECT statement assumes that it's a VARCHAR value since different length strings might be given at any time and passes it to the server as a VARCHAR.
The original function definition says:
CREATE FUNCTION LIBWEB.BNOWPAPOL(POL CHAR)
The function signature is generated for a single-byte CHAR. (Function definitions can be overloaded to handle different inputs, and signatures are used to differentiate between function versions.)
Since a VARCHAR was passed from the client and only a CHAR function version was found by the server, the returned error fits. Changing the function definition or CASTing to a matching type can solve this kind of problem. (Note that a CHAR(1) parm could only correctly handle a single-character input if a value is CAST.)