Table UNION if Foreign key value is null on table 2 - sql

I have two tables that I'm trying to combine.
Table 1:
FAME_ID, FAME_Emblem_Title, FAME_Category
Table 2:
User_ID, FAME_ID, Times_Received
However, some values does not exist in table 2. For Example:
Table 1: Table 2:
Fame_ID: 1 Fame_ID: null/does not have a value
FAME_Emblem_Title: test1 User_ID: null/does not have a value
FAME_Category: 1 Times_Received: null/does not have a value
Fame_ID: 2 Fame_ID: 2
FAME_Emblem_Title: test2 User_ID: user1
FAME_Category: 1 Times_Received: 1
My goal is to filter the SQL query by Category and user but still display all results that match the first filter even if table 2 does not have any value. By the way my output is in JSON array form.
Result:
[
{
"User_ID": "user1",
"FAME_ID": 1,
"FAME_Category": "1",
"Times_Received": 1,
},
{
"User_ID": "null",
"FAME_ID": 2,
"FAME_Category": "1",
"Times_Received": null,
}
]
I'm honestly not sure if this is possible. Any help is highly appreciated.Thanks!

The JOIN required when you want to return all rows from the LEFT table even if there is no join to the RIGHT table is called a LEFT OUTER JOIN. If there is no match, then the row for the left table will be returned, and the value null substitude for all columns of the missing right table row.
You can achieve the join you want as follows, and output in JSON with:
SELECT t2.User_ID, t1.FAME_ID, t1.FAME_Category, t2.Times_Received
FROM table1 t1 LEFT OUTER JOIN table2 t2 on t1.FAME_ID = t2.FAME_ID
FOR JSON AUTO;

Related

SQL query to override content of column when matched column

Please who can help with this scenario?
I have two tables, both they have a common column ID, and Table 1 has a column Title. Normally I should update the content of this Title column for some ID, but since the table was already in use somewhere else, it wasn't a good idea to change data directly in Table 1.
That's why I created a new table table 2, which hold only the Title that must be changed associated with these ID that must be changed.
Now I am trying to get these updated titles from table 2, when there is a matching ID in table 1, otherwise show only the contents of table 1.
The result should be something like that but without using If statements.
__ID__ Title
| | | |
| | | |
You can use LEFT OUTER JOIN to this new table and COALESCE() function to say "If there is data in the new table, use it, otherwise use the data in the existing table" . Something like:
SELECT t1.id, COALESCE(t2.title, t1.title) as title
FROM t1
LEFT OUTER JOIN t2 ON t1.id = t2.id;

How to select many rows from 1 table and insert into a specific JSONB field of a specific row in another table? But in a single raw SQL query

postgres 10.3
I have about 1000 rows inside a table called sites
If I query like this
SELECT id, name from sites;
I will get the 1000 rows.
I also have another table called jsonindexdocument with a single row where the id is 1 and a field called index that is JSONB
Is it possible that in a single query I take out all the 1000 rows in sites table and then update the field called index under id 1?
The format of the json would be
[
{
"id": 10,
"name": "somename"
},
{
"id": 11,
"name": "another name"
} // and the rest of the 1000 rows
]
I am also okay if it uses more than 1 raw SQL statement.
UPDATE
I want to add that if the result is empty set, then default to empty array in the json field
Assuming you're OK with fully replacing the index value in the jsonindexdocument table:
UPDATE jsonindexdocument
SET index = (
-- using json_agg(row_to_json(sites.*)) would also work here, if you want to copy
-- all columns from the sites table into the json value
SELECT COALESCE(json_agg(json_build_object(
'id', id,
'name', name
)), '[]'::json)
FROM sites
)
WHERE id = 1;
As an example:
CREATE TEMP TABLE sites (
id INT,
name TEXT
);
CREATE TEMP TABLE jsonindexdocument (
id INT,
index JSON
);
INSERT INTO sites
VALUES (1, 'name1')
, (2, 'name2');
INSERT INTO jsonindexdocument
VALUES (1, NULL);
UPDATE jsonindexdocument
SET index = (
SELECT COALESCE(json_agg(json_build_object(
'id', id,
'name', name
)), '[]'::json)
FROM sites
)
WHERE id = 1;
SELECT * FROM jsonindexdocument;
returns
+--+------------------------------------------------------------+
|id|index |
+--+------------------------------------------------------------+
|1 |[{"id" : 1, "name" : "name1"}, {"id" : 2, "name" : "name2"}]|
+--+------------------------------------------------------------+

SQL/T-SQL Substring LEFT or Right doesn't appear to resolve

