How to SUM a column in an oracle apex collection - oracle

So I am trying to output the total of a column from a collection.
This is the first query I tried
select sum(C007),sum(C007) A FROM APEX_COLLECTIONS WHERE COLLECTION_NAME='PURCHASE'
This is the second query I tried
select sum(C005*C007),sum(C005*C007) A FROM APEX_COLLECTIONS WHERE COLLECTION_NAME='PURCHASE'
Both produce the same result which list out all the values in the column insted of suming them
Expected Results:
10
Actual Results:
2
2
2
2
2
Please help

Looks like you did something wrong.
I created sample page; it contains a button (which will just submit the page) and an item which will display total (sum of collection's values). Item gets populated by a process which contains everything (for simplicity):
if not apex_collection.collection_exists('PURCHASE') then
apex_collection.create_collection('PURCHASE');
end if;
apex_collection.add_member(
p_collection_name => 'PURCHASE',
p_c001 => 'Little',
p_c007 => 100); --> 100 ...
apex_collection.add_member(
p_collection_name => 'PURCHASE',
p_c001 => 'Foot',
p_c007 => 200); --> ... + 200 = 300
select sum(c007)
into :P7_TOTAL
from apex_collections
where collection_name = 'PURCHASE';
When ran (and after button was pressed), item's value is - as expected - 300.

Related

oracle apex - multiple default value for APEX_ITEM.Select_list_from_query_xl with attribute multiple="multiple"

I'm using oracle 18.
I create dynamically an apex_item's select list in multiple selection mode.
Using the parameter P_attributes => 'multiple="multiple" '
It's working fine except when I try to feed it multiple default value
If I set the value to a single element, it's working.
But if I set the value with more than one item no element is selected.
I tried to space the items with , ; : and space with no result.
Please tell there's a way cause i realy need it to work. End of projetc is near and I don't have a B plan.
lHtml := APEX_ITEM.Select_list_from_query(P_idx => 20,
P_value => '2:3',
P_query => 'select ''blue'' d,1 r from dual union select ''red'',2 from dual union select ''green'',3 from dual',
P_attributes => 'multiple="multiple"',
P_item_id => 'testMultiple',
P_item_label => 'Test Multiple Default');
HTP.P ( lHtml );

find rows with same value using LINQ

The issue with the image above is that
I want to apply a bonus share to the shareholders based on the TransLog_Date
Because the shareholder in row 1 has already transferred a portion of his shares to the holder in row 3, his new share value is what shows in row 2 column 5 as 100000
I want the bonus share to be calculated based on the value in row 2, column 5 but not on row 1, column 5.
I have tried several codes in LINQ but getting it wrong as the value is multiplied on all the 3 records which is totally wrong.
var transQuery = (from tq in db.TransactionsLogDbSet
where (tq.TransactionType == TransactionType.PurchaseOfShares)
|| (tq.TransactionType == TransactionType.TransferOfShares)
|| (tq.TransactionType == TransactionType.BonusSharesDeclared)
select tq);
var query = from row in db.TransactionsLogDbSet
group row by row.Transholder_ID into g
select new { Count = g.Count() };
OK, i think I finally got what you want.
Is that right?: Group all rows of some TransactionTypes by the Transholder_ID, then from each group select the row with the maximum Translog_Date, giving you the most recent row per Transholder_ID. Once you have the result of that, you can iterate over it to calculate whatever you need.
from t in db.TransactionLogDbSet
where where tq.TransactionType == TransactionType.PurchaseOfShares || tq.TransactionType == TransactionType.TransferOfShares || tq.TransactionType == TransactionType.BonusSharesDeclared
group t by t.Transholder_ID into g
select (from t1 in g orderby t1.Translog_Date descending select t1).First();
EDIT: I'll continue to edit the following part of my answer as long as we finally arrive at the correct query.
From you last comment so far it follows that you just want to select all rows with a TransLog_Date <= a given DateTime qualifiedDate.
from t in db.TransactionLogDbSet
where t.TransactionLog_Date <= qualifiedDate
select t
Is that it?

Showing Different aggregate for different products

I want a code which is used to generate aggregate product by product. The product aggregate can be any like from Year to Date(YTD), Months to Date(MTD) and Quarter to Date(QTD). The user will pass the parameter on that basis the code should decide what kind of output the user wants.
If the Year is passing in the parameter than the code should generate the aggregate from the starting of the year to the sysdate.
If the Quarter No is passing in the parameter than the code should generate the aggregate from the starting of the quarter to the sysdate.
If the Month is passing in the parameter than the code should generate the aggregate from the starting of the month to the sysdate.
It means that on the basis of the parameter it should be able to decide which kind of user want from those 3. My input data is like this-
Product Table
Product_ID Product_name Price
1 Mobile 200
2 T.V. 400
3 Mixer 300
and
Sales Table-
Product_ID Sales_Date Quantity
1 01-01-2015 30
2 03-01-2015 40
3 06-02-2015 10
1 22-03-2015 30
2 09-04-2015 10
3 21-05-2015 40
1 04-06-2015 40
2 29-07-2015 30
1 31-08-2015 30
3 14-09-2015 30
And my ouput column contains 3 columns that are- Product_id, Product_Name and Total. The column Total_Amount(quantity*price) have to calculate sale on the basis of input given by user and is be something like this-
For example ,
If pro_test is the procedure then
call pro_test('YTD') -- Should Return the ProductWise YTD,
call pro_test('QTD') -- Should Return the ProductWise QTD and so on..
You are looking for a WHERE clause :-) List your conditions with OR and you are done.
select
p.product_id,
p.product_name,
coalesce(sum(s.quantity * p.price), 0) as total
from product p
left join sales s on s.product_id = p.product_id
where
(:aggregate = 'YTD' and to_char(s.sales_date, 'yyyy') = to_char(sysdate, 'yyyy'))
or
(:aggregate = 'MTD' and to_char(s.sales_date, 'yyyymm') = to_char(sysdate, 'yyyymm'))
or
(:aggregate = 'QTD' and to_char(s.sales_date, 'yyyyq') = to_char(sysdate, 'yyyyq'))
group by p.product_id, p.product_name;
EDIT: Here is how the corresponding PL/SQL function would look like:
create or replace function matches_date_aggregate(in_sales_date date, in_aggregate char)
return integer as
begin
if (in_aggregate = 'YTD' and to_char(in_sales_date, 'yyyy') = to_char(sysdate, 'yyyy'))
or (in_aggregate = 'MTD' and to_char(in_sales_date, 'yyyymm') = to_char(sysdate, 'yyyymm'))
or (in_aggregate = 'QTD' and to_char(in_sales_date, 'yyyyq') = to_char(sysdate, 'yyyyq')) then
return 1;
else
return 0;
end if;
end matches_date_aggregate;
Your query's WHERE clause would become:
where matches_date_aggregate(s.sales_date, :aggregate) = 1
The function cannot return BOOLEAN unfortunately, for even though Oracle's PL/SQL knows the BOOLEAN data type, Oracle SQL doesn't.

