Nattable grid representation for AND & OR operations - eclipse-rcp

I am using Nattable framework to display and author data in grid UI using Eclipse RCP.
I have following expression:
Group1
Value1 X
Value2 X
Value3
x - value is selected.
I have two use cases, system should do logical 'OR' for value1 and value2,
an another use case it should treat as logical 'AND'.
Use case 1 : Value1 && Value2
Use case 2 : Value1 || Value2
How to represent/handle such data/use-cases in better way(UI gesture) in Nattable?

Related

Abap: Select the same field from two db tables into one column of the internal table with one select

I'm new in using SQL in ABAP, so it must be a silly question, but is this somehow possible?:
as I'm new, I cannot add images directly.
Thanks a lot in advance, kind regards
It appears that what you want to do is create an SQL "Union": Select rows from two different database tables and put the results into one combined table. This is only possible if both SELECTs have the same fields. But you can usually accomplish that pretty easily by substituting the missing rows in each table by a constant value ('' in this example):
SELECT
key1
'' as key2
value1
value2
value3
'' as value4
FROM db1
UNION SELECT
key1
key2
value1
'' as value2
'' as value3
value4
FROM db2
INTO CORRESPONDING FIELDS OF TABLE #it.
When you are using an older release (<7.50), then you can combine the data of multiple SELECTs by using APPENDING instead of INTO:
SELECT
key1
value1
value2
value3
FROM db1
INTO CORRESPONDING FIELDS OF TABLE #it.
SELECT
key1
key2
value1
value4
FROM db2
APPENDING CORRESPONDING FIELDS OF TABLE #it.

Change the returned field based on another table value in TSQL

I have a table called Measures which contains result values for a code in different fields depending on the PracticeID. For example:
PracticeID Code Value1 Value2
---------- ---- ------ ------
1 CX H null
2 CX null L
So for PracticeID = 1 the result is in the field Value1 and for PracticeID = 2 the result is in field Value2 for the same code.
If I then create a translation table Measure_Translate which stores which field is to be used for a particular code at a particulare PracticeID. For example:
PracticeID Code ColumnName
---------- ---- ----------
1 CX Value1
2 CX Value2
Is there any way I can use the Measure_Translate table to dynamically determine which field needs to used to return the value for the measure (CX in this case)?
For example:
select m.PracticeID, m.Code,
--then dynamically get which Value field based on what is in the ColumnName
--field in Measure_Translate
from Measure as m
join Measure_Translate as t on t.PracticeID = m.PracticeID and t.Code = m.Code
If you can't change the actual tsql dynamically does anyone know any work around for this?
Thanks in advance
Not sure about how artificial your example data is, but for this specific data no translation table is needed. The coalesce() function can return the first value different from null.
Sample data
create table Measures
(
PracticeId int,
Code nvarchar(2),
Value1 nvarchar(1),
Value2 nvarchar(1)
);
insert into Measures (PracticeId, Code, Value1, Value2) values
(1,'CX','H',Null),
(2,'CX',Null,'L');
Solution
select m.PracticeId,
m.Code,
coalesce(m.Value1, m.Value2) as Value
from Measures m;
Result
PracticeId Code Value
---------- ---- -----
1 CX H
2 CX L
Fiddle
If the sample data was oversimplified and you really need the tranlation, then you could use a case expression.
Sample data extension
insert into Measures (PracticeId, Code, Value1, Value2) values
(3,'CX','H','L');
create table Measure_Translations
(
PracticeId int,
Code nvarchar(2),
ColName nvarchar(20)
);
insert into Measure_Translations (PracticeId, Code, ColName) values
(1,'CX','Value1'),
(2,'CX','Value2'),
(3,'CX','Value2');
Solution
Contains both the coalesce() and the case approach to highlight the difference.
select m.PracticeId,
m.Code,
coalesce(m.Value1, m.Value2) as Value_coalesce,
case mt.ColName
when 'Value1' then m.Value1
when 'Value2' then m.Value2
end as Value_translation
from Measures m
join Measure_Translations mt
on mt.PracticeId = m.PracticeId
and mt.Code = m.Code;
Result
PracticeId Code Value_coalesce Value_translation
---------- ---- -------------- -----------------
1 CX H H
2 CX L L
3 CX H L --> difference
Extended fiddle

Can I construct an update query in postgres where the columns to update are nested in a case?

Is there a way in postgres to update multiple columns with a single case clause? I've only ever seen it done the other way around--that is, multiple case clauses for each column:
update table
set col1 = case when condition then value1 else value2 end
set col2 = case when condition then value3 else value4 end
where ...
But what if I want to update several columns and the condition in the case clause is the same for all of them? Wouldn't it be more consise to have a syntax that allows the case clause to be written once and each column nested inside it? Something like this:
update table
case when condition then
set col1 = value1, col2 = value2, ...
else
set col1 = value3, col2 = value4, ...
end
where ...
Is this possible in postgres?

crystal reports : splitting delimited field to columns

How do I split a delimited field into columns in Crystal Reports XI?
The data in the fields looks like this:
value1 \t value2 \t value3 \r\n
value1 \t value2 \t value3 \r\n
I would like to format it as
Value1 Value2 Value3
I tried putting the fields into Crystal but the tab delimiters are not formatting correctly.
It displays as:
Value1 Value2Value3
Thanks in advance.
You will need three formulas:
//{#one}
Split({table.field}, " \t")[1];
//{#two}
Split({table.field}, " \t")[2];
//{#three}
Split(Split({table.field}, " \t")[3], " ")[1];
This works for Cyrstal Reports 2013 edition as well. I have a column which was separated by ',' and creating multiple formulas worked. Thank you #craig :)
Split({table.field}, ",")[1];

