SQL Update table with all corresponding data from another table concatenated? - sql

Running PostgreSQL 12.4. I am trying to accomplish this but the syntax given there doesn't seem to be working on psql, and I could not find another approach.
I have the following data:
Table 1
ID Trait
1 X
1 Y
1 Z
2 A
2 B
Table 2
ID Traits, Listed
1
2
3
4
I would like to create the following result:
Table 2
ID Traits, Listed
1 X + Y + Z
2 A + B
Concatenating with + would be ideal, as some traits have inherent commas.
Thank you for any help!

Try something like:
update table2
SET traits = agg.t
FROM
(select id,string_agg(trait, ',') t from table1 group by id) agg
where
table2.id = agg.id;
dbfiddle
Concatenating with + would be ideal, as some traits have inherent commas.
You can use whatever delimiter you like (just change the second argument to string_agg).

Related

Filter by one column then count unique value in another column in SQL

I would like to filter data by column Base =1 and then count the number of unique values in another column 'Animal' in SQL, data:
Animal Base Value
1 A 1 X
2 B 1 X
3 A 2 Y
4 A 3 V
Expected output in this case is 2 from the first two rows.
Simpler than you may have thought:
SELECT count(DISTINCT Animal)
FROM tbl
WHERE Base = 1;
Should work in any halfway decent RDBMS including your undisclosed one. (You may have to enclose column names in double-quotes.)
This should do it, assuming the table is named animals:
select count(*) from (select distinct Animal from animals where Base=1) tb1;

How to Take Average of Data Within a Column to create new Variable

I have a simple task which I cannot wrap my head around being a novice coder.
I have a data set which I am trying to manipulate.
It appears as this:
UniqueID Day Var AverageVar
1 1 X
1 2 Y
1 3 Z
2 1 A
2 2 B
2 3 C
I would like to create this new "AverageVar" variable which computes an average across the three days for each unique ID.
So, for example, the AverageVar for the first three rows I would like to create and have (X + Y + Z)/3 displayed. Is there any easy code for this in SQL or R?
SELECT * INTO newtable
FROM
(SELECT UniqueID, AVG(Var) as AverageVar
FROM table
GROUP BY UniqueID);
SELECT O.UniqueID, O.Day, O.Var, N.AverageVar
FROM oldtable O
INNER JOIN
newtable N
ON O.UniqueID = N.UniqueID;

How to merge unknown number of tables together in sqlite

I have a sqlite database, database_all.
The database_all contains many tables of which some are data_table and some are id_tables.
The data_tables look like this:
index GenderId EducationId
1 1 1
2 2 2
3 2 1
and the id_tables look like this:
dim.Gender:
Id Name
1 F
2 M
dim.Education:
Id Name
1 High
2 Low
Is there a way, to revalue all the columns that contain the string Id (the number of the columns is unknown) with the values that correspond to the specific Id value from the respective id_table ?
The desired output should look like:
index GenderId EducationId
1 F High
2 M Low
3 M High
if i understand correctly, you need to find a way to analyze the queried table structure and automatically link its columns to the corresponding value table, right ?
I think this is not possible without programming so I didn't look into this so much...
but you might find some answers using pragmas.
select x.name, y.name from sqlite_master x, pragma_table_info(x.name) y;
this will dynamically give you the a full tables-to-columns mapping.
hope this helps

Update a Column from another Column using SQLite?

I have a DB with two columns with many records and from these two columns I have to do some mathematical operations and create other two columns.
For now I did like this:
I made a SELECT and read the two columns, and put everything in a List
Then I go through the List and UPDATE the table line by line
Do you think there is a faster way to do it? I also tried like this:
UPDATE myTable SET X_GAUSS = (SELECT X FROM myTable ) + 1, Y_GAUSS = (SELECT Y FROM myTable) + 2
(it's only an example)
But in this way every line of the new columns is the same as the line before, instead I want something like:
X Y X_GAUSS Y_GAUSS
1 2 2 4
3 4 4 6
5 6 6 8
...
A subquery like SELECT X FROM myTable returns the first row of the table.
You can simply access the columns of the same row directly:
UPDATE myTable
SET X_GAUSS = X + 1,
Y_GAUSS = Y + 2;

Oracle query to link records from same table which maps to mapping table

Apologies for the horrible question title,not sure how to articulate it better.
So to start out.
**Table Dummy_Table**
id description filter_key
1 Test Record1 filterkey1
2 Test Record2 filterkey1
3 Test Record1 filterkey2
4 Test Record2 filterkey2
The records with filterkey1 map to a table like this
**Table Mapping_table**
Dummy_Table_id someother_key (one(Dummy_Table_id) to many(someother_key)
1 x
1 y
1 z
1 r
2 y
2 r
Now : In a query I map the id's to each other in the Dummy_Table using the description,so I end up with a resultset like this
id_for_filter_key1 id_for_filterkey2
1 3
2 4
Ok,thats all good and well,it's the next step I'm having a issue with.I need to add records to Table Mapping_table which should end up looking like this
**Table Mapping_table**
Dummy_Table_id someother_key
3 x
3 y
3 z
3 r
4 y
4 r
So in essence whatever the id is for filterKey1 I would like to apply it's someother_key to the id's with filterkey2 (filterKey1 and filterkey2 relate to each other with their descriptions)
Now I don't know if I'm over complicating this.I'll tell you what my problem is.
I have records in the database with filterkey1 which map to the mapping table.Afterwords I added the records with filterkey2.These rows are duplicates just with another filter key.Now I need to apply the same mappings to the records with filterkey2
Changing the table structure is not a option atm.I need to give the DBA a insert query to achieve this.
Many thanks in advance.
This query gives missing values:
SELECT d.id_for_filterkey2, m.someother_key
FROM Mapping_table m
JOIN Dummy_Table d ON m.Dummy_Table_id = d.id_for_filter_key1
Demo: http://sqlfiddle.com/#!4/61ddfe/2
When we have missing values, then we can merge them into Mapping_table:
MERGE INTO Mapping_table m
USING( copy-the-above-query-and-paste-it-here) x
ON (x.id_for_filterkey2 = m.Dummy_Table_id)
WHEN NOT MATCHED THEN
INSERT( Dummy_Table_id, someother_key )
VALUES( x.id_for_filterkey2, x.someother_key );
Demo: http://sqlfiddle.com/#!4/d74304/3