How to derive a column based on two different merged dimensions in SAP Business Objects? - merge

I have two tables, and I want to derive one column from Table1 to Table2 by common fields Name and Color (by merging).
But until I was using only one merged dimension and it was working well for me.
Now, for two dimensions, it is not working.
If I have two tables :
Table1 Name : Fruits
|--------|--------|---------------|
| Name | Color | Rateperunit |
|--------|--------|---------------|
| banana | yellow | 3 |
|--------|--------|---------------|
| apple | red | 25 |
|--------|--------|---------------|
| apple | green | 30 |
|--------|--------|---------------|
Table2 Name : Purchase
|--------|---------|-------------|-----------|----------|
| Item | Color | Rateperunit | Noofitems | Totalbill|
|--------|---------|-------------|-----------|----------|
| apple | green |30 | 3 | 90 |
|--------|---------|-------------|-----------|----------|

Related

Flatten Postgers left join query result with dynamic values into one row

I have two tables products and product_attributs. One Product can have one or many attributs and these are filled by a dynamic web form (name and value inputs) added by the user as needed. For example for a drill the user could decide to add two attributs : color=blue and power=100 watts. For another product it could be 3 or more different attribus and for another it could have no special attributs.
products
| id | name | identifier | identifier_type | active
| ----------|--------------|-------------|------------------|---
| 1 | Drill | AD44 | barcode | true
| 2 | Polisher | AP211C | barcode | true
| 3 | Jackhammer | AJ2133 | barcode | false
| 4 | Screwdriver | AS4778 | RFID | true
product_attributs
|id | name | value | product_id
|----------|--------------|-------------|----------
|1 | color | blue | 1
|2 | power | 100 watts | 1
|3 | size | 40 cm | 2
|4 | energy | electrical | 3
|4 | price | 35€ | 3
so attributs could be anything which are set dynamically by the user. My need is to generate a report on CSV which contain all products with their attributs. Without a good experience in SQL I generated the following basic request :
SELECT pr.name, pr.identifier_type, pr.identifier, pr.active, att.name, att.value
FROM products as pr
LEFT JOIN product_attributs att ON pr.id = att.product_id
as you know the result will contain for the same product as many rows as attributs it has and this is not ideal for reporting. The ideal would be this :
|name | identifier_type | identifier | active | name | value | name | value
|-----------|-----------------|------------|--------|--------|-------|------ |------
|Drill | barcode | AD44 | true | color | blue | power | 100 w
|Polisher | barcode | AP211C | true | size | 40 cm | null | null
|Jackhammer | barcode | AJ2133 | true | energy | elect | price | 35 €
|Screwdriver| barcode | AS4778 | true | null | null | null | null
here I only showed a max of two attributes per product but it could be more if needed. Well I did some research and came across the pivot with crosstab function on Postgres but the problem it requests static values but this does not match my need.
thanks lot for your help and sorry for duplicates if any.
Thanks Laurenz Albe for your help. array_agg solved my problem. Here is the query if someone may be interested in :
SELECT
pr.name, pr.description, pr.identifier_type, pr.identifier,
pr.internal_identifier, pr.active,
ARRAY_TO_STRING(ARRAY_AGG (oa.name || ' = ' || oa.value),', ') attributs
FROM
products pr
LEFT JOIN product_attributs oa ON pr.id = oa.product_id
GROUP BY
pr.name, pr.description, pr.identifier_type, pr.identifier,
pr.internal_identifier, pr.active
ORDER BY
pr.name;

Create a new DF using another two

I have two dataframes that shares the column colour, and I would like to create a new column with the Code that correspond to the column colour in a new DF as you can see:
DF1
+------------+--------------------+
| Code | colour |
+------------+--------------------+
| 1001 | brown |
| 1201 | black |
| 1300 | green |
+------------+--------------------+
DF2
+------------+--------------------+-----------+
| Name | colour | date |
+------------+--------------------+-----------+
| Joee | brown | 20210101 |
| Jess | black | 20210101 |
| James | green | 20210101 |
+------------+--------------------+-----------+
Output:
+------------+--------------------+-----------+----------+
| Name | colour | date | Got |
+------------+--------------------+-----------+----------+
| Joee | black | 20210101 | 1201 |
| Jess | brown | 20210101 | 1001 |
| James | blue | 20210101 | 092 |
+------------+--------------------+-----------+----------+
How can I do this? With join?
As mck suggested, a simple SQL join would be enough for your case, by explicitly specifying the equality of the colour column's values between the two DataFrames, as seen below (we drop one of the two colour columns since they have the same value for each row after the join):
val joined = df1.join(df2, df1("colour").equalTo(df2("colour")))
.drop(df1("colour"))
This is what we get after showing the newly formed joined DataFrame:
+----+-----+------+--------+
|code| name|colour| date|
+----+-----+------+--------+
|1001| Joe| brown|20210101|
|1201| Jess| black|20210101|
|1300|James| green|20210101|
+----+-----+------+--------+

