How to join table in SQL - mysql

T1 table
+-----------+------------------+
| cookie_id | impression |
+-----------+------------------+
| 123 | 6/17/15 |
| 123 | 6/18/15 |
| 123 | 6/18/15 |
| 234 | 6/20/15 |
| 234 | 6/22/15 |
+-----------+------------------+
T2 table
+-----------+---------+---------+---------+
| cookie_id | HP | search | book |
+-----------+---------+---------+---------+
| 123 | 6/17/15 | | |
| 123 | 6/18/15 | | |
| 123 | | 6/18/15 | |
| 123 | | | 6/19/15 |
| 234 | 6/23/15 | | |
| 234 | | 6/25/15 | |
| 234 | | | 6/29/15 |
+-----------+---------+---------+---------+
we'd like to join T1 and T2 as the expected result as below T3 table :
+-----------+------------+---------+---------+---------+
| cookie_id | impression | HP | search | book |
+-----------+------------+---------+---------+---------+
| 123 | 6/17/15 | | | |
| 123 | 6/18/15 | | | |
| 123 | 6/18/15 | | | |
| 123 | | 6/17/15 | | |
| 123 | | 6/18/15 | | |
| 123 | | | 6/18/15 | |
| 123 | | | | 6/19/15 |
| 234 | 6/20/15 | | | |
| 234 | 6/22/15 | | | |
| 234 | | 6/23/15 | | |
| 234 | | | 6/25/15 | |
| 234 | | | | 6/29/15 |
+-----------+------------+---------+---------+---------+

http://sqlfiddle.com/#!9/375e6/2
SELECT cookie_id, impression, null,null, null
FROM t1
UNION ALL
SELECT cookie_id, null as impression, hp, search,book
FROM t2

As mentioned in the comment, you seems to want to UNION the tables: This will do it, but it may not be what you really really need...
SELECT * FROM (
(SELECT cookie_id,
"" AS impression,
HP,
search,
book
FROM T1)
UNION
(SELECT cookie_id,
impression,
"" AS HP,
"" AS search,
"" AS book
FROM T2)
) a
ORDER BY cookie_id;
This is out of my head, maybe I made a typo or something.

Related

How can I enhance this query to use only one view?

I am learning SQL and DB, I need to make the following query, I need to make a query that finds the dates that there were more car crashes and list the names of the people who were involved in these car crashes
person
| name | id_person |
|--------|------------|
| Oliver | 000000001 |
| Harry | 000000002 |
| Jacob | 000000003 |
| Maria | 000000004 |
| Jack | 000000005 |
participated
| id_person | num_crash | cost_damage |
|------------|-------------|---------------|
| 00000001 | 11111101 | 200 |
| 00000002 | 11111102 | 120 |
| 00000003 | 11111102 | 120 |
| 00000004 | 11111103 | 400 |
| 00000005 | 11111104 | 300 |
| 00000002 | 11111105 | 280 |
| 00000005 | 11111106 | 260 |
crash
| num_crash | date_crash | crash_scene |
|-------------|--------------|-------------|
| 11111101 | 2020/04/28 | bairro 4 |
| 11111102 | 2020/05/01 | bairro 1 |
| 11111103 | 2020/05/01 | bairro 2 |
| 11111104 | 2020/05/04 | bairro 3 |
| 11111105 | 2020/05/04 | bairro 1 |
| 11111106 | 2020/05/04 | bairro 3 |
output example
| data_crash | num_crash | name |
|--------------|-------------|-------|
| 2020/05/04 | 11111104 | Jack |
| 2020/05/04 | 11111105 | Harry |
| 2020/05/04 | 11111106 | Jack |
| 2020/05/01 | 11111102 | Harry |
| 2020/05/01 | 11111102 | Jacob |
| 2020/05/01 | 11111103 | Maria |
This is my sql query
CREATE VIEW vwfrequencedatecrash AS
SELECT date_crash, num_crash, crash_scene, ROW_NUMBER() OVER (PARTITION
BY date_crash ORDER BY date_crash) AS frequence
FROM crash
ORDER BY frequence DESC;
CREATE VIEW vwmorefrequencedate AS
SELECT date_crash, num_crash, crash_scene, frequence
FROM vwfrequencedatecrash
WHERE frequence > 1;
SELECT vw.date_crash, pa.num_crash, p.name
FROM vwmorefrequencedate vw
JOIN crash c ON c.date_crash = vw.date_crash
JOIN participated pa ON c.num_crash = pa.num_crash
JOIN person p ON pa.id_person = p.id_person
ORDER BY vw.frequence DESC, c.date_crash;
how can I improve this query?

MySQL: Rows concatenation

