Joining 2 sql table into one table - mysql

Im trying to link a list of ID to a list of date in SQL.
This is the data i have both in different table.
| Date | ID |
|2017-12-25| 1 |
|2017-12-26| 2 |
|2017-12-27| 3 |
I will like to merge this into
| Date | ID |
|2017-12-25| 1 |
|2017-12-25| 2 |
|2017-12-25| 3 |
|2017-12-26| 1 |
|2017-12-26| 2 |
|2017-12-26| 3 |
|2017-12-27| 1 |
|2017-12-27| 2 |
|2017-12-27| 3 |

I would use CROSS JOIN for SQL Server
select a.[date], t.id from table t
cross join
(
select * from table
)a

You'll probably want to MERGE
MERGE table1 <alias1>
USING table2 <alias2>
ON <alias1>.ID = <alias2>.ID
WHEN NOT MATCHED BY TARGET THEN
INSERT (ID, Date)
VALUES (<alias1>.ID, <alias1>.Date);
SELECT * FROM table1;

Related

Percentage calculus based on multiple tables in sql

I have two tables in sql and I need to create another table performing the calculations based on two other tables.
The first one has the sum of revenue for each Ad Unit, table name is ad_unit_table
SELECT
d.`Date`,
'App' as `Partner`,
d.`Ad Unit`,
sum(d.`Revenue`) as `Revenue`
from
`d_master` as d
group by
`Ad Unit`, `Date`
The other table has the sum of revenue for ALL Ad Units, table name is sum_revenue
SELECT
`Date`,
`Partner`
`Ad Unit`,
sum(`Revenue`) as `Sum Revenue`
from
`ad_unit_table`
group by
`Date`
Now I have to find the percentage of the revenue for each Ad Unit. So the formula is (Ad Unit Rev / Sum Rev) * 100. My code currently looks like this:
SELECT
ad.`Date`,
ad.`Partner`,
ad.`Ad Unit`,
(ad.`Revenue` / s.`Sum Revenue`) * 100 as `Percentage`
FROM
`ad_unit_table` as ad
LEFT JOIN `sum_revenue` as s ON ad.`Partner`
GROUP BY
`Date`,
`Ad Unit`
It gives me all NULLS. I would appreciate any help. Thank you!
Are you sure you want to do LEFT JOIN sum_revenue as s ON ad.Partner in your last query?. I tested this construct and this creates a so called cartesian product.
All rows of the left table are combined with all the rows from the other table.
See: https://en.wikipedia.org/wiki/Cartesian_product
For example:
create table testing.test_a (id INT);
create table testing.test_b (id INT);
INSERT INTO test_a VALUES(1),(2),(3),(4);
INSERT INTO test_b VALUES(1),(2),(3),(5);
# Resulting in a cartesian product (4x4 entries)
SELECT * FROM test_a AS a LEFT JOIN test_b AS b ON a.id;
+------+------+
| id | id |
+------+------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 2 |
| 1 | 3 |
| 2 | 3 |
| 3 | 3 |
| 4 | 3 |
| 1 | 5 |
| 2 | 5 |
| 3 | 5 |
| 4 | 5 |
+------+------+
# Correctly LEFT joining test_a and test_b would be:
SELECT a.id, b.id FROM test_a AS a LEFT JOIN test_b AS b ON a.id = b.id
# Or use the USING clause to join on column from both tables with same name.
SELECT test_a.id, test_b.id FROM test_a LEFT JOIN test_b USING(id);
+------+------+
| id | id |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | NULL |
+------+------+
Are you sure the table sum_revenue has entries for all units in ad_unit_table. If this is not the case, some values from sum_revenue result in NULL values because of the not matching entries.
If you only want the values that match use an INNER JOIN instead of an LEFT JOIN.
Also make sure that when could the calculations. none of the values are NULL. Doing calculating with NULL values results in NULL values.
Example:
SELECT 100 / NULL; -- Result NULL
SELECT (10 * NULL) * 100; -- Result NULL
Without more information like table definitions and/or sample data this is all that I can do.

MySQL - Select values and remove duplicates by table name

I have two tables which have the same structure but another names (in first table I store default values, in second table I store saved values by user).
I select these values using union all:
SELECT * FROM `table_default` UNION ALL SELECT * FROM `table_saved`
Structure of table_default:
| ID | SOME_VAL |
| 1 | def_val |
| 2 | def_val |
| 3 | def_val |
Structure of table_saved:
| ID | SOME_VAL |
| 1 | test |
| 3 | text |
And now, when I using this query:
SELECT * FROM `table_default` UNION ALL SELECT * FROM `table_saved`
I got:
| ID | SOME_VAL |
| 1 | def_val |
| 2 | def_val |
| 3 | def_val |
| 1 | test |
| 3 | text |
But I want to get unique values by ID. Table_saved is more important so when select return duplicates I want to remove always record from table_default.
So finally I want to get:
| ID | SOME_VAL |
| 2 | def_val | --> from TABLE_DEFAULT because this record (by ID) is not exist in table_saved
| 1 | test | --> from TABLE_SAVED
| 3 | text | --> from TABLE_SAVED
I can't use GROUP BY id because I don't know which record will be remove (sometime GROUP BY remove duplicate from table_default but sometimes GROUP BY also remove duplicates from table_saved) so I can't manage this.
Is it possible to remove duplicates (something like GROUP BY) using table name and row name ? Or maybe somebody has another idea. Please help.
Thanks.
If I understand correctly, you want to always retain all records from table_saved, plus records from table_default having IDs not appearing in table_saved. One approach is to use a left join to find the unique records from table_default. Then union that with all records from table_saved.
SELECT t1.ID, t1.SOME_VAL
FROM table_default t1
LEFT JOIN table_saved t2
ON t1.ID = t2.ID
WHERE t2.ID IS NULL
UNION ALL
SELECT ID, SOME_VAL
FROM table_saved;
If a default value is always present you could use a LEFT JOIN and COALESCE:
SELECT d.ID, COALESCE(s.SOME_VAL, d.SOME_VAL) AS SOME_VAL
FROM table_default d
LEFT JOIN table_saved s USING(ID)

