i wrote a query and want to order by its number. I did thus but its not working. in this query pre_number are basically numbers 1,2,3,....,. I want order by this number, but its giving random values
select distinct NAME ||'--'|| pre_number as VALUE
from ftcon
where name ='ABC' and status = 'ACTIVE'
order by pre_number ;
I assume that you have stored the number as text. In this case I might get sorted in lexical order, i.e., first all the number beginning with 0, then those with 1, then with 2 etc, no matter how large the number is. E.g., 1, 120, 13, 250, 26, 33, 4, 51, 6. In this case convert it to a number for sorting or better, use a numeric type for this column.
select distinct NAME ||'--'|| pre_number as VALUE
from FTTH.FTTH_CONNECTIONS
where name ='ABC' and status = 'ACTIVE'
order by TO_NUMBER(pre_number);
Related
I am trying to count all rows that meet a criteria, and then also have it list sequence numbers for each row so we can quickly identify those meeting the criteria. Ex: We know 5 meet criteria of Complete Response and we want to know there are 5 but also what those sequence numbers are. How would i write that in my query?
select
count(sf.protocol_subject_id) as responseCount, sf.BEST_RESPONSE
from oncore.sv_sub_followup sf
where sf.protocol_no = $P{pPclNo} and sf.best_response is not null
group by sf.best_response
order by decode(best_response, 'Complete Response', 1, 'Partial Response', 2, 'Stable', 3, 'Progressive', 4, 'Not Evaluable', 5)
I should also mentioned that this is one of 9 subreports compiling a larger report. Not sure if that makes a difference when trying to do this. I'm very new to pl/sql. Thanks in advance for any help.
If I got this right, you need to count the rows for each BEST_RESPONSE for a given PROTOCOL_NO, and also list the IDs (protocol_subject_id) of those rows.
You could use the analityc function LISTAGG, that concatenates field values over a group. Something like this:
SELECT
best_response,
COUNT(protocol_subject_id) AS responsecount,
LISTAGG(protocol_subject_id, ', ') WITHIN GROUP (
ORDER BY
1
) matching_rows_ids
FROM
sv_sub_followup
WHERE
protocol_no = :protocol_no
AND best_response IS NOT NULL
GROUP BY
best_response
ORDER BY
decode(best_response, 'Complete Response', 1, 'Partial Response', 2,
'Stable', 3, 'Progressive', 4, 'Not Evaluable',
5);
You can use a subquery to obtain your results like the below
SELECT responsecount,
sf1.best_response,
sf1.protocol_subject_id
(
select count(sf.protocol_subject_id) AS responsecount,
sf.best_response
FROM oncore.sv_sub_followup sf
WHERE sf.protocol_no = $p{ppclno}
AND sf.best_response IS NOT NULL
GROUP BY sf.best_response)rec,
oncore.sv_sub_followup sf1
WHERE sf1.protocol_no=$p{ppclno}
AND sf1.best_response=rec.best_response
ORDER BY instr('Complete Response,Partial Response,Stable,Progressive,Not Evaluable',sf1.best_response);
My source table looks like this:
id|value|count
Value is a String of values separated by semicolons(;). For example it may look like this
A;B;C;D;
Some may not have values at a certain position, like this
A;;;D;
First, I've selectively moved records to a new table(targettable) based on positions with values using regexp. I achieved this by using [^;]+; for having some value between the semicolons, and [^;]*; for those positions I don't care about. For example, if I wanted the 1st and 4th place to have values, I could incorporate regexp with insert into like this
insert into
targettable tt (id, value, count)
SELECT some_seq.nextval,value, count
FROM source table
WHERE
regexp_like(value, '^[^;]+;[^;]*;[^;]*;[^;]+;')
so now my new table has a list of records that have values at the 1st and 4th position. It may look like this
1|A;B;C;D;|2
2|B;;;E;|1
3|A;D;;D|3
Next there are 2 things I want to do. 1. get rid of values other than 1st and 4th. 2.combine identical values and add up their count. For example, record 1 and 3 are the same, so I want to trim so they become A;D;, and then add their count, so 2+3=5. Now my new table looks like this
1|A;D;|5
2|B;E;|1
As long as I can somehow get to the final table from source table, I don't care about the steps. The intermediate table is not required, but it may help me achieve the final result. I'm not sure if I can go any further with Orcale though. If not, I'll have to move and process the records with Java. Bear in mind I have millions of records, so I would consider the Oracle method if it is possible.
You should be able to skip the intermediate table; just extract the 1st and 4th elements, using the regexp_substr() function, while checking that those are not null:
select regexp_substr(value, '(.*?)(;|$)', 1, 1, null, 1) -- first position
|| ';' || regexp_substr(value, '(.*?)(;|$)', 1, 4, null, 1) -- fourth position
|| ';' as value, -- if you want trailing semicolon
count
from source
where regexp_substr(value, '(.*?)(;|$)', 1, 1, null, 1) is not null
and regexp_substr(value, '(.*?)(;|$)', 1, 4, null, 1) is not null;
VALUE COUNT
------------------ ----------
A;D; 2
B;E; 1
A;D; 3
and then aggregate those results:
select value, sum(count) as count
from (
select regexp_substr(value, '(.*?)(;|$)', 1, 1, null, 1) -- first position
|| ';' || regexp_substr(value, '(.*?)(;|$)', 1, 4, null, 1) -- fourth position
|| ';' as value, -- if you want trailing semicolon
count
from source
where regexp_substr(value, '(.*?)(;|$)', 1, 1, null, 1) is not null
and regexp_substr(value, '(.*?)(;|$)', 1, 4, null, 1) is not null
)
group by value;
VALUE COUNT
------------------ ----------
A;D; 5
B;E; 1
Then for your insert you can use that query, either with an auto-increment ID (12c+), or setting an ID from a sequence via a trigger, or possibly wrapped in another level of subquery to get the value explicitly:
insert into target (id, value, count)
select some_seq.nextval, value, count
from (
select value, sum(count) as count
from (
select regexp_substr(value, '(.*?)(;|$)', 1, 1, null, 1) -- first position
|| ';' || regexp_substr(value, '(.*?)(;|$)', 1, 4, null, 1) -- fourth position
|| ';' as value, -- if you want trailing semicolon
count
from source
where regexp_substr(value, '(.*?)(;|$)', 1, 1, null, 1) is not null
and regexp_substr(value, '(.*?)(;|$)', 1, 4, null, 1) is not null
)
group by value
);
If you're creating a new sequence to do that, so they start from 1, you can use rownum or row_number() instead.
Incidentally, using a keyword or a function name like count as a column name is confusing (sum(count) !?); those might not be your real names though.
I would use regexp_replace to remove the 2nd and 3rd parts of the string, combined with an aggregate query to get the total count, like :
SELECT
regexp_replace(value, '^[^;]+;([^;]*;[^;]*;)[^;]+;', ''),
SUM(count)
FROM source table
WHERE
regexp_like(value, '^[^;]+;[^;]*;[^;]*;[^;]+;')
GROUP BY
regexp_replace(value, '^[^;]+;([^;]*;[^;]*;)[^;]+;', '')
In my table one of column i have a value like below
Y-1
Y-2
Y-3
Y-4
Y-5
Y-6
Y-7
Y-8
Y-9
Y-10
Y-11
Y-12
Y-13
Y-14
when i am order by this column its working fine if the row has value up to Y-9 other wise my result is wrong like below.
Y-1
Y-10
Y-11
Y-12
Y-13
Y-14
Y-2
Y-3
Y-4
Y-5
Y-6
Y-7
Y-8
Y-9
But i want the output like below
Y-1
Y-2
Y-3
Y-4
Y-5
Y-6
Y-7
Y-8
Y-9
Y-10
Y-11
Y-12
Y-13
Y-14
How to acheive the above result.i am using oracle database.Any help will be greatly appreciated!!!!!
Assuming the data is in a table t with a column col and the structure is an alphabetical string followed by dash followed by a number, and both the alphabetical and the number are always not NULL, then:
select col from t
order by substr(col, 1, instr(col, '-')), to_number(substr(col, instr(col, '-')+1))
You can use an order by manipulatinng the column content and cast to number eg:
order by substr(col1, 1,2), TO_NUMBER(sustr(col1, 3,10))
I think the good way is to get constant length field
select col from t
order by substr(col, 1, 2)|| lpad(substr(col, 3),5,'0')
it will correct work only with two nondigit simbol in begining of string up to 99999 number
I have to insert multiple row into a table from a file structured like this:
BANAC2C100017701007_X75 _CA 4X2 CT MLCR DR SX EP 160 E4
where 4x2, MLCR, 160 E4 have to be inserted into the same column for the same code BANAC2C100017701007. As example, the table should be structured like this:
After to split the elements from the file, how can I put them into the table? Any suggestion?
It can be done with sqlldr. I have made some assumptions, but if the data is one row per line as you describe above, with the same number of elements a line, a properly constructed control file with multiple "into table" statements can write different parts of one row of data as multiple rows to the same table.
The control file:
LOAD DATA
infile "file.dat"
TRUNCATE
INTO TABLE data_table
(entirerow BOUNDFILLER char(4000)
,code expression "regexp_substr(:entirerow, '(.*?)(_)', 1, 1, NULL, 1)"
,desc expression "regexp_substr(:entirerow, '(.*?)( +)', 1, 3, NULL, 1)"
)
INTO TABLE data_table
(entirerow BOUNDFILLER position(1) char(4000)
,code expression "regexp_substr(:entirerow, '(.*?)(_)', 1, 1, NULL, 1)"
,desc expression "regexp_substr(:entirerow, '(.*?)( +)', 1, 5, NULL, 1)"
)
INTO TABLE data_table
(entirerow BOUNDFILLER position(1) char(4000)
,code expression "regexp_substr(:entirerow, '(.*?)(_)', 1, 1, NULL, 1)"
,desc expression "regexp_substr(:entirerow, '(.*?)( +)', 1, 9, NULL, 1) || ' ' ||
regexp_substr(:entirerow, '(.*?)( +|$)', 1, 10, NULL, 1)"
)
A couple of things to note:
Since there are no delimiters, and none are specified, the entire row will be read into the first field "entirerow". Since it is not a column in the table, and it is defined as BOUNDFILLER, it is "remembered" for use later.
The next field "code" is found in the control file. No data field exists to match it with, but sqlldr finds it matches a column in the table and sees it is an expression so it applies the expression with the intention of putting the result into the column. The expression uses REGEXP_SUBSTR against the remembered BOUNDFILLER to cut out the parts we need. For code, get the characters up to but not including the first underscore. For desc, get the 3rd set of characters that are followed by one or more spaces (but not the spaces).
For the second logical row, we need to re-position the logical pointer back to the beginning of the row read in so sqlldr can re-process. Otherwise the logical pointer is at the end of the data and nothing will be returned. This is done with the "position" parameter seen in the "entirerow" definition of the 2nd and 3rd "into table" statements. The last "into table" follows the previous paradigm of just getting the 9th and 10th fields and concatenating them together. I chose to do this rather than come up with another regex to do it as it keeps consistency with the other fields, plus if you want to change it in the future it will be easier to follow.
As you can see it works and is reusable:
SQL> select code, desc
from data_table;
CODE DESC
------------------------- -------------
BANAC2C100017701007 4X2
BANAC2C100017701007 MLCR
BANAC2C100017701007 160 E4
Possible caveat: each row is being scanned 3 times, and the regexp calls are expensive so depending on the amount of data you need to load this may not be a feasible solution for your situation.
I am building stacked column flash chart on my query. I would like to split values in column for different locations. For argument sake I have 5 ids in location 41, 3 ids in location 21, 8 ids in location 1
select
'' link,
To_Char(ENQUIRED_DATE,'MON-YY') label,
count(decode(location_id,41,id,0)) "location1",
count(decode(location_id,21,id,0)) "location2",
count(decode(location_id,1,id,0)) "location3"
from "my_table"
where
some_conditions = 'Y';
as a result of this query Apex is creating stacked column with three separate parts( hurray!), however it instead of having values 5,3 and 8, it returns three regions 16,16,16. ( 16 = 5 +3+8).
So obviously Apex is going through all decode conditions and adding all values.
I am trying to achieve something described in this
article
Apex doesn't appear to be doing anything funky, you'd get the same result running that query through SQL*Plus. When you do:
count(decode(location_id,41,id,0)) "location1",
.. then the count gets incremented for every row - it doesn't matter which column you include, and the zero is just treated as any fixed value. I think you meant to use sum:
sum(decode(location_id,41,1,0)) "location1",
Here each row is assigned either zero or one, and summing those gives you the number that got one, which is the number that had the specified id value.
Personally I'd generally use caseover decode, but the result is the same:
sum(case when location_id = 41 then 1 else 0 end) "location1",