I have a field [values] where a range of values (say A - E) can be added. Viewed across a number of records we get something like
Record 1 [values] A
Record 2 [values]
Record 3 [values] A
Record 4 [values] C
Record 5 [values] B
Is there a way of producing a summary field of [values] where the above would return :
A
B
C
my thanks in advance
You may be able to do what you want with a Value List:
Make a new value list labeled 'Values' (File Menu > Manage > Value Lists)
Make the value list 'Use Values From Field' and specify your Values field
Create a new Calculation field, 'Listed Values'
Set the calculation to type 'Text' and with the following code:
ValueListItems ( Get(FileName) ; "Values" )
You will likely want to turn on "Do Not Store Calculation Results" for the Listed Values field.
Related
I am trying to reorder the Bars in bar chart in qliksense. Below is the order which I have to sort the bars.
NEW
IN_PROGRESS_PL
ASSIGNED
IN_PROGRESS_SPOC
RESOLVED/CLOSED
FBS E2S BACKLOG
CANCELLED
DUPLICATE
But by default the bars are ordered in alphabetical order or by the numbers, I have tried the below solution, but it still didn't rearranged the bars.
Default order :
Match('NEW','IN_PROGRESS_PL','ASSIGNED','IN_PROGRESS_SPOC','RESOLVED/CLOSED','FBS E2S BACKLOG' , 'CANCELLED','DUPLICATE' )
Can someone please help me to fix this issue.
The first parameter in Match is the targeted field.
If we have data like:
RawData:
Load * inline [
Stage , IdeasCount
ASSIGNED , 63
CANCELLED , 11
INTERNAL_123, 2
IN_PROGRESS1, 20
IN_PROGRESS2, 47
];
Then the sorting expression will be:
=match(Stage, 'IN_PROGRESS1', 'ASSIGNED', 'IN_PROGRESS2', 'INTERNAL_123', 'CANCELLED')
The sorting properties:
And the result chart will have the correct sorting:
P.S. (1)
Have to mention that if the value is not found in the Match function then the function will return null and thus the corresponding bar will be pushed in the front. In this case you can wrap the match in if statement and if no matching value is found then assign some large number:
= if( match(Field, 'value1', 'value2' ...) > 0,
match(Field, 'value1', 'value2' ...),
1000
)
P.S. (2)
In your case you can also use Dual function to create "default" sort order for a specific field.
If we change the script to:
RawData:
Load
Dual(Stage, OrderId) as Stage,
IdeasCount
;
Load * inline [
Stage , IdeasCount, OrderId
ASSIGNED , 63 , 2
CANCELLED , 11 , 5
INTERNAL_123, 2 , 4
IN_PROGRESS1, 20 , 1
IN_PROGRESS2, 47 , 3
]
;
The result table will look the same - with two fields only Stage and IdeasCount. But in the background each value of Stage field will have and number representation (based on the initial OrderId field). And as a side effect of that the auto sorting option will sort the field data by its internal number representation and the chart will "sort itself" correctly
/*
I have some records I need to filter through. The 2nd field must match the first field of the next record and the max hier_record_starting must be the final record. so I need a query that pulls records 1,2,4,6 and I need the row_number to be dynamic. Any help would be appreciated as I'm having trouble conceptualizing this one
*/
create temp table data (
hier_record_starting date
,hier_record_ending date
,row_number smallint
)
;
insert into data(hier_record_starting,hier_record_ending,row_number)
values('2013-09-16','2013-10-08','1') -- 09/16/13 thru 10/08/13
,('2013-10-08','2021-10-31','2') -- 10/08/13 thru 10/31/21
,('2021-10-31','2021-11-27','3') -- invalid as 2nd field value does not match 1st field value of next record
,('2021-10-31','2021-12-25','4') -- 10/31/21 thru 12/25/21
,('2021-11-27','9999-01-01','5') -- invalid as 2nd field value does not match 1st field value of next record
,('2021-12-25','9999-01-01','6') -- 12/25/21 thru 01/01/99
I have jsonb data as
{
"a":[
{"b":1,"c":2,"d":3},
{"b":4,"c":5,"d":6}
],
"g":[
{"b":1,"c":2,"d":3},
{"b":4,"c":5,"d":6}
]
}
I want to remove c key from nested array in both "a" and "g" keys. Is there a single query to perform this?
step-by-step demo: db<>fiddle
SELECT
jsonb_object_agg(key, a) -- 5
FROM (
SELECT
mydata,
key,
jsonb_agg(a_elems.value - 'c') as a -- 3/4
FROM
mytable,
jsonb_each(mydata) elems, -- 1
jsonb_array_elements(elems.value) AS a_elems -- 2
GROUP BY mydata, key -- 4
) s
GROUP BY mydata -- 5
Expand the JSON elements into one row each. This generates two columns: One for the key and one for the value (the JSON array)
Expand the JSON array into one row each (which separates the aggregated JSON object from which you want to remove the c element)
You can use the - operator to remove the element.
To reaggregate the original JSON object, you need to group it backwards. jsonb_agg() reaggregates the arrays
Finally you need to rebuild the original JSON object with jsonb_object_agg() using the previously generated key column and the new array column.
I am trying to bulk update some rows in postgres. Now not all of the rows need to update the same column values. For example, row 1 needs to update column 1 and 3 whereas row 2 needs to update column 2 and 4. so row 1 column 2 and 4 should not change and row 2 column 1 and 3 should not change.
I have tried using CASEs to conditionally SET the correct column values but it doesn't work with multiple rows. It DOES work if I only try to update 1 row at a time.
update topic as tmp set
"from" = (CASE WHEN tmp2."from2"::text = 'OLD_VALUE' THEN tmp."from"::int2 ELSE tmp2."from2"::int2 end),
"text_search" = (CASE WHEN tmp2."text_search2"::text = 'OLD_VALUE' THEN tmp."text_search"::text ELSE tmp2."text_search2"::text end),
"weight" = (CASE WHEN tmp2."weight2"::text = 'OLD_VALUE' THEN tmp."weight"::numeric ELSE tmp2."weight2"::numeric end)
from (values
(1051,1,'Electronic Devices',3),
(1052,'OLD_VALUE','OLD_VALUE',100)
) as tmp2("id2","from2","text_search2","weight2")
where tmp2."id2" = tmp."id"
This is the error message i get
SQL Error [22P02]: ERROR: invalid input syntax for type integer: "OLD_VALUE"
When I try with only 1 FROM value
from (values (1051,1,'Electronic Devices',3))
or
from (values (1052,'OLD_VALUE','OLD_VALUE',100))
it works correctly.
It even works correctly if the same columns need to be updated eg.
from (values
(1051,1,'Electronic Devices',3),
(1052,2,'Topic 2',100)
)
Why is it not working correctly when I need to update different columns for each row?
When you provide the list of values as values (1051,1,'Electronic Devices',3),(1052,'OLD_VALUE','OLD_VALUE',100)), the first set of values is interpreted as the "template" of data types, and in this case (1051,1,'Electronic Devices',3), it's int, int, text, int. Then, any subsequent values provided, will be expected to have the same data type signature. When it parses (1052,'OLD_VALUE','OLD_VALUE',100), it sees int,text,text,int, which doesn't match the data type signature it expects, so it reports an error.
When you omit the first value and provide only (1052,'OLD_VALUE','OLD_VALUE',100), then it identifies int,text,text,int as the "template" data type signature, and it proceeds without complaint.
How can I write a calculation field in a table that outputs '1' if there are other (related) records in the same table that meet a given set of criteria and '0' otherwise?
Here's my problem explained in more detail:
I have a table containing 'students' and another containing 'exam results'. The 'exam results' table looks like this:
StudentID SubjectID Level Result
3234 1 2 A-
3234 2 4 B+
4739 1 4 C+
A student can only pass a Level 4 exam in subject 2 if they have also passed a Level 2 exam in subject 1 with a B+ or higher. I want to define a field in the 'students' table that contains a '1' if there exists an exam result belonging to the right student that meets these criteria and a '0' otherwise.
What would be the best way to do this?
Let us take an example of a Results table where the results are also calculated as a numeric value, e.g.
StudentID SubjectID Level Result cResultNum
3234 1 2 A- 95
3234 2 4 B+ 85
4739 1 4 C+ 75
and an Exams table with the following fields (among others):
RequiredSubjectID
RequiredLevel
RequiredResultNum
Given these, you can construct a relationship between Exams and (another occurrence of) Results as:
Exams::RequiredSubjectID = Results 2::SubjectID
AND
Exams::RequiredLevel = Results 2::Level
AND
Exams::RequiredResultNum ≤ Results 2::cResultNum
This allows each exam record to calculate a list of students that are eligible to take that exam as =
List ( Results 2::StudentID )
I want to define a field in the 'students' table that contains a '1'
if there exists an exam result belonging to the right student that
meets these criteria and a '0' otherwise.
This request is unclear, because there are many exams a student may want to take, and a field in the Students table can calculate only one result.
You need to do a self-join in the table for the field you want to check, for example:
Exam::Level = Exam2::Level
Exam::Student = Exam2::Student
And for the "was passed" criteria I think you could do an "If" on the calculation like this:
If ( Last(Exam2::Result) = "D" and ...(all the pass values) ; 1 ; 0 )
Edit:
It could be just with the not pass value hehe I miss that it will be like this:
If ( Last(Exam2::Result) = "F" ; 0 ; 1 )
I hope this helps you.