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

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

Related

Return count id's value from multiple rows in one column Postgres

I'm having two tables (relation between themTest_case.id = Test_tag.test_id) like this:
Test_case table
id
name
1
Test name 1
2
Test name 2
3
Test name 3
4
Test name 4
Test_tag table
test_id
tag
1
feature:example1
1
package:Reports
1
QA
2
feature:example1
2
package:Reports
2
QA
3
feature:example1
3
package:Reports
3
QA
4
feature:newexample1
4
package:Charts
4
QA
The database tables and structure were already defined as I'm using a oublic library to push the results.
So, I need to return in the result the count of the id's and the value feature:example1
knowing that is a test that contains the tag package:Reports
So, it should return something like
Results
count(id)
tag
3
feature:example1
I already tried some different approaches without success.
How can I do that?
I think I'm as confused as everyone else, but this is a shot in the dark based on the various comments. There are much easier ways to arrive at this dataset, but I'm trying to read between the lines on your comments:
select
count (t.test_id), t.tag
from
test_case c
join test_tag t on c.id = t.test_id
where
t.tag like 'feature%' and
exists (
select null
from test_tag t2
where t2.test_id = t.test_id and t2.tag = 'package:Reports'
)
group by
t.tag

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

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).

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

group by ID and delete the values

I am modifying my description to make it more sense.
I want to select all the ID's which doesn't have value = Z2 ; How can I do that using sqlserver query ?
below is the data example
PK ID Value
1 1 x1
2 1 x2
3 1 x3
4 1 X4
5 2 X1
6 3 z2
7 2 Z2
8 4 X1
9 4 X2
EDIT: Since you clarified your question to just wanting to select the IDs that don't have z2 as a value, the query gets simpler. Just select all id's and remove those that have a z2 value using EXCEPT;
SELECT id FROM mytable
EXCEPT
SELECT id FROM mytable WHERE value='z2';
An SQLfiddle to test with.
(I'm assuming you want case insensitivity in this query)
DELETE FROM table where value <> 'z2';
You said in your question that you just want to select all the IDs which doesn't have value = Z2. So why would you want a DELETE query? A simple SELECT query like the following is supposed to give you the expected output:
SELECT ID
FROM Table
WHERE Value NOT IN ('Z2')
From the query, you can see that I have used an SQL clause - NOT IN, which will exclude certain values from specific column(s) while retrieving from your table. You don't need a DELETE query to view those IDs.
For better understanding, please refer to this link: SQL: NOT Condition

SQL update statement from a history table based on timestamp

I'm trying to write an update statement in Oracle that will find an attribute from a history table based on a timestamp. So, for example the data looks like:
TABLE A
A_ID TIMESTAMP ATTR
---------------------------------
1 5/27/2012 10:30:00 AM ?
TABLE B
B_ID A_ID TIMESTAMP ATTR
---------------------------------------
1 1 5/26/2012 9:01:08 AM W
2 1 5/27/2012 8:38:21 AM X
3 1 5/28/2012 9:01:01 AM Y
4 1 5/29/2012 11:37:54 PM Z
The lower bound is >= B.TIMESTAMP, but I'm not sure how to write the upper bound as < B."the next TIMESTAMP". So, in the example above the attribute on table A should update to "X".
This seems like a fairly common use case. I've seen this post, but it looks like a satisfactory answer was never reached, so I thought I'd post again.
UPDATE A SET attr = (
SELECT b1.attr
FROM B b1
INNER JOIN (
SELECT MAX(b3.timestamp) mx FROM B b3
WHERE b3.timestamp < A.timestamp
) b2 ON b1.timestamp = b2.mx
)
I can't remember if Oracle will allow me to use table A within the inner join sub query... Would you mind trying it?