May be this is really a simple question, thanks in advance.
What I currently have:
+-----+---+---+---+---+
| sid | a | b | c | d |
+-----+---+---+---+---+
| 123 | | | | 4 |
| 123 | | 2 | | |
| 123 | | | 3 | |
| 123 | 1 | | | |
| 456 | | 5 | | |
| 456 | | | 6 | |
| 789 | | | | 8 |
| 789 | 7 | | | |
+-----+---+---+---+---+
What I am trying to get:
+-----+------+------+------+------+
| sid | a | b | c | d |
+-----+------+------+------+------+
| 123 | 1 | 2 | 3 | 4 |
| 456 | | 5 | 6 | |
| 789 | 7 | | | 8 |
+-----+------+------+------+------+
How such "rows concatenation" could be done in MySQL?
You can do this with the MAX() aggregation function with a GROUP BY clause in your query.
SELECT sid, MAX(a), MAX(b), MAX(c), MAX(d)
FROM table
GROUP BY sid
I used MAX() because it will filter the NULL values with others values.
More explanation here : MySQL Documentation

**MySql** Select Before After Row For Each GroupId

How can I have for each grp_id 2 rows:
before and after row of current unix timestamp,
+---------+--------------------+------------+-------+
| id | grp_id | utimes | value |
+---------+--------------------+------------+-------+
| 4156187 | 5282 | 1455663600 | 15897 |
| 4159888 | 5282 | 1455630000 | 26998 |*
| 4156190 | 5282 | 1455676200 | 28497 |
| 4156186 | 5282 | 1455661800 | 14097 |
| 4156183 | 5282 | 1455652800 | 5097 |
| 4156184 | 5282 | 1455656400 | 8697 |
| 4156185 | 5282 | 1455660000 | 12297 |
| 4156182 | 5282 | 1455651000 | 3297 |*
| 4163311 | 7216 | 1455693000 | 45297 |
| 4163275 | 7203 | 1455681600 | 33897 |
| 4163309 | 7214 | 1455697800 | 50097 |
| 4163308 | 7214 | 1455696000 | 48297 |
| 4163307 | 7214 | 1455694200 | 46497 |
| 4163306 | 7214 | 1455692400 | 44697 |
| 4163305 | 7214 | 1455690600 | 42897 |
| 4163304 | 7214 | 1455688800 | 41097 |
| 4151121 | 4356 | 1455703200 | 55497 |
| 4163271 | 7205 | 1455685500 | 37797 |
| 4163272 | 7205 | 1455687000 | 39297 |
| 4163269 | 7205 | 1455684900 | 37197 |
| 4163273 | 7205 | 1455687300 | 39597 |
| 4163264 | 7206 | 1455674400 | 26697 |
| 4163270 | 7205 | 1455685200 | 37497 |
+---------+--------------------+------------+-------+
Example:
unix-timestamp : 1455647703
+---------+--------------------+------------+-------+
| id | grp_id | utimes | value |
+---------+--------------------+------------+-------+
| 4159888 | 5282 | 1455630000 | 26998 |
| 4156190 | 5282 | 1455651000 | 28497 |
| 4159889 | XYZ | 1455630000 | 26998 |
| 4156191 | XYZ | 1455651000 | 28497 |
| 4159883 | ABC | 1455630000 | 26998 |
| 4156195 | ABC | 1455651000 | 28497 |
+---------+--------------------+------------+-------+
Thank you !
You can do it with a LEFT JOIN operation. The ON clause contains the 'business logic':
SELECT t1.*
FROM mytable AS t1
LEFT JOIN mytable AS t2
ON t1.grp_id = t2.grp_id
AND
((t1.utimes < 1455647703 AND t2.utimes < 1455647703 AND t2.utimes > t1.utimes)
OR
(t1.utimes > 1455647703 AND t2.utimes > 1455647703 AND t2.utimes < t1.utimes))
WHERE t1.grp_id = 5282 AND t2.id IS NULL
Demo here

How can I count the users with more than 1 Order?

