Open a refcursor for a PL/SQL statement? - oracle

I'm creating a report for a business using Oracle and SSRS. The report requires me to aggregate contiguous ranges of serial numbers, which can consist of alphanumerics.
For example, say I have the following serials:
OPS114
OPS115
OPS116
OPS117
OPS145
OPS146
OPS160
890RPT
896RPT
897RPT
The report should have a single aggregate row for each contiguous range, with the count of each range, like so:
OPS114 - OPS117 (4)
OPS145 - OPS146 (2)
OPS160 - OPS160 (1)
890RPT - 890RPT (1)
896RPT - 897RPT (2)
I've pulled the data I need, and I'm bulk-collecting it into a table variable. Now, I need to aggregate the rows - this wouldn't be bad if I only needed to manipulate the data, but I need to have this available as a query for the refcursor. Can I open a refcursor for a PL/SQL FOR loop, or am I barking up the wrong tree? I've attempted to Google this, but the "cursor for loop" is not what I'm looking for. The alternative is to try to aggregate the results in SSRS using VB. (So either way, it won't be a good time.) I'm not sure if I have access to create a SQL table type for this, so this is the alternative I've sought.
If anyone has any experience with this, it would be greatly appreciated!

You could do this from a single SQL statement but you need to define the data better. Your column stores strings but you are using them as numbers to find out the range. And it seems the number part could either be before or after the string part.
If you are able to write some logic that separates out the numbers like this (and maybe keep the string part in another column)-
114
115
116
117
145
146
160
890
896
897
Then it reduces to a simple gaps and islands problem.
Step 1 - Select rownum along with this column (this would be a continuous sequence starting from 1)
Step 2 - Subtract rownum from this numeric data column.
Step 3 - Group by that result
Step 4 - Get min(numeric value) and max(numeric value) and count(numeric value) from the group which will be your result when combined as string.
Numeric_part Rownum Difference
------------ ------ ------------
114 1 113
115 2 113
116 3 113
117 4 113
145 5 140
146 6 140
160 7 153
890 8 882
896 9 887
897 10 887
Grouping this by Difference column, you get -
Difference Min(num) Max(num) count(num) Result
---------- --------- ---------- ---------- -----------------
113 114 117 4 114 - 117 (4)
140 145 146 2 145 - 146 (2)
153 160 160 1 160 - 160 (1)
882 890 890 1 890 - 890 (1)
887 896 897 2 896 - 897 (2)
That SQL statement can be used in PLSQL to return a cursor as in this link that #MickMnemonic has in the comments.
Basically -
OPEN cursor_variable FOR SELECT something FROM something...;

I spoke to a coworker about this and he had an idea that I've been able to implement.
I was able to create a pipelined function that handled my data selection and transforms for me; this allowed me to aggregate my rows as I needed to and only pass rows after a contiguous range was complete.
I am now calling that function from a procedure using the SELECT ... FROM TABLE(MYFUNCTION()) syntax. This allows me to get everything into a refcursor without much issue.
While this may not be performant (looping over a cursor, and manually aggregating), this is for a monthly report, and so I won't attempt to optimize until it's necessary (as we have other work to do).

Related

Transpose without PIVOT in ORACLE

currently I am using pl/sql Developer(Oracle). I am told to convert a Row wise arranged data into columns but without the use of PIVOT. Since the Table I am working on dynamically changes, I am not able to use DECODE too.
POLICY SEQS INVDATE SUM(AMT)
-------- ------ ----------- ----------
policA 123 01-JAN-10 40
policA 123 01-FEB-10 50
policA 123 01-MAR-10 60
policA 456 01-JAN-10 360
policA 456 01-FEB-10 450
policA 456 01-MAR-10 540
policA 789 01-FEB-10 1000
polcA 789 01-MAR-10 1000
I have to re-arrange the dates and the sum of amounts column wise. So that the Single Policy and Single SEQS will have the dates and its amount column wise in a line.
"POLICY","SEQS","INST1","INST1SUM","INST2","INST2SUM","INST3","INST3SUM"
"policA","123","01-JAN-10","40","01-FEB-10","50","01-MAR-10","60"
"policA","456","01-JAN-10","360","01-FEB-10","450","01-MAR-10","540"
"policA","789","01-FEB-10","1000","01-MAR-10","1000"
Some Policy might not be starting from Jan, so the INST1 must be from feb, INST2 must be Mar and INST3 and corresponding INSTSUM must be NULL.
Is there any way that this can be done using CROSS JOINS or using xml function?
Can I use xmlagg with alternative data (INST and SUM)?
I have done some research and am not able to solve this out. Can you please help me with this?

Getting ORA-00937 although selected column is included in the GROUP By clause

