Can you interleave multiple values when joining columns in OpenRefine? - openrefine

I have a table of two columns where each cell has multiple values separated by a new-line. The values across the two columns are aligned. I would like to merge the values of the two columns in to a new column where the values of the first lines are merged before the values of the second line.
Something like this:
| Column 1 | Column 2 | New Column |
|----------|----------|------------|
|A |B |A (B) |
|C |D |C (D) |
|----------|----------|------------|
|E |F |E (F) |
|G |H |G (H) |
I tried both joining and adding a column based on values from columns but I can only merge in order so it ends up like this:
| Column 1 | Column 2 | New Column |
|----------|----------|------------|
|A |B |A |
|C |D |C |
| | |B |
| | |D |
|----------|----------|------------|
|E |F |E |
|G |H |G |
| | |F |
| | |H |
|----------|----------|------------|
I can get a manual individual answer like this:
cells["Column 1"].value.split("\n")[0] + " (" + cells["Column 2"].value.split("\n")[0] + ")"
Is there a way to iterate though the arrays? I have tried using forEach but only get errors.

You can use records and cell splitting for the required transformation:
Add new column "Index" via "Edit column => Add column based on this column..." and the GREL expression rowIndex. We need this column to restore the original rows after modifying them.
Move the new column "Index" to the beginning via "Edit column => Move column to beginning".
Change into records mode.
On "Column 1" split the cells on whitespace via "Edit column => Split multi-valued cells" using \n as separator and activating the "regular expression" checkbox.
Repeat the last step on "Column 2".
Add the new column using the expression row.cells["Column 1"].value + " (" + row.cells["Column 2"].value + ")"
Undo the cell splitting on via "Edit cells => Join multi-valued cells".
Note that if you use \n it will literally insert "\n" as separator.
So I usually use a custom separator like | and then replace it
with \n in a separate step.
Delete remaining columns (like "Index").

Related

Returning rows that contain text from another table

I have two tables Main and Lookup
Main has a column called [Promotion Codes List] that is a string of different promo codes comma separated.
Lookup has a column of all the individual promotion codes called [Promotion Codes].
I am trying to create a view of the Main table that will filter Main table if if Main.[Promotion Codes List] contains a code that is in the Lookup.[Promotion Codes] table.
Main Table sample data
|id|Promotion Codes List|
|--| ------------------ |
|1 | BR-Coll-210731-10903, BR-NFRD-210731-10124 |
|2 | BR-Coll-210731-10903, BR-Deal-210731-11265 |
|3 | BR-Coll-210731-11037, BR-Deal-210731-11275 |
|4 | BR-Coll-210731-11037 |
Lookup Table sample data
|id|Promotion Codes|
|--| ------------- |
|1 | BR-NFRI-251231-04855 |
|2 | BR-Deal-210731-11265 |
|3 | BR-Coll-210731-11402 |
|4 | BR-Deal-210731-11275 |
|5 | BR-Coll-210731-11037 |
Desired output
Main Table
|id|Promotion Codes List|
|--| ------------------ |
|2 | BR-Coll-210731-10903, BR-Deal-210731-11265 |
|3 | BR-Coll-210731-11037, BR-Deal-210731-11275 |
|4 | BR-Coll-210731-11037 |
I started off playing around with
SELECT Promotion Codes List, CASE WHEN EXISTS (SELECT *
FROM dbo.[Lookup] B
WHERE B.[Promotion Codes] = A.Promotion Codes List) THEN 'common' ELSE 'not common' END AS valid FROM dbo.Main AS A
and it works if there is are exact matches in row, but as you can see in the main table there can be multiple br codes in one record so I tried to add a like statement somewhere but couldn't figure it out.
Thanks !
Try below
SELECT Promotion Codes List, CASE WHEN EXISTS (SELECT 1
FROM dbo.[Lookup] B
WHERE A.[Promotion Codes List] like '%'+B.[Promotion Codes]+'%') THEN 'common' ELSE 'not common' END AS valid FROM dbo.Main AS A

SQL triggers - conditionals and math operations

