I have an input string that was delimited by white space. In the expression builder I try using expression language to split it. Splitting works fine as array output [] but issues come out when input string is not a sequence number red box.
Now, I need to sort the array but I don't know how to use it.
The ideal condition is
1 2 3 4
1 3 4
1 4
2 4
4
but sometimes input string is not sequential number.
1 4 2
1 2 4 3
3 4 1 2
anyone can help me how to use sort the array value ?
for right now, I just tried alternative by using iif. So, here is some code that works
iif( split(LG_Pembelajaran_Guru_01, ' ')[1] == '1', true()
, iif( split(LG_Pembelajaran_Guru_01, ' ')[2] == '1', true(),
, iif( split(LG_Pembelajaran_Guru_01, ' ')[3] == '1', true(),
, iif( split(LG_Pembelajaran_Guru_01, ' ')[4] == '1', true(), false())))
)
iif( split(LG_Pembelajaran_Guru_01, ' ')[2] == '2', true()
, iif( split(LG_Pembelajaran_Guru_01, ' ')[1] == '2', true(),
, iif( split(LG_Pembelajaran_Guru_01, ' ')[3] == '2', true(),
, iif( split(LG_Pembelajaran_Guru_01, ' ')[4] == '2', true(), false())))
)
iif( split(LG_Pembelajaran_Guru_01, ' ')[3] == '3', true()
, iif( split(LG_Pembelajaran_Guru_01, ' ')[1] == '3', true(),
, iif( split(LG_Pembelajaran_Guru_01, ' ')[2] == '3', true(),
, iif( split(LG_Pembelajaran_Guru_01, ' ')[4] == '3', true(), false())))
)
iif( split(LG_Pembelajaran_Guru_01, ' ')[4] == '4', true()
, iif( split(LG_Pembelajaran_Guru_01, ' ')[1] == '4', true()
, iif( split(LG_Pembelajaran_Guru_01, ' ')[2] == '4', true()
, iif( split(LG_Pembelajaran_Guru_01, ' ')[3] == '4', true(), false())))
)
but using iif is less efficient when there are long array (let say [1 ... 8]) and must be rigorous.
Related
I have the following table EPS_PROPOSAL_EXT_T that I've condensed:
+--------------------+------------+------------+
| PROPOSAL_NUMBER |ONR_OPTION_1|ONR_Option_2| ...
+--------------------+------------+------------+
| 1234 | N | N | ...
| 1235 | Y | Y | ...
| 1236 | N | Y | ...
+--------------------+------------+------------+
The problem here is that I need to do a LISTAGG across all those different ONR_Options, but I can't have them show up as 'Y'. I need to replace the ones that are 'Y' with something human understandable. See SELECT statement below where I've attempted to do this:
SELECT eps.PROPOSAL_NUMBER, eps.TITLE, per.FULL_NAME, ext.NRP_IREF_CD, ext.RESEARCH_TYPE_CD,
nsf.NSF_CODE, ext.NPS_THRUST_DESCRIPTION,
CASE
WHEN ext.ONR_Option_1 = 'Y' THEN 'Option 1'
WHEN ext.ONR_Option_2 = 'Y' THEN 'Option 2'
WHEN ext.ONR_Option_3 = 'Y' THEN 'Option 3'
WHEN ext.ONR_Option_4 = 'Y' THEN 'Option 4'
WHEN ext.ONR_Option_5 = 'Y' THEN 'Option 5'
WHEN ext.ONR_Option_6 = 'Y' THEN 'Option 6'
WHEN ext.ONR_Option_7 = 'Y' THEN 'Option 7'
WHEN ext.ONR_Option_8 = 'Y' THEN 'Option 8'
WHEN ext.ONR_Option_9 = 'Y' THEN 'Option 9'
WHEN ext.ONR_NOT_APPLICABLE = 'Y' THEN 'Not Applicable'
ELSE ''
END ONR
FROM EPS_PROPOSAL eps
LEFT JOIN EPS_PROPOSAL_EXT_T ext
ON eps.PROPOSAL_NUMBER = ext.PROPOSAL_NUMBER
LEFT JOIN EPS_PROP_PERSON per
ON eps.PROPOSAL_NUMBER = per.PROPOSAL_NUMBER AND
(per.PROP_PERSON_ROLE_ID = 'PI' OR per.PROP_PERSON_ROLE_ID = 'PD')
LEFT JOIN EPS_PROP_ABSTRACT abs
ON eps.PROPOSAL_NUMBER = abs.PROPOSAL_NUMBER
LEFT JOIN NSF_CODES nsf
ON eps.NSF_CODE = nsf.NSF_SEQUENCE_NUMBER
WHERE eps.OWNED_BY_UNIT = '401' AND eps.requested_start_date_initial >= DATE '2019-10-01';
This works but you can see the problem with it, right? The way I've setup my CASE statement, it will not take into account when for a particular PROPOSAL_NUMBER there is more than one ONR_Option that is TRUE.
In the example I gave above, if I search for a proposal with number = 1235; the ONR result should be - 'Option 1, Option 2'.
How do I solve this dilemma with LISTAGG and COALESCE? Or is that the way to go about this?
LISTAGG doesn't help here. Why? Because your data model is wrong. In my opinion, should be something like this:
SQL> with test (prop, onr, status) as
2 (select 1234, '1', 'N' from dual union all
3 select 1234, '2', 'N' from dual union all
4 select 1234, '3', 'Y' from dual union all
5 select 1235, '1', 'Y' from dual union all
6 select 1235, '2', 'Y' from dual union all
7 select 1235, '3', 'N' from dual
8 )
9 select prop,
10 listagg(case when status = 'Y' then 'Option ' || onr end, ',')
11 within group (order by onr) as result
12 from test
13 group by prop;
PROP RESULT
---------- ------------------------------
1234 Option 3
1235 Option 1,Option 2
SQL>
The way it currently is, see if this - concatenation of as many CASEs as there are ONR_OPTION columns in the table, which scales as a goat you'd like to teach to fly, along with pain of removing multiple commas - helps:
SQL> with test (prop, onr_1, onr_2, onr_3) as
2 (select 1234, 'N', 'N', 'Y' from dual union all
3 select 1235, 'Y', 'Y', 'N' from dual
4 )
5 select prop,
6 trim(both ',' from
7 case when onr_1 = 'Y' then 'Option 1' else null end ||','||
8 case when onr_2 = 'Y' then 'Option 2' else null end ||','||
9 case when onr_3 = 'Y' then 'Option 3' else null end
10 ) as result
11 from test;
PROP RESULT
---------- ------------------------------
1234 Option 3
1235 Option 1,Option 2
SQL>
I am experiencing row lock contention in my oracle DB. I tried to kill some session to unlock them, but this rows are still locked.
I know exact which row are locked.
Can I find the session ID that has locked this row. I can get the ROWID of that row.
As the good folks on AskTom say, we don't maintain a list of locked rows
But, if you want to try this - it'll show you locks by USER in your database, including Row locks.
SELECT
p.username username,
p.pid pid,
s.sid sid,
s.serial# serial,
p.spid spid,
s.username ora,
DECODE(l2.type, 'TX', 'TRANSACTION ROW-LEVEL', 'RT', 'REDO-LOG', 'TS', 'TEMPORARY SEGMENT ', 'TD', 'TABLE LOCK', 'TM', 'ROW LOCK'
, l2.type) vlock,
DECODE(l2.type, 'TX', 'DML LOCK', 'RT', 'REDO LOG', 'TS', 'TEMPORARY SEGMENT', 'TD', DECODE(l2.lmode + l2.request, 4, 'PARSE '
|| u.name || '.' || o.name, 6, 'DDL', l2.lmode + l2.request), 'TM', 'DML ' || u.name || '.' || o.name, l2.type) type,
DECODE(l2.lmode + l2.request, 2, 'RS', 3, 'RX', 4, 'S', 5, 'SRX', 6, 'X', l2.lmode + l2.request) lmode,
DECODE(l2.request, 0, NULL, 'WAIT') wait
FROM
gv$process p,
gv$_lock l1,
gv$lock l2,
gv$resource r,
sys.obj$ o,
sys.user$ u,
gv$session s
WHERE
s.paddr = p.addr
AND s.saddr = l1.saddr
AND l1.raddr = r.addr
AND l2.addr = l1.laddr
AND l2.type <> 'MR'
AND r.id1 = o.obj# (+)
AND o.owner# = u.user# (+)
--AND u.name = 'GME'
AND ( :user_name IS NULL
OR s.username LIKE upper(:user_name) )
ORDER BY
p.username,
p.pid,
p.spid,
ora,
DECODE(l2.type, 'TX', 'TRANSACTION ROW-LEVEL', 'RT', 'REDO-LOG', 'TS', 'TEMPORARY SEGMENT ', 'TD', 'TABLE LOCK', 'TM', 'ROW LOCK'
, l2.type)
This is a report in SQL Developer.
I have this line of query in SQL sever and would like to convert it in Oracle. Would someone please provide some pointer?
CAR_CD CAR_YR CAR_MONTH CAR_SEQ
LXR 2017 12 1234
I would like the outcome like this, using the query below in ORACLE database, not in SQWL.
LXR1712001234
CAR_CD + SUBSTRING(CAST(CAR_YR AS VARCHAR(4)),3,2) + CAR_MONTH + RIGHT(REPLICATE('0',6) + CAST(CAR_SEQ AS VARCHAR(6)),6) AS CAR_NUMBER,
SELECT CAR_CD || SUBSTR(CAR_YR,-2) || CAR_MONTH || LPAD(CAR_SEQ,6,'0') FROM yourtable;
Try this.
Use SUBSTR and LPAD:
SELECT CAR_CD
|| SUBSTR( CAR_YR, -2 ) -- or SUBSTR( CAR_YR, 3, 2 )
|| CAR_MONTH -- or LPAD( CAR_MONTH, 2, '0' ) for a zero-padded string.
|| LPAD( CAR_SEQ, 6, '0' ) AS Car_number
FROM your_table
However, you would be better converting your year and month columns to a single DATE data type then you could do:
SELECT CAR_CD
|| TO_CHAR( CAR_YEAR_MONTH_DATE, 'YYMM' ) -- or 'YYFMMM' without leading zero for month
|| LPAD( CAR_SEQ, 6, '0' ) AS Car_number
FROM your_table
using http://www.sqlines.com/online
And replace concat operator +by || :
SGTC_CD ||
SUBSTR(TO_CHAR(SG_FY(4)),3,2) ||
SG_MONTH || SUBSTR(RPAD('0', LENGTH('0') *6, '0') ||
TO_CHAR(SG_SEQ(6)),
GREATEST(-LENGTH(RPAD('0', LENGTH('0') *6, '0') ||
TO_CHAR(SG_SEQ AS VARCHAR2(6))),-6))
I have three columns in a tables
Column names with datatypes
First_week - Number
Days - Number
Second_week- Number
Column Values
8
6
11
I want to concatenate these values such that result returns 8/6/11
If anyone of the column values are null then the concatenation must be 8/-/11
If all are null then the result must be -/-/-
How can this be achieved in an oracle query?
To achieve that you could use decode, nvl functions or case expression. Here is an example of using decode:
with your_table(First_week, Days, Second_week) as(
select 8, 6, 11 from dual union all
select 5, null, 12 from dual union all
select null, 7, null from dual union all
select null, null, null from dual
)
select decode(to_char(First_week), null, '-', to_char(First_week)) || '/' ||
decode(to_char(Days), null, '-', to_char(Days)) || '/' ||
decode(to_char(Second_week), null, '-', to_char(Second_week)) as result
from your_table
RESULT
---------
8/6/11
5/-/12
-/7/-
-/-/-
http://www.techonthenet.com/oracle/functions/coalesce.php
"In Oracle/PLSQL, the coalesce function returns the first non-null expression in the list. If all expressions evaluate to null, then the coalesce function will return null."
http://docs.oracle.com/cd/B19306_01/server.102/b14200/operators003.htm#i997789
"|| Concatenates character strings and CLOB data."
Now we have all the building blocks to write something like:
COALESCE(First_week, '-') || '/' || COALESCE(Days, '-') || '/' || COALESCE(Second_week, '-')
What About This
SELECT NVL (TO_CHAR (FIRST_WEEK), '-')
|| '/'
|| NVL (TO_CHAR (DAYS), '-')
|| '/'
|| NVL (TO_CHAR (SECOND_WEEK), '-')
FROM YOUR_TABLE
I have two tables.
table A ( id, title, department )
1, title 1 , 1
2, title 2 , 1
3, title 3 , 2
table B ( uid, mid , is_active )
1, 1, 1
2, 1, 1
3, 3, 1
And here is my query statement:
$this->db->select(" id, title ")
->select(' count( B.uid ) AS B_count ')
//->select(' IFNULL( COUNT( `B.uid`), 0) AS B_count ', false)
//->select( " IFNULL( COUNT(DISTINCT B.uid) AS B_count , 0 )" )
->join( 'B', ' B.mid = A.id ' , 'left')
->where('department', 1 )
->where('B.is_active' , 1 )
->limit( $limit, $offset );
$this->db->group_by( array("B.mid") );
return $this->get_all();
And i expect to get result like this
id, title , B_count
1, title 1, 2
2, title 2, 0
However i can get only the first record but no the second record.
And i already have tried IFNULL function and left join tables.
really don't know how to solve the issue.
Does anyone know the solution or what the problem is? Thanks in advance.
use get('A') instead of get_all()