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",
Related
I have the following situation:
column 1 column 2 column 3
A 1 1
A 1 2
B 3 4
B 3 5
I need to color my letters when the value in column 2 never occurs in column 3. But I need to color all my letters. Does anyone know how to write a case statement for this?
So I'll explain my example: I dont't need to color the letter A because there is a match between column 2 and 3 and the first row.
I do need to color my B's because there is never a match between columns 2 and 3.
I already tried this:
count(distinct(case when "Column 2" != "Column 3" then 1 else 0 end))
but this gives a result for each row and I need a result for the total package.
Thanks!
You can approach this as following:
Create a logical column on your analysis that does a case statement that returns 1 or 0 depending if the values of column2 and column3 are the same (pretty much like the case-when that you provided on your answer but without the count distinct).
Wrap that case statement with a MAX grouped by your column1. This will give you either a consistent 1 or 0 across all your different values of column1. You can use this value for your conditional formatting. The key here is to use the aggregated function with the group by.
You have here some oracle documentation on how to use the logical SQL group by.
Hope that helps! Good luck!
I have a table with 40 lac rows with a column 'playcount' which has min value 1 and max value of around 17,000.
I would like to split this table into 15 groups by adding a column which will have values 1 to 15 based on 'playcount' column.
Hive has a function NTILE which allows to do something similar. Here if I did NTILE(15) OVER (ORDER BY playcount) AS mygroup, it does break it up but based on count of playcount values and as the lower values are lot lot more( more than 50% have values less than 5), the grouping is such that values over 35 have group value of 15(maximum).
I would like to do the grouping based on the playcount and not on count of playcount values.
Is something similar possible in hive.
Thanks
One possibility I can think of is playcount%15 as mygroup.
I am trying to create a 360 questionnaire dynamically using a classic report in Oracle Apex. Got the first part to work nicely using the following:
SELECT q.display_text,
apex_item.radiogroup(rownum, 1, a.answer, null, null, null, null) "ineffective",
apex_item.radiogroup(rownum, 2, a.answer, null, null, null, null) "sometimes"
FROM xxpay_360_questions q,
xxpay_360_answers a
where a.question_id (+) = q.question_id
and a.user_name (+) = :APP_USER
order by q.questionnaire_id,
q.display_sequence
This outputs 3 report columns. The first one is the question and the second two are the horizontal radio buttons to select answer 1 or 2. The 360 questionnaire also needs sections and sub sections and some textarea questions. For those I would like to merge the 3 report columns into 1 column (akin to colspan=1). I would probably need to output them using a union in the above select, but I'm not sure how to dynamically output a colspan and a single report column value.
Note that I am using theme 20 in order to get the Oracle Applications look and this uses table layout.
Anyone know how to output a single report column instead of 3 for some rows and then colspan=1 it? Changing the font for the section and sub-section would be a bonus.
Not sure whether css can do colspan when using table layout.
Add a column to your report query that will serve as a flag for the
3 in 1 column rows.
Create a new report template, make it a "named column" style.
Create the two different column formats you want using the #COLUMN_NAME# token for columns.
Set the condition for each of these two formats using the value of your new flag column.
The added benefit to this is you can now use HTML to do whatever formatting you end up needing later.
I have made following.
Test table:
create table tst as
select 1 a, 2 b, 3 c, 4 d, 5 e from dual
union all
select 11, 12, 13, null, null from dual
union all
select 21, 22, 23, 24, 25 from dual;
Region source:
select a, b,
case when d is null and e is null then
'<td colspan="3">' || c || '</td>'
else '<td>' || c || '</td><td>' || d || '</td><td>' || e || '</td>'
end merged_column
from tst
Report properties: Display as - Standard Display Column, Heading of a column merged_column:
<th>C</th><th>D</th><th>E</th>
Result looks like this:
Maybe it is not so cool and useful example, but cells in second row look merged (and they are really merged, of course). Also it is impossible to sort by columns 4 and 5, and you need manually align text there.
Sorry, can't give a link to the page, apex.oracle.com upgraded to version 5.0, version 4.2 is unavailable now.
I wish I had more time to work up a proper example, but you can use Oracle's LISTAGG function to group the answers into a single row per question and add some HTML tags for styling. Generally speaking, I generate something like this:
<SPAN TITLE="Some help text">Some question text?</SPAN>
<UL>
<LI>[RADIO group1 value1] radio_label1</LI>
<LI>[RADIO group1 value2] radio_label2</LI>
</UL>
Hopefully, you can use this example as a starting point to coding what you want.
As an aside, I should point out one potential issue with what you are trying to do. You may already be aware of it but, via this method, Apex is limited to displaying no more than 50 questions at a time because the value for p_idx has to be a whole number between 1 and 50. (Source: Apex documentation) You can work within that limitation, but being aware of the issue from the start is much easier than discovering it half-way through.
Good Luck!
I am using PL SQL developer and Oracle 11g. Here is my issue:
EDIT
It looks like using rank (partition by header number, line number order by date) works.
I would like to create a counter that changes each time a column has a different value than the last. I need to be able to do this in a view (non-modifiable constraint). The goal would be to use this counter to build a new unique key into that view. Thus, I need to reset the 'counter' each time a line number changes in the planning table (see below).
I have 3 tables. Under each table I have put some important columns:
Header
--headerNumber (unique)
Detail
--headerNumber (not unique)
--lineNumber (unique)
Planning lines
--headerNumber (not unique)
--lineNumber (not unique)
--some date
Into the planning lines table, there exist multiple lines, per line number as well. I want to build into a view (this is a constraint, it must be a view) the ability to build a unique number off of the line number from planning.
Here is some example data:
Header (1 row):
Header Number = 1
Detail (2 rows)
Header Number = 1, LineNumber = 1
Header Number = 1, LineNumber = 2
Planning (4 rows)
Header Number = 1, Line number = 1, date = 01/01/14
Header Number = 1, Line number = 1, date = 01/02/14
Header Number = 1, Line Number = 2, date = 01/01/14
Header Number = 1, Line Number = 2, date = 01/03/14
Into the view I want it to look like this:
HeaderNumber | Line Number | 'Counter' (What I am trying to create)
1 | 1 | 1
1 | 1 | 2
1 | 2 | 1
1 | 2 | 2
Here are some final issues I have faced:
It does not appear as if I can use row number -- The view will contain multiple headers, and more importantly, row number seems to be built out of the entire select's contents -- thus I can't trim it. Row number ends up counting 1,2,3,4 (row above).
Does anyone have an idea about how to build this into a select (to put into a view)? I know this can be done by a procedure but I really need a view.
Regards,
Seriously Confused Man
I'm not sure what you have tried exactly when you say "row number" - do you mean ROWNUM or row_number()?
Doesn't this do what you want?
select headernumber, linenumber,
row_number() over (partition by headernumber order by linenumber)
as counter
from ...;
In my table I have essentially 2 columns (many more but there is an obvious left side and right side). One of the fields, FIELD1, on the left side comes from a LookupSet() and each ID can have two items from FIELD1. Using a join(lookupset(), vbcrlf) I am able to get both values for the ID and put it into one cell in the table. This works but with the vbcrlf, the row height is increased. This causes a problem because there is data on the right side which cannot have additional space between it.
I did a split(join(lookupset())).getValue(0) for the first row and then the row below it for value 1. With some iif statements to check errors etc, this works.
One problem I solved is that the values for FIELD1 can be longer than the width of the cell, but will not take up more than two. I was able to do some substring magic in oracle like:
SELECT ID, SUBSTR(FIELD1, 1, 70) FROM....
UNION
SELECT ID, SUBSTR(FIELD1, 70) FROM ...
using sorting, I am able to get up to 4 rows of data per ID which I will be able to split the lookupset and get each value into the table.
My last problem, and hopefully someone can help with is that when I ge the substring, It can cut off words and the next row will start with the rest of the word.
Is there a way, possibly using a regex to make sure to keep words in tact, but also to ensure that the total length returned is < some number of characters? I am happy to abandon any part of the approach i am currently taking if I am off track.
I was able to solve this (with some help) by using
SUBSTR(FIELD1,1, regexp_instr(FIELD1, '[ ]', 70))
SELECT ID, SUBSTR(FIELD1,1, regexp_instr(FIELD1, '[ ]', 70)) FROM....
UNION
SELECT ID, SUBSTR(FIELD1, regexp_instr(FIELD1, '[ ]', 70)) FROM ...