I have some database, lets say like that:
|a |b |c |d |e |
----------------------------------
|0 |12 |NA | | |
|NA|NA |30 | 42 | |
|NA|NA |NA | |53 |
I'd like to do few things:
for first row - set value to column D to be sin(b)
for second row - if the value is NA for column B, copy the previous known value (i.e (a,b)=(0,12))
on third row - if the value is NA for column B , copy the previous known value (i.e (a,b)=(0,12)) and set value to column D to be sin(b).
Those three examples of activities I have to execute on my databases for each row.
If triggers are not the right solution for this, I'd like to have recommendation how to solve it.

How to merge columns of non unique rows in a database? (Sybase ASE)

Consider the data as:
|Column 1|Column 2|Column 3|
----------------------------
|A |Tom |1 |
|A |Tom |2 |
|B |Ron |3 |
There are few duplicates in Column 1 that are preventing me to create an index. I need to only create an index on Col 1.
How do I merge/flatten the values to get something like:
|Column 1|Column 2|Column 3|
----------------------------
|A |Tom |1,2 |
|B |Ron |3 |
How do we do this without using concatenate/LIST/STUFF? The database is Sybase ASE.
You'll have to write a loop to do this. But if you only want to create that index, why not create it as non-unique?
If you have to create it as unique, just add an identity column to the table and create the index on column1 + the identity column (or use the auto-identity DBoption)

How to Merge 2 Rows into one by comma separate?

I need to merge this individual rows to one column, I now how to merge column by comma separated,
+---------------+-------+-------+
|CID |Flag |Value |
+---------------+-------+-------+
|1 |F |10 |
|1 |N |20 |
|2 |F |12 |
|2 |N |23 |
|2 |F |14 |
|3 |N |21 |
|3 |N |22 |
+---------------+-------+-------+
Desired Result can be anything,
+-----------+----------------------------+ +--------------------------+
|Part Number| Value | | Value |
+-----------+----------------------------+ +--------------------------+
| 1 | 1|F|10 ; 1|N|20 | Or | 1|F|10 ; 1|N|20 |
| 2 | 2|F|12 ; 2|N|23 ; 2|F|14 | | 2|F|12 ; 2|N|23 ; 2|F|14 |
| 3 | 3|N|21 ; 3|N|22 | | 3|N|21 ; 3|N|22 |
+-----------+----------------------------+ +--------------------------+
Note:
Any hint in right direction with small example is more than enough
EDIT :
I have massive data in tables like thousands of records where parent's and child relationship is present. I have to dump this into text files by comma separated values In single line as record. Think as primary record has relationship with so many other table then all this record has to be printed as a big line.
And I am trying to achieve by creating query so load can be distributed on database and only thing i have to worry about in business is just dumping logic into text files or whatever form we need in future.
You can try to use LISTAGG and your query will look like this:
select a.cid, a.cid || listagg(a.flag || '|' || a.value, ',')
from foo.dat a
group by a.cid
You can use different separators and of course play with how the result will be formatted.

Crystal Report SUM function with CASE

I have the following column values in my crystal report:
|Code |Unit |
|A |2 |
|B |3 |
|C |2 |
|D |3 |
|E |1 |
|F |1 |
|G |4 |
|H |(3) |
I want to summarize the Unit except the Units which has Code H,J,K, and L.
The Codes: H,J,K, and L contains units which has parenthesis.
Is there a way to do this?
If you want to exclude any row or value from summary, it can be done by writing your case inside Use a formula field under Evaluate in Running Total Field
Refer the following Image...
The rows or fields which doesn't satisfy the condition will be skipped from Evaluation of summary.
Try this and get back with results !!
If you want to omit only units with ‘(‘ in it just convert this filed to number
Use
Val ({Unit})
this will return 0 for non-numeric text and number for numeric create sum of these you will get what you want
If you want not to use any special then create formula field like this
if {fa_rep_vr_Main.CustomTitle} not in('A','B','C') then
0
else
val({Unit})
use sum of this
If you want sum of NewPriceAD then use it in mentained field