Below is my SQL. I don't know what else to do to not receive "not a single-group group function" error.
SELECT PP.PENSIONERID,SUM(ROUND(EXP(SUM(LN(INFIDX)))*AMOUNT,2))AMOUNT
FROM AG_PEN_PARTS PP
JOIN LAG_INF_INDICES INF ON INF.INFYEAR>=EXTRACT(YEAR FROM PP.BEGDATE)
GROUP BY PP.PENSIONERID,PP.AMOUNT
I can't include ROUND(EXP(SUM(LN(INFIDX)))*AMOUNT,2) in the GROUP BY clause as group functions are not allowed there. Any ideas?
EDIT: Here're my two tables:
LAG_INF_INDICES: INFYEAR | INFIDX
----------------
2010 1.079
2011 1.116
2012 1.011
2013 1.024
AG_PEN_PARTS: PENSIONERID | BEGDATE | AMOUNT
------------------------------
112 07/20/2013 120
113 01/10/2012 100
112 12/12/2010 90
114 03/05/2011 70
here's the result I would expect to get:
PENSIONERID | AMOUNT
----------------------
112 235.08
113 103.53
114 80.87
So for instance, for the pensionerid 112 there're two records. First I need to multiply 90 with the multiplication of the INFIDX values beginning from 2010 and then 120 with the multiplication of the INFIDX values beginning from 2013 and then get sum of these two values.
To get this working, move the second SUM to another SELECT with a second GROUP BY:
SELECT PENSIONERID, SUM(AMOUNT) as AMOUNT FROM (
SELECT PP.PENSIONERID, ROUND(EXP(SUM(LN(INFIDX)))*AMOUNT,2) AMOUNT
FROM AG_PEN_PARTS PP
JOIN LAG_INF_INDICES INF ON INF.INFYEAR>=EXTRACT(YEAR FROM PP.BEGDATE)
GROUP BY PP.PENSIONERID, AMOUNT
) GROUP BY PENSIONERID
SQL Fiddle

Aggregating the table from many rows to one

I have a table with data as follows:
Name opening receipt transfer closing
abcd 1000 40 30 1010
efg 256 109 219 146
hjk 9356 210 210 9356
mnp 2000
I need is a result like this -
opening_abcd receipt_abcd transfer_abcd closing_abcd opening_efg receipt_efg .....
1000 40 30 1010 256 109 .....
What I have tried till now is to create multiple views one by one having the one row values like -
select opening opening_abcd, receipt receipt_abcd, transfer transfer_abcd, closing closing_abcd
from this_table
where name = 'abcd'
and similarly for others and then I have merged them together by using JOIN.
Is there a solution possible if I can select all of them in one query or any other way which is better than mine?

SQL Oracle Sort string (numbers) and (letters with numbers)

I am new to oracle and I have a problem.
I have a column named file_id.
When I do an order by it sorts strings such as
1
1
10
100
11
11
110
114
12
300
31
4200
B14
B170
B18
edit:
I would like it to sort this way.
1
1
10
11
11
12
31
100
300
4200
B14
B18
B170
The answer below works perfectly. Only other problem I ran into now..I have records that are blank. How could I make the blank records order at the end?
1
1
10
11
11
12
31
100
300
4200
BLANK
BLANK
BLANK
BLANK
BLANK
B14
B18
B170
Thank you for your help.
select column
from table
order by
regexp_substr(column, '^\D*') nulls first,
to_number(regexp_substr(column, '\d+'))
fiddle
This is an old question, but it was the first hit on google so I thought I'd share an alternative solution:
select column
from table
order by
LPAD(column, 10)
The LPAD function pads the left-side of the string with spaces so that the results will be sorted numerically. This works for non-numeric values, and null values will be sorted last. This works well if you know the maximum length of the strings to be sorted (you may need to adjust the second parameter to suit your needs).
Source: http://www.techonthenet.com/oracle/questions/sort1.php
EDIT:
I noticed that while my solution works well for my case, the output is slightly different from the accepted answer (http://www.sqlfiddle.com/#!4/d935b8/2/0):
1
1
10
11
11
12
31
100
110
114
300
A14
A18
4200
A170
(null)
(null)
4200 should come after 300. For my situation this is good enough, but this may not always be the case.
Based on the previous solution:
SELECT column
FROM table
ORDER BY LPAD(column, (SELECT MAX(LENGTH(column)) FROM table)) ASC
The advantage of this approach is that you don't need know the table column size.

Hive: Joining tables with different scenarios

I have a question on joining tables in a different scenario. Please find the sample tables below.
Capacity of expected table row 3-5 should be repeated as table 2 does not have those fields.
could anyone please help to get expected table?
Table 1:
No ProjectID Capacity
1 514 4
2 418 10
3 418 30
4 401 40
5 502 41
Table2:
NO ProjectID Capacity1 Capacity2
1 514 4 10
2 418 10 20
Expected Table:
NO ProjectID Capacity1 Capacity2
1 514 4 10
2 418 10 20
3 418 30 30
4 401 40 40
5 502 41 41
1.Do left outer join
2.For the values not matching take them from table 1 with if condition.
select t1.no,t1.projectid,t1.capacity1,if(t2.capacity2 is null,t1.capacity,t2.capacity)
from table1 t1 left outer join table2 t2 on t1.no=t2.no
I think above query meets your requirement let me know if need any more help.

Resources