Postgres: Extracting specific words from free text column

I have a table below
| id | description |
+----+--------------------------------+
| 1 | Added: Apple, Grape and Orange |
| 2 | Orange |
| 3 | Removed: Plum and Grape |
Then, I have another table that contains the list of fruits
| id | name |
+----+--------+
| 1 | apple |
| 2 | orange |
| 3 | plum |
With these two tables, is it possible to extract every word from the first table on description column that matches all the fruit names?
I want the output like this
| id | description | word |
+----+--------------------------------+--------+
| 1 | Added: Apple, Grape and Orange | Apple |
| 1 | Added: Apple, Grape and Orange | Orange |
| 2 | Orange | Orange |
| 3 | Removed: Plum and Grape | Plum |
Thank you.
That would be a simple inner join with pattern matching as join condition:
SELECT mytable.id,
mytable.description,
fruit.name AS word
FROM mytable
JOIN fruit ON mytable.description ILIKE '%' || fruit.name || '%';
If both tables are large, that will be pretty slow, since it requires a nested loop join. You could use a trigram index on mytable(description) to speed up the query.

How to present data in a tree like table in UI5

I have data which is expected to be displayed in a structured table. It is expected that the table can be opened and collapsed on two levels allowing
you to drill down in the data.
Example: Cities World Wide
When the table is loaded it should be displayed like this:
Area | Country | Name | Population | ...
--------------+----------------+-------------+-------------+----
Europe | | | |
North America | | | |
... | | | |
when I click on "Europe" the second level is shown:
Area | Country | Name | Population | ...
--------------+----------------+-------------+-------------+-----
Europe | | | |
| Germany | | |
| Czech Republic | | |
North America | | | |
... | | | |
When I now click on "Germany" the third level containing the actual city data is shown:
Area | Country | Name | Population | ...
--------------+----------------+-------------+-------------+-----
Europe | | | |
| Germany | | |
| | Berlin | 4 Million | ...
| | Leipzig | 0.5 Million | ...
| Czech Republic | | |
North America | | | |
... | | | |
How can I achieve a drill down in a tree like structure like shown in this example in UI5?
Since one of the requirement is to also to see and compare the data at once I believe a table control would be best but I don't know how to bring in this drill down behavior.
Mobile support is not required. The person using this will be a data analyst working on a big screen.
Any help appreciated.
While writing this question I came up with the search phrase "ui5 tree table" and voila:
https://experience.sap.com/fiori-design-web/ui-components/tree-table/
This is what I was looking for.

Tableau to create single chart from multiple parameters

I have tableau workbook online
Before, I had filter for single Principal, and applied to all CUSIPs, and I was able to plot all the inflation-adjusted principals based on Index ratios for a particular date, (refer tab Inflation-Adjusted Trend) i.e.
Now, I have multiple filters based on multiple Principals, i.e. buy one CUSIP for $1500, buy another for $900, etc (refer tab Infl-Adjusted Trend 2)
These were the columns and rows
But I do not like the format of this graph.
I wish to have all the lines together in one graph, just like the single-principal tab below ..... how to fix this? How to bring all the values into one chart?
You currently have six calculated fields calculating your inflation-adjusted principals, one for each CUSIP. Here's what that table might end up looking like:
+-----------+-------------+-------------+-------------+-------------+-----+
| CUSIP | 912828H45 P | 912828NM8 P | 912828PP9 P | 912828QV5 P | ... |
+-----------+-------------+-------------+-------------+-------------+-----+
| 912828H45 | $100 | NULL | NULL | NULL | ... |
| 912828NM8 | NULL | $455 | NULL | NULL | ... |
| 912828PP9 | NULL | NULL | $132 | NULL | ... |
| 912828QV5 | NULL | NULL | NULL | $553 | ... |
| ... | ... | ... | ... | ... | ... |
+-----------+-------------+-------------+-------------+-------------+-----|
There's definitely a better way. Your fields are set up like this:
IF [Cusip] = "912828H45"
THEN
[912828H45 Principal] * [Index Ratio]
END
Instead of setting up one field per CUSIP, make a single field that calculates that value for each CUSIP.
IF [Cusip] = "912828H45"
THEN
[912828H45 Principal] * [Index Ratio]
ELSEIF [Cusip] = "912828NM8"
THEN
[912828NM8 Principal] * [Index Ratio]
...
END
Now your table looks like this.
+-----------+------------------------------+-----+
| CUSIP | Inflation-Adjusted Principal | ... |
+-----------+------------------------------+-----+
| 912828H45 | $100 | ... |
| 912828NM8 | $455 | ... |
| 912828PP9 | $132 | ... |
| 912828QV5 | $553 | ... |
| ... | ... | ... |
+-----------+------------------------------+-----+
That's a LOT easier to work with. Drag that single field into Rows and color by [Cusip].