I have a two table where I have some values in a column UniqueKeys such as:
Table 1
2016_2016-2 S2_001840_30_01
2017_2017-2 D4_002213_3_01
The problem is that I am trying to match these with table 2 Unique values where the values are written in a different order such as :
Table 2:
001840_2016-2_S2_30_D_179_364128_400985
002213_2017-2_D4_3_E_752_376901_422828
Table 1 is from a different source system and table 2 is from different one. What I am trying to achieve is create a new table TABLE 3 where when the unique values match between table 1 and table 2 then insert the data from certain columns of table 1 and 2 into table 3 or else ignore the rest.
The way the Unique values should be is the following:
Year and Period: 2016-2
Cycle : S2
Unit: 001840
Group: 30
Giving the end result in Table 3 as:
001840_2016-2_S2_30
002213_2017-2_D4_3
You need to split both input values by "_" and then recombine the parts in the way they lead to the same format. Then you can join the tables.
Use two functions, the first one for values from type table 1, the second for values from table 2.
Effekt:
SELECT ...
FROM table1
JOIN table2 ON splitfunction1(table1.Key1) = splitfunction2(table2.Key2);

Finding non-matching data in two tables as per columns and arranging column data as row by row

i have a requirement like below
i have two tables in same data base, both table have same structure and column count.but the columns not present in the same position.
ex:
table 1
id name age
1 dhileep 22
2 uday 33
table 2
id age name
1 20 udayga
2 22 uday
i have id column is same for all tables, if i change the table also i have id same, but may columns name and column count and data count will change.
my final output is:
column_name id table1 table 2
name 1 dhileep udayga
note: i gave above as example, the count of columns is more than 500 and data exist approximately 50000+
use Sql JOIN .to join the 2 tables
use the following answer .i think it is useful for u.
SELECT t1.id,t1.name,t2.name FROM table1 AS t1 JOIN table2 AS t2 ON t1.id = t2.id

Recursive Delete SQL Oracle

I'm searching a way to do a recursive delete on a table.
The situation is that table have 3 foreign key 1 on itself and 2 others, I want to delete depending on the date of the occurrence.
Table1 --> Id1, dateOCC, ParentID
1, 13-12-26, null
2, 13-07-18, null
3, 14-12-31, 1
4, 13-06-26, 1
5, 14-07-23, null
6, 13-07-22, 2
Table2--> ID, stuff
Table3 --> ID, stuff
The ID of Table 2 and Table 3 are linked directly on ID of Table1.
The amount of data inside table 1 is approximately 20 000 000 row and the others table is approximately the same amount.
Here is on of the request I tried(its inside of a cursor who delete the data returned.
SELECT EO.ID,
EO.DATEOCC,
EO.PARENTID
FROM TABLE1 EO
WHERE EO.DATEOCC <= TO_DATE ('2013-12-31','YYYY-MM-DD')
AND NOT EXISTS(SELECT 1 FROM TABLE2 WHERE ID = EO.ID)
AND NOT EXISTS( SELECT 1 FROM TABLE3 WHERE ID = EO.ID)
START WITH EO.PARENTID IS NULL
CONNECT BY PRIOR EO.ID = EO.PARENTID;
This request is really really slow to output the data that I want.
And it seems that is not return the data that I need to delete.
Edit #1
Ok so heres an example of what I need to do(In this example I suppose that the table 2 and table 3 have no matching ID on Table 1)
Table1 --> Id1, dateOCC, ParentID
1, 13-12-26, null
2, 13-07-18, null
3, 14-12-31, 1
4, 13-06-26, 1
5, 14-07-23, null
6, 13-07-22, 2
After the delete sequence the table have to be like that if the >= date is 13-12-31
Table1 --> Id1, dateOCC, ParentID
1, 13-12-26, null
3, 14-12-31, 1
5, 14-07-23, null
So as you can see I delte the child that I can delete with his parent if possible. If I cant delete his parent because another child exist and I cant delete it I dont delete de parent(delete only the child that I can).
In a hierarchical query, the WHERE clause is applied after the START WITH and CONNECT BY are used to build the hierarchy. But syntactically it comes first, which makes it intuitively seem that it will be applied first.
If what you really want is to apply the WHERE clause first, then build the hierarchy, you can use a subquery like this:
SELECT EO.ID,
EO.DATEOCC,
EO.PARENTID
FROM (
SELECT * FROM TABLE1 EO
WHERE EO.DATEOCC <= TO_DATE ('2013-12-31','YYYY-MM-DD')
AND NOT EXISTS(SELECT 1 FROM TABLE2 WHERE ID = EO.ID)
AND NOT EXISTS( SELECT 1 FROM TABLE3 WHERE ID = EO.ID)
) EO
START WITH EO.PARENTID IS NULL
CONNECT BY PRIOR EO.ID = EO.PARENTID;
But it is not clear whether that is what you want. This would give you the top-level parents within the desired date range, and without children in the other tables, then build the entire hierarchy for those parents. It's possible that lower nodes in the hierarchy would have children in the other tables, which would cause the delete to fail.
If that's not what you want, I think you need to describe your requirements more clearly.