Unioning Two Tables With Different Number of Columns

Unioning two tables with different number of columns

Add extra columns as null for the table having less columns like

Select Col1, Col2, Col3, Col4, Col5 from Table1
Union
Select Col1, Col2, Col3, Null as Col4, Null as Col5 from Table2

How do I Union two tables with different columns

I suspect you want aggregation:

SELECT MonthId, DepartmentId, Salary, Deduction, Paid, MAX(Bank_1), MAX(Bank_2)
FROM (SELECT MonthId, DepartmentId, Salary, Deduction, Paid, Null AS Bank_1, Null AS Bank_2
FROM Table_A
WHERE MonthId = '02/2016'
UNION ALL
SELECT MonthId, DepartmentId, Salary, Deduction, Paid, Bank_1, Bank_2
FROM Table_B
WHERE MonthId = '02/2016'
) ab
GROUP BY MonthId, DepartmentId, Salary, Deduction, Paid;

UNION 2 Table with different Column name and different number of columns

Change your statement, so that you have the same number (and type) in both select statements:

$result = $db->rows("SELECT calendar_id, calendar_title, calendar_starttime, calendar_endtime
FROM (SELECT academic_schedule_id AS calendar_id, academic_schedule_name AS calendar_title, academic_schedule_datetimestart AS calendar_starttime, academic_schedule_datetimeend AS calendar_endtime
FROM academic_schedule_table
WHERE academic_schedule_faculty = 'Teknik Mesin' AND academic_schedule_status = 'Active'
UNION ALL

SELECT appointment_table.dosen_schedule_id AS calendar_id, dosen_schedule_table.dosen_schedule_name AS calendar_title, dosen_schedule_table.dosen_schedule_datetimestart AS calendar_starttime, dosen_schedule_table.dosen_schedule_datetimeend AS calendar_endtime
FROM appointment_table
INNER JOIN dosen_table
ON dosen_table.dosen_id = appointment_table.dosen_id
INNER JOIN dosen_schedule_table
ON dosen_schedule_table.dosen_schedule_id = appointment_table.dosen_schedule_id
WHERE appointment_table.siswa_id = ".$_SESSION['siswa_id']." AND appointment_table.status = 'Disetujui'
) ab
GROUP BY calendar_id, calendar_title, calendar_starttime, calendar_endtime");

SQL Server : how to union 3 tables with different number of rows

Number of rows doesn't matter for a union number of columns do.

What do you expect with a union?

I suppose you want you want all information (name address, contact) of customer having Id= @CustomerId

If I am right, you should make 2 join between the 3 tables using CustomerId as join clause

Edit:
https://www.db-fiddle.com/f/uGhtAZAVk64KzgThGekKk6/11

Union with different number of columns

Just add null or any other default value you like as static column

select  c1 as col1, 
c2 as col2,
c3 as col3,
null as col4,
null as col5
from Table1
union
select c1,
null,
null,
c4,
c5
from Table2

SQL UNION two tables with different column names

The logic in your query looks fine. I question the back ticks and the parentheses are not needed. Does this do what you need?

CREATE VIEW Dataset.Combined_Table AS                                
SELECT Date, Account, Measure
FROM Dataset.Table1
UNION ALL
SELECT Date, Account, Metric as Measure
FROM Dataset.Table2;

SQL Server : Union two tables with different columns

You need full outer join :

select coalesce(t1.name, t2.name), coalesce(t1.Region, t2.Region),
t1.Price_2018, t1.Cost_2018, t2.Price_2017, t2.Cost_2017
from table1 t1 full outer join
table2 t2
on t2.name = t1.name;


Related Topics



Leave a reply



Submit