How can I create a column in postgres from values and selections based on other columns?

I want to create a new field (or two) in my table that is a concatenation of other fields, which seems relatively straightforward. But what is the case syntax or if/when syntax I'd use to help create the following fields (GPA_TXT, and newfield)?
The logic is: Each GPA should be #.#, each new field should be:
name & "-" & GPA_TXT & (
case where GPA_TXT > 3.3
set newfield = newfield & 'GradeA',
case where GPA_TXT >2.7 and GPA_TXT < 3.3
set newfield = newfield & "GradeB",
etc...
)
For example:
name major GPA(num) GPA_TXT [newfield]
Bob sci 2 02.0 Bob-sci-GradeC-02.0
Jane chem 3.1 03.1 Jane-chem-GradeB-03.1
Charlie phys 3.7 03.7 Charlie-phys-GradeA-03.7
Garfield food 0 00.0 Garfield-food-GradeF-00.0
So I guess I have two questions in here:
How to create the GPA TXT field.
How to write a case statement to calculate a field according to the values in other fields.
If anyone can link me to a resource with examples or explain I would greatly appreciate it! I'm looking through the documentation but not getting anywhere without examples.
Important note: I would create a view based on your current table and avoided adding new columns, as they will denormalize your schema. Read more here.
Also, I will use lowercase names for all the identifiers to avoid qouting.
to form GPA_TXT field you can use to_char() function: to_char(gpa, 'FM09.0') (the FM will avoid space in front of the resulting string);
for the second field, I would use GPA and not GPA_TXT for numeric comparison. You can check more on CASE construct in the docs, but the block might be the following one:
CASE WHEN gpa >= 3.3 THEN 'A'
WHEN gpa > 2.7 AND gpa < 3.3 THEN 'B'
WHEN gpa > 0 THEN 'C'
ELSE 'F' END
Sorry, I don't know how grades are assigned per GPA, please, adjust accordingly.
The resulting query for the view might be (also on SQL Fiddle):
SELECT name,major,gpa,
to_char(gpa, 'FM09.0') AS gpa_txt,
name||'-'||major||'-Grade'||
CASE WHEN gpa >= 3.3 THEN 'A'
WHEN gpa > 2.7 AND gpa < 3.3 THEN 'B'
WHEN gpa > 0 THEN 'C'
ELSE 'F' END || '-' || to_char(gpa, 'FM09.0') AS adesc
FROM atab;
To build a view just prepend CREATE VIEW aview AS before this query.
EDIT
If you still go for adding columns, the following should do the trick:
ALTER TABLE atab ADD gpa_txt text, ADD adesc text;
UPDATE atab SET
gpa_txt = to_char(gpa, 'FM09.0'),
adesc = name||'-'||major||'-Grade'||
CASE WHEN gpa >= 3.3 THEN 'A'
WHEN gpa > 2.7 AND gpa < 3.3 THEN 'B'
WHEN gpa > 0 THEN 'C'
ELSE 'F' END || '-' || to_char(gpa, 'FM09.0');
I recommend a "generated" column rather than storing the data redundantly. It will take less space on disk, which is likely to actually make it faster than storing the generated value, and will certainly be less prone to accidentally falling out of sync with the base data. Assuming you like the format provided by #vyegorov, You could create a function like this, using the record type of your table (which matches the table name) as input:
CREATE FUNCTION adesc(rec atab)
RETURNS text
IMMUTABLE
LANGUAGE SQL
AS $$
SELECT to_char($1.gpa, 'FM09.0') AS gpa_txt,
$1.name||'-'||$1.major||'-Grade'||
CASE WHEN $1.gpa >= 3.3 THEN 'A'
WHEN $1.gpa > 2.7 AND $1.gpa < 3.3 THEN 'B'
WHEN $1.gpa > 0 THEN 'C'
ELSE 'F' END || '-' || to_char($1.gpa, 'FM09.0') AS adesc;
$$;
You would need to reference this using a relation qualifier (the table name or an alias). When such a reference is not resolved by finding an actual column, PostgreSQL will look for a function taking the table's record type as its only parameter. So you would be able to do something like this:
SELECT name, major, gpa, atab.adesc
FROM atab;
Such a "generated column" can be used in indexes for fast searching, if that's what you're after, with something like adesc(atab).*.
here is query that returns your values from a table with 3 columns:
select *
, to_char(gpa, '09.9') as gpa_text
, name || '-' || major || '-Grade' ||
case when gpa between 3.5 and 4.0 then 'A'
when gpa between 2.5 and 3.4 then 'B'
when gpa between 1.5 and 2.4 then 'C'
when gpa between 0.5 and 1.4 then 'D'
else 'F' end
|| '-' || ltrim(to_char(gpa, '09.9')) as newfield
from students
This is working code, here is the newfield for Bob, "Bob-sci-GradeC-02.0"
I would strongly suggest that you do not have a text column in a database to hold a duplicate of a numeric value. I'm not quite sure why I need the ltrim, it seems odd that the formatted string would have a leading blank.