Change value from one column into another - sql

I have got a table:
ID | Description
--------------------
1.13.1-3 | .1 Hello
1.13.1-3 | .2 World
1.13.1-3 | .3 Text
4.54.1-4 | sthg (.1) Ble
4.54.1-4 | sthg (.2) Bla
4.54.1-4 | aaaa (.3) Qwer
4.54.1-4 | bbbb (.4) Tyuio
And would like to change ending of ID by taking value from second column to have result like:
ID | Description
--------------------
1.13.1 | Hello
1.13.2 | World
1.13.3 | Text
4.54.1 | Ble
4.54.2 | Bla
4.54.3 | Qwer
4.54.4 | Tyuio
Is there any quick way to do it in postgresql?

Use regex to manipulate the strings into what you want:
update mytable set
ID = regexp_replace(ID, '\.[^.]*$', '') || substring(Description from '\.[0-9+]'),
Description = regexp_replace(Description, '.*\.[0-9]+\S* ', '')
See SQLFiddle showing this query working with your data.

Related

Amending the output by adding extra value in SQL

I want to amending the output from SQL table for instance adding extra text or element from the selective table in SQL. And below query is not able to execute as facing mismatch input.
select date, '123' & text from database123
Normal Output
| Date | Text |
| -------- | ------|
| 01/01/2021 | Car |
| 01/02/2021 | Car |
Expecting Output
| Date | Text |
| -------- | ------ |
| 01/01/2021 | 123Car |
| 01/02/2021 | 123Car |
You can use concat or ||:
SELECT concat('123', text), '123' || text
FROM database123

How can Make a selection to filter out the duplicates without grouping them? I want them all to display individually?

I have some data in a database that is sorted into a text column a individual identifier for each text item and a language for each of these text columns.
SELECT Text, Language, COUNT(*)
FROM TableA
WHERE Language = 'English'
GROUP BY Text, Language
HAVING COUNT(*) > 1
This Query gives me a list of the data I need however I have 2 issues, It is grouped up so the results display as:
| Text | Language | Amount Counted |
|------------|----------|-----------------|
| Hello Text | English | 5 |
The issue is I can sort based on the text to make a count however I cannot figure out how to add the unique identifier in there and list these out as one big list? For example The text 'Hello' could be in the list 5 Times and I would get this listed as above. However Each version of hello Will have a Different ID Value Perhaps The first version of Hello is (ID 232) and the Second is (ID 546) how can I add in the ID value which is in the same table and just list all the duplicated with their ID values?
So I would get As a example:
| Text | Language | ID |
|----------------|----------|------|
| Hello Text | English | 232 |
| Hello Text | English | 546 |
| Hello Text | English | 643 |
| Hello Text | English | 745 |
| Hello Text | English | 1353 |
| Other Text | English | 343 |
| Other Text | English | 433 |
| Different Text | English | 433 |
| Different Text | English | 437 |
| Different Text | English | 563 |
| Different Text | English | 898 |
Do you just want a window function?
SELECT text, language, id
FROM (SELECT a.*, COUNT(*) OVER (PARTITION BY Text) as cnt
FROM TableA a
WHERE Language = 'English'
) a
WHERE cnt > 1
ORDER BY id;

How to query multiple rows into a single-line string result?

Let's say we have this table named phrases and it has contents like so:
phrases
+----+--------+
| id | phrase |
+----+--------+
| 1 | the |
| 2 | quick |
| .. | ... |
| 8 | lazy |
| 9 | dog |
+----+--------+
Desired result
+---------------------------------------------+
| sentence |
+---------------------------------------------+
| the quick brown fox jumps over the lazy dog |
+---------------------------------------------+
What should be the query statement such that it would result into a single result string like the above?
You can use STRING_AGG with an empty delimiter, and probably with a sort order
SELECT STRING_AGG(phrase , '' ORDER BY id) as sentence
FROM phrases;

postgres: Multiply column of table A with rows of table B

Fellow SOers,
Currently I am stuck with the following Problem.
Say we have table "data" and table "factor"
"data":
---------------------
| col1 | col2 |
----------------------
| foo | 2 |
| bar | 3 |
----------------------
and table "factor" (the amount of rows is variable)
---------------------
| name | val |
---------------------
| f1 | 7 |
| f2 | 8 |
| f3 | 9 |
| ... | ... |
---------------------
and the following result should look like this:
---------------------------------
| col1 | f1 | f2 | f3 | ...|
---------------------------------
| foo | 14 | 16 | 18 | ...|
| bar | 21 | 24 | 27 | ...|
---------------------------------
So basically I want the column "col2" multiplicated with all the contents of "val" of table "factor" AND the content of column "name" should act as tableheader/columnname for the result.
We are using postgres 9.3 (upgrade to higher version may be possible), so an extended Search resulted in multiple possible solutions: using crosstab (though even with crosstab I was not able to figure this one out), using CTE "With" (preferred, but also no luck). Probably this may also be done with the correct use of array() and unnest().
Hence, any help is appreciated on how to achieve this (the less code, the better)
Tnx in advance!
This package seems to do what you want:
https://github.com/hnsl/colpivot

How can I join two datasets using a key in OpenRefine, with the secondary table having more than one value?

I have a dataset X like this:
Code | Name
------------
123 | AAA
456 | BBB
And the other Y like this:
Code | Level
------------
123 | A
123 | B
456 | B
456 | C
I want to join them using OpenRefine to something like this:
Code | Name | Level A | Level B | Level C
------------------------------------------
123 | AAA | value | value | -
456 | BBB | - | value | value
When I try to add a column using cell.cross() from 'X.Code' it only gets the value from the first appearance of 'X.Code' in 'Y'.
cell.cross("Y", "Code")[0].cells["Rede"].value[0]
How can I get to this desired output, using GREL?
You need Columnize by key/value your project Y to have one column by Level like the example below. Use Transpose -> Columnize by key/value
Code | Level A | Level B | Level C
------------------------------------------
123 | value | value | -
456 | - | value | value
Then you can use the cell.cross function for each column. For example: cell.cross("Y", "Code")[0].cells["Level A"].value[0] to import the data into the Project X