Combine Two Tables for One Output

How can I merge the columns from two tables into one output?

Specifying the columns on your query should do the trick:

select a.col1, b.col2, a.col3, b.col4, a.category_id 
from items_a a, items_b b
where a.category_id = b.category_id

should do the trick with regards to picking the columns you want.

To get around the fact that some data is only in items_a and some data is only in items_b, you would be able to do:

select 
coalesce(a.col1, b.col1) as col1,
coalesce(a.col2, b.col2) as col2,
coalesce(a.col3, b.col3) as col3,
a.category_id
from items_a a, items_b b
where a.category_id = b.category_id

The coalesce function will return the first non-null value, so for each row if col1 is non null, it'll use that, otherwise it'll get the value from col2, etc.

Combine two tables for one output

You'll need to use UNION to combine the results of two queries. In your case:

SELECT ChargeNum, CategoryID, SUM(Hours)
FROM KnownHours
GROUP BY ChargeNum, CategoryID
UNION ALL
SELECT ChargeNum, 'Unknown' AS CategoryID, SUM(Hours)
FROM UnknownHours
GROUP BY ChargeNum

Note - If you use UNION ALL as in above, it's no slower than running the two queries separately as it does no duplicate-checking.

Combine 2 rows of different tables into one row in SQL?

What you are looking for is a table join, in this case an INNER JOIN between table1 and table2 where the email column matches.

SELECT table1.email, table1.name, table1.last_name, table1.activated, table2.status, table2.end_period, table2.qtd
FROM `table1` AS table1
INNER JOIN `table2` AS table2
ON table1.email = table2.email
WHERE table1.email = 'ex@ex.com';

This results in your expected output:

























emailnamelast_nameactivatedstatusend_periodqtd
ex@ex.comjohnconnor11182823220

Combine two table for one output sql query

Use a full outer join:

SELECT
COALESCE(t.i_id, n.i_id) AS i_id,
t.thread_note,
t.seq_id,
n.note_text,
n.entered_date
FROM Threads t
FULL OUTER JOIN Notes n
ON n.i_id = t.i_id
ORDER BY
i_id;

Note that having the need to do a full outer join often can indicate a problem with your relational model, because it means you don't know the key relationships between your tables.

Demo

Edit:

If you are using a database such as MySQL which does not support a full outer join, we can still simulate one:

SELECT *
FROM Threads t
LEFT JOIN Notes n
ON n.i_id = t.i_id
UNION ALL
SELECT *
FROM Threads t
RIGHT JOIN Notes n
ON n.i_id = t.i_id
WHERE t.i_id IS NULL;

Making a query to merge two tables upon criteria, with giving priority to the rows from the second table

All you need is UNION ALL for the 2 tables and use NOT EXISTS in the WHERE clause for table1:

SELECT t1.Dat, t1.Country,  t1.Val FROM table1 AS t1
WHERE NOT EXISTS (SELECT 1 FROM table2 AS t2 WHERE t2.Country = t1.Country AND t2.Dat = t1.Dat)
UNION ALL
SELECT Dat, Country, Val FROM table2
ORDER BY Dat, Country

Results:

Dat         Country     Val
1/10/2021 Australia 10
1/10/2021 Canada 50000
1/10/2021 Greece 50100
1/10/2021 japan 1000
2/10/2021 Australia 20
2/10/2021 Canada 60000
2/10/2021 Greece 60100
2/10/2021 Japan 2000

Combine two different tables Without Duplicating using Postgres SQL

Use combine two tables using UNION ALL then apply ROW_NUMBER() for serializing user_id wise value with descending date. Then retrieve last record by using CTE. Using UNION ALL for avoiding extra ordering.

-- PostgreSQL
WITH c_cte AS (
SELECT t.*
, ROW_NUMBER() OVER (PARTITION BY t.user_id ORDER BY t.date DESC) row_num
FROM (SELECT user_id, name, date
FROM table_1

UNION ALL

SELECT user_id, name, date
FROM table_2) t
)
SELECT user_id, name, date
FROM c_cte
WHERE row_num = 1
ORDER BY user_id

Also another way for doing same thing without CTE

SELECT u.user_id, u.name, u.date
FROM (SELECT t.*
, ROW_NUMBER() OVER (PARTITION BY t.user_id ORDER BY t.date DESC) row_num
FROM (SELECT user_id, name, date
FROM table_1

UNION ALL

SELECT user_id, name, date
FROM table_2) t
) u
WHERE u.row_num = 1
ORDER BY u.user_id

Excel - Combine data from multiple tables dynamically

If you have Excel 2019 or Office 365, with the FILTERXML and TEXTJOIN functions, you can use:

 =FILTERXML("<t><s>" & TEXTJOIN("</s><s>",TRUE,Table1,Table2, Table3) & "</s></t>","//s[.!=0]")

If those zero's are really blanks, you can omit [.!=0] from the xPath argument, but it won't hurt to leave it there

Sample Image

Edit:

With MAC versions of Office 365 that do not have the FILTERXML function, I believe the following will work:

=LET(
a,299,
x,IF(SEQUENCE(99,,0)=0,1,SEQUENCE(99,,0)*a),
y,TEXTJOIN(REPT(" ",a),TRUE,Table19,Table20,Table21),
z, TRIM(MID(y,x,a)),FILTER(z,(z<>"0")*(z<>""))
)

Note the a parameter in the above function

  • Because of how the splitting algorithm works, the sequence for each cell will not always start at the beginning of a string.
    • Hence, if there are enough letters in the various strings, the start number may eventually get offset enough to cause a split in the wrong location
  • One fix is to use an arbitrarily large number of space's to insert.
  • 99 is frequently large enough, but not for this data set.
  • 299 seems to be large enough for the data set as shown in your actual data.
    • I believe the minimum number should be the sum of the lengths of all the characters in the original tables (including the 0's) plus one (1). But not sure of this.
    • You can certainly adjust it as needed
  • If the number becomes too large, you could run into the 32,767 character limitation. If that happened, an error message would occur.

So, if you wanted to compute a, dynamically, you could try something like:

=LET(
a,SUM(LEN(Table19[Column1]),LEN(Table20[Column1]),LEN(Table21[Column1]))+1,
x,IF(SEQUENCE(99,,0)=0,1,SEQUENCE(99,,0)*a),
y,TEXTJOIN(REPT(" ",a),TRUE,Table19,Table20,Table21),
z, TRIM(MID(y,x,a)),FILTER(z,(z<>"0")*(z<>""))
)

but no guarantees.



Related Topics



Leave a reply



Submit