Changing a 0 or 1 value to "Yes" and "No" in APEX report

I have a report region on my page and it's getting data from a students table. Within that table, there's a column called cv_lodged which is either 1 or 0 representing Yes or No. In the report it shows this columns data as 0 or 1, I want to change it to show Yes or No.
How can I do this?
You can put a CASE statement in your query
SELECT (CASE WHEN cv_lodged = 1
THEN 'Yes'
ELSE 'No'
END) cv_lodged,
other_columns
FROM students
If this is something that is likely to be common, you're probably better served creating a function that converts your pseudo-boolean data to a string. Something like
CREATE FUNCTION my_bool_to_str( p_num IN NUMBER )
RETURN VARCHAR2
IS
BEGIN
IF( p_num = 1 )
THEN
RETURN 'Yes';
ELSE
RETURN 'No';
END IF;
END;
that you would call in your query
SELECT my_bool_to_str( cv_lodged ) cv_lodged,
other_columns
FROM students
I've usually just created a static LOV in Shared Components called YES1_NO0, with 2 entries: 'Yes' -> 1, 'No' -> 0.
In Interactive Reports you can then change these columns. To do this, edit the column of the IR. Change 'Display Type' to 'Display as Text (based on LOV, escape special characters)'. Then under 'List of Values' set 'Column Filter Type' to 'Use Named List of Values to Filter Exact Match' and select the named LOV.
But sure, you can totally do this in SQL.
Currently, there is a simpler way to do this.
First open the APEX page with the "Yes/No" item as developer and click on the item to open the page item settings.
Change the Settings value from Use component settings to Custom.
Afterwards change the Yes Value to 1 and the No Value to 0.
This solution has some advantages:
No need to decode the value 1 or 0 to Y or N (select decode(mycolumn, 1, 'Y', 'N') from mytable where id = 123;) or select case when ...
No need to decode the submitted value back to 1 or 0 from Y or N

