Aggregating data between two tables in MySQL - mysql

So I have two tables that I need to aggregate data by.
The first looks like this:
zip code | key
x 1
x 2
x 3
y 4
y 5
The second looks like this:
characteristics | key
a 1
b 1
c 1
d 2
e 2
f 3
g 4
and I need to join them to look like this...
zip code | key | characteristics
x 1 a
x 1 b
x 1 c
x 2 d
x 2 e
x 3 f
y 4 g
... ... ...
I can't quite think of what the correct subqueries / joins would be to make this happen. Any help is really appreciated.

Try this
select table1.zipCode, table1.key, table2.characteristics
from table1
inner join table2 on table1.key = table2.key
Ok...
Then try this.
select t1.zipcode, t1.keys, t2.character
from table_1 t1
full outer join Table_2 t2 on t1.keys = t2.keys

Related

Mysql left outer join with a column in right table that is not foreign key

My understanding of left outer join is,
table1:
id(pk) name(unique_key) address phone
table2:
new_id(pk) name(foreign key) company work-experience
1st table:
1 a x 123
2 b y 234
3 c z 345
2nd table
1 a aaa 2
2 a aab 3
3 b aab 3
if I will do,
select * from table1 left outer join table2 on table1.name=table2.name,
it will give me
1 a x 123 1 a aaa 2
1 a x 123 2 a aab 3
2 b y 345 3 b aab 3
3 c z 345 NULL NULL NULL NULL
Now instead of above result, I want to get all the rows where company is aab. Also , for any entry in 1st table, if there is no corresponding entry in 2nd table then it should give me NULL for all columns in 2nd table.
like this:
1 a x 123 aab 3
2 b y 234 aab 3
3 c z 345 NULL NULL
is the above result possible with left outer join ? If not, How can I get the above result ?
You can simply add the conditions for the second table (right-side table), in the ON part of LEFT JOIN.
SELECT * FROM table1 AS t1
LEFT OUTER JOIN table2 AS t2
ON t1.name = t2.name AND
t2.company = 'aab'
Also, in case of multi-table queries, it is advisable to use Aliasing, for code clarity (enhanced readability), and avoiding unambiguous behaviour.
select t1.name,t1.address,t1.phoneNo,t2.comapnay,t2.workExperiance from table1 as t1
left outer join table2 as t2 on t1.name=t2.name AND
t2.company = 'aab'

SQL Number of Occurrences, summary Query

I have a table as below,
Product Promotion exists (Y/N) Week
A Y 1
B Y 1
C Y 1
A Y 2
B Y 2
C N 2
A Y 3
B Y 3
C Y 3
A Y 4
B Y 4
C N 4
I want to see an Promition exists combination Output on total table. Something like
A, B - 4
B,C - 2
A,C - 2
Since this is Just for 3 products looks simple.. I am looking at some thousands of records, and looking for same combinations where total count of occurrence is greater than some number. If taking the above example, if that count is 4.. then my output should be
A,B - 4
Try this:
SELECT p1, p2, COUNT(*) AS cnt
FROM (
SELECT t1.Product AS p1, t2.Product AS p2
FROM mytable AS t1
JOIN mytable AS t2
ON t1.Week = t2.Week AND
t1.Product < t2.Product AND
t1.Exists = 'Y' AND t2.Exists = 'Y') AS t
GROUP BY p1, p2
ORDER BY cnt DESC
To get only pairs exceeding a certain value, just wrap the above in a subquery and add a WHERE cnt >= someValue.
Demo here

SQL output two rows using one entry

I want to output two rows in a one entry but with two tables. here is my table:
table_A
-----------------------------------------------------------------------
checkkey checknum confirmed printed canceled
-----------------------------------------------------------------------
1 1 Y Y Y
2 2 Y Y N
3 10 N Y Y
table_B
-----------------------------------------------------------------------
checkkey checknum status
-----------------------------------------------------------------------
1 1 V
2 2 V
3 10 V
I want an output like this
-----------------------------------------------------------------------
checkkey checknum confirmed printed canceled status
-----------------------------------------------------------------------
1 1 Y Y Y
1 1 Y Y Y V
2 2 Y Y N
2 2 Y Y N V
3 10 N Y Y
3 10 N Y Y V
you can use a UNION , first query will get all with status as V based on tableB and second one gets all rows from A
select a.checkkey, a.checknum,a.confirmed, a.printed,a.canceled, b.status
from table_A a
inner join table_B b
on a.checkkey = b.checkkey
union all
select a.checkkey, a.checknum,a.confirmed, a.printed,a.canceled, NULL as status
from table_A a
order by checkkey, checknum;
I think this does what you want:
select a.*, 'V' as status
from table_A a
union all
select a.*, NULL as status
from table_A a
order by checkkey, checknum;
Table_B doesn't seem necessary at all.

MYSQL checking all values in a set match another set

I have two tables A and B with values below
Table A
X
------
1
2
3
Table B
X Y
------ ------
1 A
2 A
3 A
1 B
2 B
1 C
3 D
I need to find only the Y values from Table B which match All of the values in Table A.
So for the above example the only Y value that matches is A (A has an X value of 1,2,and 3)
This is an example of a "set-within-sets" subquery. I like to approach this with aggregation and a having clause.
select b.y
from tableB b join
tableA a
on b.X = a.X
group by b.y
having count(distinct b.x) = (select count(*) from tableA);

joining all columns of two tables in mysql conditionally

Mysql database has a tableA which has a many columns . one of the columns is SIM1.
Another table is tableB which has many columns . one of the columns is SIM2
the requirement is to join all columns of tableA and tableB given that SIM1 = SIM2.
LIKE THIS
tableA
col1 col2 SIM1 ..........col24
a x 1 5
b y 1 3
c z 0 2
d g 2 1
tableB
colA colB SIM2
x g 1
y f 0
x s 0
y e 2
The result of Select query should be
col1 col2 SIM1............col24 colA colB SIM2
a x 1 ........... 5 x g 1
c z 0 ......... . 2 x s 0
d g 2 .......... 1 y e 2
is it possible?
select * from tableA inner join tableB on tableA.SIM1 = tableB.SIM2