SQL Query to CAST and SUM columns in two different Tables having Multiple Entries

Need help in Building a Query which can select a Item and sum up its QTY which is in 2 different Tables.
Example Scenario:
Table 1:
ID | ITEM_NAME | QTY |
1 | Item_Desc1 | 0 |
2 | Item_Desc2 | 2 |
3 | Item_Desc3 | 4 |
4 | Item_Desc4 | 0 |
Table 2:
ID | ITEM_ID | BATCHNO | QTY
1 | 1 | B1 | 100
2 | 1 | B2 | 100
3 | 2 | B3 | 0
4 | 3 | B2 | 100
5 | 4 | B2 | 200
6 | 4 | B3 | 100
Need a Query to show in a Data grid Table as follows:
ID | Item_Name | QTY
1 | Item_Desc1 | 200
2 | Item_Desc2 | 2
3 | Item_Desc3 | 104
4 | Item_Desc4 | 300
Note:
This Query should be useful to run a Search query using 'LIKE' operator by using which User can search for Items Needed
SAMPLE Image of My APPLICATION to Run Search query using Table 1 and Table 2
SAMPLE Image of My APPLICATION DATABASE - TABLE2
SAMPLE Image of My APPLICATION DATABASE - TABLE1
To get sum from 2 table you can use following query
select a.ID, a.ITEM_NAME, a.QTY + coalesce(b.QTY,0)
from Table1 a
left join (
select ITEM_ID,sum(QTY) QTY
from Table2
group by ITEM_ID
) b on a.ID = b.ITEM_ID
Demo
Try the following query
select tbl1.id as ID,tbl1.Item_Name, tbl.qty+sum(tbl2.qty) as QTY
from table1 as tbl1 JOIN table2 as tbl2
ON tbl1.id = tbl2.id group by tbl2.item_id

SQL select with complex insert

I am trying to concatenate 2 tables into 1 table with 1 mysql command. I also want to concatenate a file path into one path in the new table. Any information would be helpful. Thank you.
Something like this :
INSERT INTO table3 VALUES (Location) SELECT "A.Loc_Path B._FilePath" FROM Table2 A INNER JOIN table1 B ON A._Loc_ID = B._Loc_ID
I would like to take Table 1 & 2 and create Table 3
Table 1
| ID | _FilePath | _Loc_ID |
| 1 | 001\yay\txt.html | 1 |
| 1 | 002\yay\txt.php | 2 |
Table 2
| _Loc_ID | Loc_Path |
| 1 | D:\documents\test\ |
| 2 | C:\Temp\test\ |
Table 3
| Id | Location |
| 1 | D:\documents\test\001\yay\txt.html |
| 2 | C:\Temp\test\002\yay\txt.php |
Use concat
INSERT INTO table3 (Location) SELECT concat (A.Loc_Path , B._FilePath)
FROM Table2 A INNER JOIN table1 B ON A._Loc_ID = B._Loc_ID
what you need is to use the concat function to concatenate the two columns:
INSERT INTO table3 VALUES (Location) SELECT CONCAT(A.Loc_Path, B._FilePath) FROM
Table2 A INNER JOIN table1 B ON A._Loc_ID = B._Loc_ID

MySQL query to select group of IDs from one table, depending on query on a second table

I am sure this would be easy to google if I knew the right words to use, but I've tried and not come up with anything: apologies if this is a common question on SO.
I have one table which lists a set of records which can be one of 4 types.
table_1:
+-------+------------+------+
| id | value | type |
+-------+------------+------+
| 1 | x | 1 |
| 2 | y | 1 |
| 3 | z | 2 |
| 4 | a | 3 |
+-------+------------+------+
I have another table which references the id of this table and stores data
table_2:
+-------+------------+------+
| id | table_1_id |value |
+-------+------------+------+
| 1 | 4 | A |
| 2 | 2 | B |
| 3 | 3 | C |
| 4 | 2 | D |
+-------+------------+------+
I want to write a query that effects:
"Find all the records from table 1 which are of type 1, take the id's of those records, and find all the records in table 2 where 'table_1_id' which match one of that set of ids."
In the above very oversimplified table example that would result in the query returning records with ids 2 and 4 in table 2
Sounds like your looking for IN:
select *
from table2
where table_1_id in (select id from table1 where type = 1)
Or perhaps you could JOIN the tables:
select t2.*
from table2 t2
join table1 t1 on t2.table_1_id = t1.id
where t1.type = 1
Joining the tables could result in duplicate records. Depends on your needs.
SELECT t1.value,t1.type,t2.value FROM table1 t1,table2 t2 WHERE t1.id = t2.table_1_id AND t1.type = 1;