HBase getting all timestamped values for a cell

i have the following scenario in my hbase instance
hbase(main):002:0> create 'test', 'cf'
0 row(s) in 1.4690 seconds
hbase(main):003:0> put 'test', 'row1', 'cf:a', 'value1'
0 row(s) in 0.1480 seconds
hbase(main):004:0> put 'test', 'row2', 'cf:b', 'value2'
0 row(s) in 0.0070 seconds
hbase(main):005:0> put 'test', 'row3', 'cf:c', 'value3'
0 row(s) in 0.0120 seconds
hbase(main):006:0> put 'test', 'row3', 'cf:c', 'value4'
0 row(s) in 0.0070 seconds
Now if you will see, the last two inserts are for the same column family, same column and same key. But if i understand hbase properly cf:c+row3 represent a cell which will have all timestamped versions of inserted value.
But a simple scan return only recent value
hbase(main):010:0> scan 'test'
ROW COLUMN+CELL
row1 column=cf:a, timestamp=1317945279379, value=value1
row2 column=cf:b, timestamp=1317945285731, value=value2
row3 column=cf:c, timestamp=1317945301466, value=value4
3 row(s) in 0.0250 seconds
How do i get all timestamped values for a cell, or how to perform time range based query?
In order to see versions of a column you need to give the version count.
scan 'test', {VERSIONS => 3}
will give you 2 versions of columns if they are available. you can use it in get aswell :
get 'test', 'row3', {COLUMN => 'cf:c', VERSIONS => 3}
for getting the value of a spesific time you can use TIMESTAMP aswell.
get 'test', 'row3', {COLUMN => 'cf:c', TIMESTAMP => 1317945301466}
if you need to get values "between" 2 timestamps you should use TimestampsFilter.
To change the number of versions allowed in a column family use the following command:
alter 'test', NAME=>'cf', VERSIONS=>2
then add another entry:
put 'test', 'row1', 'cf:a2', 'value1e'
then see the different versions:
get 'test', 'row1', {COLUMN => 'cf:a2', VERSIONS => 2}
would return something like:
COLUMN CELL
cf:a2 timestamp=1457947804214, value=value1e
cf:a2 timestamp=1457947217039, value=value1d
2 row(s) in 0.0090 seconds
Here is a link for more details:
https://learnhbase.wordpress.com/2013/03/02/hbase-shell-commands/.
The row key 'row3' of cf:c for value4 should be unique otherwise it gets overwritten:
hbase(main):052:0> scan 'mytable' , {COLUMN => 'cf1:1', VERSION => 3}
ROW COLUMN+CELL
1234 column=cf1:1, timestamp=1405796300388, value=hello
1 row(s) in 0.0160 seconds
hbase(main):053:0> put 'mytable', 1234, 'cf1:1', 'wow!'
0 row(s) in 0.1020 seconds
Column 1 of cf1 having a value of 'hello' is overwritten by second put with same row key 1234 and a value of 'wow!'
hbase(main):054:0> scan 'mytable', {COLUMN => 'cf1:1', VERSION => 3}
ROW COLUMN+CELL
1234 column=cf1:1, timestamp=1405831703617, value=wow!
2 row(s) in 0.0310 seconds
Now the second insert contained a new value 'hey' for column 1 of cf1 and the scan query for last 3 versions now shows 'wow!' and 'hey', please not the versions are displayed on descending order.
hbase(main):055:0> put 'mytable', 123, 'cf1:1', 'hey'
hbase(main):004:0> scan 'mytable', {COLUMN => 'cf1:1', VERSION => 3}
ROW COLUMN+CELL
123 column=cf1:1, timestamp=1405831295769, value=hey
1234 column=cf1:1, timestamp=1405831703617, value=wow!

Resources