mysql - sum columns from multiple tables based on multiple id's - mysql

I have a mySQL database with a couple of identical tables. I need to join all the tables and sum up the views and hits every time the id1 and id2 are equal in at least 2 tables, or simply show the row if not.
Please see below the tables structure:
Table1:
id..id2...views...hits
1...102...55......12
2...103...12......22
Table2:
id..id2...views...hits
1...123...512......13
2...103...123......43
Table3:
id..id2...views...hits
1...102...232......43
2...103...100......70
The end result should be the following table:
id...id2...views...hits
1....102...287....65 <-- This one is the result of adding 1st row of table1 and 2nd row of table 2
1....123...512....13 <-- This is the 1st row of table2 as there's no other id2 = 123
2....103...235....135 <-- This is the sum of 2nd row in table1 + 2nd row in table2 + 2nd row in table3
I hope this makes sense and someone can help with it.
Thanks !

Put the rows of all three tables together with a union, then group and sum like usual:
SELECT id, id2, SUM(views), SUM(hits)
FROM
(
SELECT id, id2, views, hits
FROM Table1
UNION ALL
SELECT id, id2, views, hits
FROM Table2
UNION ALL
SELECT id, id2, views, hits
FROM Table3
) x
GROUP BY id, id2

Related

SQL to get Union values if data exists in both the tables based on column

i have two tables (table1 and table2) with common column customer_id, with union and few filters i am getting all the values from table 1 and table2. I need only rows where both the tables have the data based on the common column customer_id. Below is the example.
The output that I need is as follows
Here's what I've tried:
SELECT customer_id,
age,
amount,
type
FROM table1
WHERE age > 5
UNION
SELECT customer_id,
age,
amount,
type
FROM table2
WHERE age > 5
Your query combines the rows of the two tables but doesn't match customers present in both. What you need to add is the condition on the "customer_id" being present in the corresponding other table:
SELECT *
FROM table1
WHERE age > 5 AND customer_id IN (SELECT customer_id FROM table2)
UNION
SELECT *
FROM table2
WHERE age > 5 AND customer_id IN (SELECT customer_id FROM table1)
You should choose between:
UNION, if you want to return unique rows between the two tables - makes an additional aggregation operation
UNION ALL, if you want to allow repeated rows between the two tables
If you don't have repeated identical rows in your tables, then you should go with the most efficient one, which is UNION ALL.

SQL Query to order 2 different tables with a similar attribute

I've 2 tables, they both have a structure similar to this one
id | content | date
However I would like to a make a query where the 2 tables are mixed together and ordered by their date
for example in the TABLE_A I've (1,Joe,20-11-2020)/(2,John,20-11-2021) TABLE_B has (1,Luke,20-11-2010)/(1,Mark,20-11-2011) The result I want to see is: (1,Luke,20-11-2010)/(1,Mark,20-11-2011)/(1,Joe,20-11-2020)/(2,John,20-11-2021)
SELECT T.ID,T.CONTENT,T.DATE
FROM TABLE_1 AS T
UNION ALL
SELECT A.ID,A.CONTENT,A.DATE
FROM TABLE_2 AS A
ORDER BY 3
try this:
select id, content, date
from (
select id, content, date
from tab_A
union all
select id, content, date
from tab_B
) C order by date
use "union" instead of "union all" if you are not interest to have duplicates rows

how can I get the total count of same name of different columns in one table in MySQL?

Here is the example of my question.
In the ID table, there are two columns, one is id1 and another is id2.
I just want to know the total id count in id1 and id2 columns, like the right table.
In that situations,
How can I get the total count of same name of different columns in one table in MySQL code?
You could use union all to generate a single column with all the IDs, and then count them:
SELECT id, COUNT(*)
FROM (SELECT id1 AS id FROM mytable
UNION ALL
SELECT id2 AS id FROM mytable) t
GROUP BY id

How to combine data from two tables with no common fields in SQL?

I have 2 tables: one table with many rows and a second table with one row. The tables have no fields in common. Is it possible to combine them into one table with many rows?
I've checked UNION, but MSDN says:
The following are basic rules for combining the result sets of two queries by using UNION:
Each SELECT statement within UNION must have the same number of columns.
The columns must also have similar data types.
The columns in each SELECT statement must also be in the same order.
Example
This is what my tables look like right now:
Table 1 Table 2
Column1 Column2 Column4 Column5 Column3
------- ------- ------- ------- -------
A 1 E 10 a
B 2
C 3
D 4
And this is what I'm trying to achieve as a result:
CONSOLIDATED_Table 3
Column1 Column2 Column3 Column4 Column5
------- ------- ------- ------- -------
A 1 E 10 a
B 2 E 10 a
C 3 E 10 a
D 4 E 10 a
You can add additional columns like this:
select tid, t_name, null as s_name
from teachers t
union all
select sid, null, s_name
from students s;
yes definitely you can use INNER JOIN . its a easy way to get data from multiple table. either you can use sub query and get data in Row format
We use joins to combine columns of multiple tables, whereas we use Union to join rows of multiple tables given that the columns types and nber of columns are the same in all the select queries with union.
Since you want to show all the rows, we can use Union.
Select tid as id, t_name as name from teachers
union all
Select sid as id, s_name as name from students;
Teachers and students
select * from (
select 'teacher' as rowtype, tid as id , t_name as name
from teachers
union all
select 'student', sid, s_name
from students) t
order by name;
This approach produces a cartesian product, but since you have only 1 row in table 2, it should work for your specific use case.
select * from table_1, table_2;

SQL Distinct cannot working if having count clause

I am having trouble with my sql query (Select distinct didnt work).
My sql is :
select distinct
count(T2.Column1)
from Table1 t2
where T2.Column1='2017-05-210'
The actual Column 1 data only have 3 data,
But the output is 12
Nb :
- Column1 data is having 1 to Many situation with Column2,
Here are the actual data:
Column 1 Column 2
1 A
1 B
1 C
1 D
2 A
2 B
2 C
2 D
3 A
3 B
3 C
3 D
Can anyone help me?
Really appreciate for your attention.
Thanks!
You want to count distinct values, so do count(distinct ):
select count(distinct T2.Column1)
from Table1 t2
where T2.Column1='2017-05-210'
(However, your sample data and the query's data/columns do not match.)
Your sample data, result and query do not match.
However, what your query does is:
Find all records with a column1 = '2017-05-210'.
Count all of these records where column1 is not null (which is true for all these records, as column1 = '2017-05-210').
This results in one number (one row, one column). But you additionally say with DISTINCT that you want to remove any duplicates from your result rows. With one row only there can be no duplicates, so the function is superfluous here.
So think about what you want to count really. You count distinct values (i.e. count ignoring duplicates) with COUNT(DISTINCT column), but COUNT(DISTINCT column1) would return 1 of course, because you are only looking for one value which is '2017-05-210' (or zero in case there is no record matching this value).