I'm working on a MySQL database and I need to query the database and find out the users with more than one order. I tried using COUNT() but I cannot get it right. Can you please explain the correct way to do this?.
Here are my tables:
User
+-------------+----------+------------------+------------+
| userID | fName | email | phone |
+-------------+----------+------------------+------------+
| adele012 | Adele | aash#gmail.com | 0123948498 |
| ana022 | Anna | ashow#gmail.com | 0228374847 |
| david2012 | David | north#gmail.com | 902849302 |
| jefAlan | Jeffery | jefal#gmail.com | 0338473837 |
| josquein | Joseph | jquein#gmail,com | 0098374678 |
| jweiz | John | jwei#gmail.com | 3294783784 |
| jwick123 | John | jwik#gmail.com | 0998398390 |
| kenwipp | Kenneth | kwip#gmail.com | 0112938394 |
| mathCler | Maththew | matc#gmail.com | 0238927483 |
| natalij2012 | Natalie | nj#gmail.com | 1129093210 |
+-------------+----------+------------------+------------+
Orders
+---------+------------+-------------+-------------+
| orderID | date | User_userID | orderStatus |
+---------+------------+-------------+-------------+
| 1 | 2012-01-10 | david2012 | Delivered |
| 2 | 2012-01-15 | jweiz | Delivered |
| 3 | 2013-08-15 | david2012 | Delivered |
| 4 | 2013-03-15 | natalij2012 | Delivered |
| 5 | 2014-03-04 | josquein | Delivered |
| 6 | 2014-01-15 | jweiz | Delivered |
| 7 | 2014-02-15 | josquein | Delivered |
| 8 | 2015-10-12 | jwick123 | Delivered |
| 9 | 2015-02-20 | ana022 | Delivered |
| 10 | 2015-11-20 | kenwipp | Processed |
+---------+------------+-------------+-------------+
select user_userID, count(*) as orders_count from orders
group by user_userID having orders_count > 1
if you want additional data from your users table, you can do:
select * from user where user_id in (
select user_userID as orders_count from orders
group by user_userID having orders_count > 1
)

How to query for matching record sets

I am trying to do a query to find people living at the same address. But my SQL appears to fail and not sure why.
mysql> SELECT * FROM polished1 WHERE LName = 'BENDICH'; +------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+
| id | StateVoterID | Title | FName | MName | LName | NameSuffix | Birthdate | Gender | RegStNum | RegStFrac | RegStPreDirection | RegStName | RegStType | RegStPostDirection | RegUnitType | RegUnitNum | RegCity | RegState | RegZipCode | CountyCode | PrecinctPart | LegislativeDistrict | CongressionalDistrict | Mail1 | Mail2 | Mail3 | Mail4 | MailCity | MailZip | MailCountry | Registrationdate | AbsenteeType | LastVoted | StatusCode |
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+
| 2790 | 72253 | | ARNOLD | J | BENDICH | | 5 | M | 1754 | | NE | 2ND | ST | | | 0 | SEATTLE | WA | 98115 | 2071 | 43 | 7 | | | | | | | | 10/1/1965 | P | 2/12/2013 | A | NULL |
| 2791 | 72253 | | JUDITH | E | BENDICH | | 1 | F | 1754 | | NE | 2ND | ST | | | 0 | SEATTLE | WA | 98115 | 2071 | 43 | 7 | | | | | | | | 10/1/1965 | P | 2/12/2013 | A | NULL |
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+
2 rows in set (0.00 sec)
mysql> SELECT *, COUNT(*) c FROM polished1 WHERE LName = 'Bendich' GROUP BY RegStNum, RegStName, RegStType, RegUnitType, RegStPreDirection, RegStPostDirection, RegUnitNum, RegCity, RegState, RegZipCode HAVING c > 1;
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+---+
| id | StateVoterID | Title | FName | MName | LName | NameSuffix | Birthdate | Gender | RegStNum | RegStFrac | RegStPreDirection | RegStName | RegStType | RegStPostDirection | RegUnitType | RegUnitNum | RegCity | RegState | RegZipCode | CountyCode | PrecinctPart | LegislativeDistrict | CongressionalDistrict | Mail1 | Mail2 | Mail3 | Mail4 | MailCity | MailZip | MailCountry | Registrationdate | AbsenteeType | LastVoted | StatusCode | c |
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+---+
| 2790 | 72253 | | ARNOLD | J | BENDICH | | 5 | M | 1754 | | NE | 2ND | ST | | | 0 | SEATTLE | WA | 98115 | 2071 | 43 | 7 | | | | | | | | 10/1/1965 | P | 2/12/2013 | A | NULL | 2 |
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+---+
1 row in set (0.01 sec)
http://bpaste.net/show/UWC8wdOWMbFEQcT7eV6f/
select p.*
from polished1 p JOIN
(
SELECT *, COUNT(*) c
FROM polished1
WHERE LName = 'Bendich'
GROUP BY RegStNum, RegStName, RegStType, RegUnitType, RegStPreDirection, RegStPostDirection, RegUnitNum, RegCity, RegState, RegZipCode
HAVING c > 1) filtered ON p.RegStNum=filtered.RegStNum
and p.RegStName=filtered.RegStName
and p.RegStType=filtered.RegStType
and p.RegUnitType=filtered.RegUnitType
and p.RegStPreDirection=filtered.RegStPreDirection
and p.RegStPostDirection=filtered.RegStPostDirection
and p.RegUnitNum=filtered.RegUnitNum
and p.RegCity=filtered.RegCity
and p.RegState=filtered.RegState
and p.RegZipCode=filtered.RegZipCode
You should retrieve the addresses where count(*)>1 first and then get all joined addresses