How to sum column values from two tables and preserve another column which is only in one table

khan

i have created tables item and item2, I know maybe it's data redundancy but i want to know how can select it, and create a view?

create table item(
id  number(10) primary key,
name varchar2(20),
mark number(10));
insert into item values(10,'Apple1',23);
insert into item values(11,'Apple2',0);
insert into item values(12,'Apple3',0);
insert into item values(13,'Apple4',0);
insert into item values(14,'Apple4',0);
insert into item values(15,'Apple4',0);
insert into item values(16,'Apple4',0);

create table  item2(
id number(10),
mark number(10));
alter table item2 add(constraint id_fk FOREIGN KEY (id) references  item(id));
Insert into  item2 values(10,1);
Insert into  item2 values(10,1);
Insert into  item2 values(11,7);
Insert into  item2 values(12,14);

I can query both:

select * from item;

ID  Name    Mark
10  Apple1    23
11  Apple2     0
12  Apple3     0
13  Apple4     0
14  Apple4     0
15  Apple4     0
16  Apple4     0

select * from item2;

ID  Mark
10     1
10     1
11     7
12    14

I want to get the result set below using the select statement sum from the item and item2 tables:

ID  Name    Mark
10  Apple1    25
11  Apple2     7
12  Apple3    14
13  Apple4     0
14  Apple4     0
15  Apple4     0
16  Apple4     0

How can I combine my queries to produce that output?

mathguy

If I understand this correctly, you want to "pretend" that the second table had the NAME column also, populated according to the first table; then you would want to GROUP BY id and get the sum of MARK.

If so, instead of joining the tables to get the names (either before or after combining the tables and computing the sums), you can use a UNION ALL, in which you insert a fake NAME column with NULL in it for the second table; then you group by id, you sum the MARK column, and you take the MAX over NAME. MAX ignores NULL, so it will just pick the name from table ITEM.

The solution below follows that logic in every detail.

select   id, max(name) as name, sum(mark) as mark
from     ( select id, name, mark
             from item
           union all
           select id, null as name, mark
             from item2
         )
group by id
;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to select from a table in SQL where element in a column contains only one feature of another column which in turn contains two features in itself?

Set two values in one table based on sum of a column in another table

Getting unique values from joining two tables in which one table might have repeated values for a particular column

How to return data from two tables only when the column value in one table is same as another table using MySQL?

how to know which column should be used to join three tables if many id pointing from one table to another?

sum column values from one table in relation with values of column from another table

How to add a column to a dataset which adds values from one column and subtracts values from another column in R?

How to get only updated column values from two tables having same table structure

MySQL query to sum one column and count another column, from two tables, based on a common value?

Android Sum Quantity values from two Columns in two tables and update the Quantity value in one of the Column with the summed value

How do I join two tables on one column while preserving the values in another column?

How to sum multiple values (integer) from one column to descriptions (character) values in another column?

How to automatically load values from one table column to another in MYSQL

How to create a column that has the count from one table of values in another?

Subtract the sum of one column from another table's column

Select values from one column which share a value in another column

How to display all the values from one table but on two columns in that table, display the value of other tables if their column name is the same

How to find the values which are between column values (value should be looked from only one column)

copy values of two column of a table into a column from another table

Compare values from one column in table A and another column in table B

How to sum values of one column and group them by another column

Joining two tables and computing sum of one column, however retain row from left table if no matches in right

How to set values in one column table by values of column in another?

How to combine two tables in MySQL which have only one common column and other are different?

In MySQL how to join tables using their names which are based on column values of another table?

Sum two columns only if the values of one column is bigger/greater 0

mysql insert unique values from one column to a column of another table

Selecting multiple columns from two tables in which one column of a table has multiple where conditions and group them by two columns and order by one

How to get all columns from one table and only one column from another table with ID ? - MySql