How do I combine two nested MySQL queries into one View? - mysql

I have two queries, almost similar, but never the less,They must be treated as separate as they have different meanings and values, I want to combine them into one view, I tied doing UNION, but the result was they were all combined into one table, which is not what I want, I would like them to appear as entirely separate tables under one view, here is what I did:
CREATE VIEW TEAM_SUMMARY AS
SELECT DISTINCT COUNTRY.country_name AS CountryName_T1,count(Team1)AS NoOfGames,
SUM(Team1_score) AS TotalGoalsFor,SUM(Team2_score) AS TotalGoalsAgainst
FROM COUNTRY,MATCH_RESULTS WHERE
country_name = Team1
group by country_name
UNION
SELECT DISTINCT COUNTRY.country_name AS CountryNameT_2,count(Team2)AS NoOfGames,
SUM(Team2_score) AS TotalGoalsFor,SUM(Team1_score) AS TotalGoalsAgainst
FROM COUNTRY,MATCH_RESULTS WHERE
country_name = Team2
group by country_name;
UPDATE:
So, the output of my current query is something like this:
mysql> SELECT * FROM TEAM_SUMMARY;
+----------------------+-----------+---------------+-------------------+
| CountryName | NoOfGames | TotalGoalsFor | TotalGoalsAgainst |
+----------------------+-----------+---------------+-------------------+
| Algeria | 1 | 1 | 1 |
| Argentina | 4 | 5 | 1 |
| Australia | 2 | 2 | 6 |
| Belgium | 3 | 5 | 2 |
| Bosnia & Herzegovina | 1 | 3 | 1 |
| Brazil | 6 | 7 | 13 |
| Cameroon | 2 | 1 | 8 |
| Chile | 1 | 3 | 1 |
| Columbia | 3 | 7 | 1 |
| Costa Rica | 2 | 1 | 1 |
| Croatia | 1 | 1 | 3 |
| Ecuador | 1 | 0 | 0 |
| England | 1 | 1 | 2 |
| France | 3 | 5 | 1 |
| Germany | 4 | 9 | 3 |
| Ghana | 1 | 1 | 2 |
| Greece | 1 | 2 | 1 |
| Honduras | 2 | 1 | 5 |
| Iran | 1 | 0 | 0 |
| Italy | 2 | 0 | 2 |
| Ivory Coast | 1 | 2 | 1 |
| Japan | 2 | 1 | 4 |
| Mexico | 1 | 1 | 0 |
| Netherlands | 4 | 4 | 1 |
| Nigeria | 2 | 3 | 3 |
| Portugal | 1 | 2 | 1 |
| Russia | 1 | 1 | 1 |
| South Korea | 2 | 2 | 5 |
| Spain | 2 | 1 | 7 |
| Switzerland | 2 | 4 | 6 |
| Uruguay | 2 | 3 | 4 |
| USA | 2 | 2 | 3 |
| Algeria | 3 | 6 | 6 |
| Argentina | 3 | 3 | 3 |
| Australia | 1 | 1 | 3 |
| Belgium | 2 | 1 | 1 |
| Bosnia & Herzegovina | 2 | 1 | 3 |
| Brazil | 1 | 4 | 1 |
| Cameroon | 1 | 0 | 1 |
| Chile | 3 | 3 | 3 |
| Columbia | 2 | 5 | 3 |
| Costa Rica | 3 | 4 | 1 |
| Croatia | 2 | 5 | 3 |
| Ecuador | 2 | 3 | 3 |
| England | 2 | 1 | 2 |
| France | 2 | 5 | 2 |
| Germany | 3 | 9 | 1 |
| Ghana | 2 | 3 | 4 |
| Greece | 3 | 1 | 4 |
| Honduras | 1 | 0 | 3 |
| Iran | 2 | 1 | 4 |
| Italy | 1 | 2 | 1 |
| Ivory Coast | 2 | 2 | 4 |
| Japan | 1 | 1 | 2 |
| Mexico | 3 | 4 | 3 |
| Netherlands | 3 | 11 | 3 |
| Nigeria | 2 | 0 | 2 |
| Portugal | 2 | 2 | 6 |
| Russia | 2 | 1 | 2 |
| South Korea | 1 | 1 | 1 |
| Spain | 1 | 3 | 0 |
| Switzerland | 2 | 3 | 1 |
| Uruguay | 2 | 1 | 2 |
| USA | 2 | 3 | 3 |
+----------------------+-----------+---------------+-------------------+
64 rows in set (0.01 sec)
UPDATE2: Each query provides 32 row, and here they are combined into 64 rows so I don't know which belongs to which query, you can see that USA is the last row of each query and then it starts with Algeria again for the second query with different values that do not represent the column description.
What I want is something like this:
+------+--------+ +------+--------+
| code | SUM(*) | | code | SUM(*) |
+------+--------+ +------+--------+
| AAA | 4 | | AAA | 4 |
| BBB | 3 | | CCC | 1 |
+------+--------+ +------+--------+
Then I did some searching in order to use JOIN as shown here Combine results of two unrelated queries into single view but, this scenario is much less complicated than mine and couldn't apply it on my scenario, Any Idea?

One view doesn't product two result sets. But you can identify where they come from:
CREATE VIEW TEAM_SUMMARY AS
SELECT 'Team1' as which,
c.country_name AS CountryName_T1, count(Team1) AS NoOfGames,
SUM(Team1_score) AS TotalGoalsFor,
SUM(Team2_score) AS TotalGoalsAgainst
FROM COUNTRY c JOIN
MATCH_RESULTS mr
ON c.country_name = mr.Team1
GROUP BY country_name
UNION ALL
SELECT 'Team2' as which,
c.country_name AS CountryNameT_2,
count(Team2) AS NoOfGames,
SUM(Team2_score) AS TotalGoalsFor,
SUM(Team1_score) AS TotalGoalsAgainst
FROM COUNTRY c JOIN
MATCH_RESULTS mr
ON c.country_name = mr.Team2
GROUP BY country_name;
Notes:
SELECT DISTINCT with GROUP BY is almost always unnecessary (as in this case.
Use UNION ALL by default. Only use UNION when you specifically want to incur the overhead for removing duplicates.
Table aliases make the query easier to write and to read.
The above adds a column which to specify where each row comes from.

Related

Is there a mySQL procedure that can merge duplicate rows of data into one, then allow me to manipulate that data as if it were one row?

I'm trying to come up with a stored procedure that takes multiple rows that are exactly identical, and combines them into one row while summing one column, which can then be run through more stored procedures based on the sum of that one column.
I've tried a GROUP BY statement, but that doesn't actually group the rows together, because if I run the table through another procedure it performs actions as if each row were not combined. Performing a SELECT * FROM mytable query shows that each row was not actually combined into one.
Is there any way to permanently combine multiple rows into one singular row?
To start, I've got a table like this:
+-------+-----+--------+---------+------+-----+-----------+
| RowID | pID | Name | Date | Code | QTY | Purchased |
+-------+-----+--------+---------+------+-----+-----------+
| 1 | 1 | bob | 9/29/20 | 123 | 1 | |
| 2 | 1 | bob | 8/10/20 | 456 | 1 | |
| 3 | 2 | rob | 9/15/20 | 123 | 1 | |
| 4 | 2 | rob | 9/15/20 | 123 | 1 | |
| 5 | 2 | rob | 9/15/20 | 123 | 1 | |
| 6 | 2 | rob | 9/15/20 | 123 | 1 | |
| 7 | 2 | rob | 9/15/20 | 123 | 1 | |
| 8 | 3 | john | 7/12/20 | 987 | 1 | |
| 9 | 3 | john | 7/12/20 | 987 | 1 | |
| 10 | 4 | george | 9/12/20 | 684 | 1 | |
| 11 | 5 | paul | 2/2/20 | 454 | 1 | |
| 12 | 6 | amy | 1/12/20 | 252 | 1 | |
| 13 | 7 | susan | 5/30/20 | 131 | 1 | |
| 14 | 7 | susan | 6/6/20 | 252 | 1 | |
| 15 | 7 | susan | 5/30/20 | 131 | 1 | |
+-------+-----+--------+---------+------+-----+-----------+
By the end, i'd like to have a table like this:
+-------+-----+--------+---------+------+-----+-----------+
| RowID | pID | Name | Date | Code | QTY | Purchased |
+-------+-----+--------+---------+------+-----+-----------+
| 1 | 1 | bob | 9/29/20 | 123 | 1 | |
| 2 | 1 | bob | 8/10/20 | 456 | 1 | |
| 3 | 2 | rob | 9/15/20 | 123 | 5 | |
| 4 | 3 | john | 7/12/20 | 987 | 2 | |
| 5 | 4 | george | 9/12/20 | 684 | 1 | |
| 6 | 5 | paul | 2/2/20 | 454 | 1 | |
| 7 | 6 | amy | 1/12/20 | 252 | 1 | |
| 8 | 7 | susan | 5/30/20 | 131 | 2 | |
| 9 | 7 | susan | 6/6/20 | 252 | 1 | |
+-------+-----+--------+---------+------+-----+-----------+
Where exactly identical rows are combined into one row, and the QTY field is summed, that I can then add purchases to, or make deductions from the quantity as a total. Using GROUP BY statements can achieve this, but when I go to alter the quantity or add purchases to each person, it treats it like the first table, as if nothing was actually grouped.
So you have this table:
| RowID | pID | Name | Date | Code | QTY | Purchased |
+-------+-----+--------+---------+------+-----+-----------+
| 1 | 1 | bob | 9/29/20 | 123 | 1 | |
| 2 | 1 | bob | 8/10/20 | 456 | 1 | |
| 3 | 2 | rob | 9/15/20 | 123 | 1 | |
| 4 | 2 | rob | 9/15/20 | 123 | 1 | |
| 5 | 2 | rob | 9/15/20 | 123 | 1 | |
| 6 | 2 | rob | 9/15/20 | 123 | 1 | |
| 7 | 2 | rob | 9/15/20 | 123 | 1 | |
| 8 | 3 | john | 7/12/20 | 987 | 1 | |
| 9 | 3 | john | 7/12/20 | 987 | 1 | |
| 10 | 4 | george | 9/12/20 | 684 | 1 | |
| 11 | 5 | paul | 2/2/20 | 454 | 1 | |
| 12 | 6 | amy | 1/12/20 | 252 | 1 | |
| 13 | 7 | susan | 5/30/20 | 131 | 1 | |
| 14 | 7 | susan | 6/6/20 | 252 | 1 | |
| 15 | 7 | susan | 5/30/20 | 131 | 1 | |
The best way, as has been suggested, is to create a new table with the content of your query, then to rename the old table, and the new table to the original table's name, to check if everything is all right, and to drop the original table if yes.
CREATE TABLE indata_new AS
WITH grp AS (
SELECT
MIN(rowid) AS orowid
, pid
, name
, MAX(date) AS date
, code
, SUM(qty) AS qty
FROM indata
GROUP BY
pid
, name
, code
)
SELECT
ROW_NUMBER() OVER(ORDER BY orowid ASC) AS rowid
, *
FROM grp;
ALTER TABLE indata RENAME TO indata_old;
ALTER TABLE indata_new RENAME TO indata;
-- if "indata" now contains the data you want ...
SELECT * FROM indata;
-- out rowid | orowid | pid | name | date | code | qty
-- out -------+--------+-----+--------+------------+------+-----
-- out 1 | 1 | 1 | bob | 2020-09-29 | 123 | 1
-- out 2 | 2 | 1 | bob | 2020-08-10 | 456 | 1
-- out 3 | 3 | 2 | rob | 2020-09-15 | 123 | 5
-- out 4 | 8 | 3 | john | 2020-07-12 | 987 | 2
-- out 5 | 10 | 4 | george | 2020-09-12 | 684 | 1
-- out 6 | 11 | 5 | paul | 2020-02-02 | 454 | 1
-- out 7 | 12 | 6 | amy | 2020-01-12 | 252 | 1
-- out 8 | 13 | 7 | susan | 2020-05-30 | 131 | 2
-- out 9 | 14 | 7 | susan | 2020-06-06 | 252 | 1
-- you can ...
DROP TABLE indata_old;

Complex pivoting in mysql

I have a table in MySQL that has data and I want to achieve some sort of pivoting from the table, which has become complicated for me. I have tried looking into this but nothing seems to work for me. This is the structure of my table :
roomid| day| 1 | 2 | 3 | 4 | 5 | 6 |
------+----+---+---+---+---+---+---+
1 | 1 |BIO| | | | | |
1 | 2 | |CHE| | | | |
1 | 3 | | | | | |ENG|
1 | 4 | | |KIS| | | |
1 | 5 | | | | | |PHY|
2 | 1 |BIO| | | | | |
2 | 2 | |CHE| | | | |
2 | 3 | | | | |ENG| |
2 | 4 | | |KIS| | | |
2 | 5 | | | | | |PHY|
This table is holding timetable data, the roomid is id for rooms and the day is days from monday to friday (1 to 5). The columns 1 to 6 are period ids.
I need to organize the data to achieve results that show period ids for each class, each day. something like this :
|roomid| 1 | 2 | 3 | 4 | 5 | 6 | 1 | 2 | 3 | 4 | 5 | 6 | 1 | 2 | 3 | 4 | 5 | 6 | 1 | 2 | 3 | 4 | 5 | 6 | 1 | 2 | 3 | 4 | 5 | 6 |
-------+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
1 |BIO| | | | | | |CHE| | | | | | | | | |ENG| | |KIS| | | | | | | | |PHY|
2 |BIO| | | | | | |CHE| | | | | | | | | |ENG| | |KIS| | | | | | | | |PHY|
Kindly notice that the period ids repeat themselves for different days.
You can use conditional aggreagtion:
select room_id,
max(case when day = 1 then slot_1 end) as day_1_slot_1,
max(case when day = 1 then slot_2 end) as day_1_slot_2,
. . .
max(case when day = 2 then slot_1 end) as day_2_slot_1,
max(case when day = 2 then slot_2 end) as day_2_slot_2,
. . .
from schedule s
group by room_id
While not claiming to be a definitive solution, a normalised design might be somewhat as follows. Frankly, it stretches credulity to suggest that the present design is somehow more appropriate than this.
+--------+-----+------+---------+
| roomid | day | slot | subject |
+--------+-----+------+---------+
| 1 | 1 | 1 | BIO |
| 2 | 1 | 2 | BIO |
| 1 | 2 | 2 | CHE |
| 2 | 2 | 2 | CHE |
| 1 | 4 | 3 | KIS |
| 2 | 4 | 3 | KIS |
| 2 | 3 | 5 | ENG |
| 1 | 3 | 6 | ENG |
| 1 | 5 | 6 | PHY |
| 2 | 5 | 6 | PHY |
+--------+-----+------+---------+

How to select multiple values for one table "cell" MySQL

For all players, I need to find the player number and a list of the numbers of teams for which they have ever played.
Here is the table "MATCHES":
+---------+--------+----------+-----+------+
| MATCHNO | TEAMNO | PLAYERNO | WON | LOST |
+---------+--------+----------+-----+------+
| 1 | 1 | 6 | 3 | 1 |
| 2 | 1 | 6 | 2 | 3 |
| 3 | 1 | 6 | 3 | 0 |
| 4 | 1 | 44 | 3 | 2 |
| 5 | 1 | 83 | 0 | 3 |
| 6 | 1 | 2 | 1 | 3 |
| 7 | 1 | 57 | 3 | 0 |
| 8 | 1 | 8 | 0 | 3 |
| 9 | 2 | 27 | 3 | 2 |
| 10 | 2 | 104 | 3 | 2 |
| 11 | 2 | 112 | 2 | 3 |
| 12 | 2 | 112 | 1 | 3 |
| 13 | 2 | 8 | 0 | 3 |
+---------+--------+----------+-----+------+
The best I could come up with was:
SELECT DISTINCT playerno, teamno
FROM matches
ORDER BY playerno;
which results in:
+----------+--------+
| playerno | teamno |
+----------+--------+
| 2 | 1 |
| 6 | 1 |
| 8 | 1 |
| 8 | 2 |
| 27 | 2 |
| 44 | 1 |
| 57 | 1 |
| 83 | 1 |
| 104 | 2 |
| 112 | 2 |
+----------+--------+
Notice how player 8 has played on two teams. How can I get the table to show only one row for player 8 and a list of teamno's (1 & 2)?
You could use the group_concat aggregate function:
SELECT playerno, GROUP_CONCAT(DISTINCT teamno)
FROM matches
GROUP BY playerno
ORDER BY playerno;
You could use group_concat
SELECT playerno, group_concat( teamno)
FROM matches
GROUP BY playerno;

How to select all rows which have identical values

Here is my table:
+----+-------+------+
| id | name | code |
+----+-------+------+
| 1 | jack | 1 |
| 2 | peter | 1 |
| 3 | jack | 1 |
| 4 | ali | 2 |
| 5 | peter | 3 |
| 6 | peter | 1 |
| 7 | ali | 2 |
| 8 | jack | 3 |
| 9 | peter | 2 |
| 10 | peter | 4 |
+----+-------+------+
I want to select all rows that satisfy: the number of {those rows which its code value is between 1-3 and its name vale is identical} be more or equal than 4
From the above data, I want this output:
+----+-------+------+
| id | name | code |
+----+-------+------+
| 2 | peter | 1 |
| 5 | peter | 3 |
| 6 | peter | 1 |
| 9 | peter | 2 |
+----+-------+------+
How can I do that?
Use a subquery to figure out which names should be returned, then build your main query on that.
Try this:
select *
from mytable
where name in (
select name
from mytable
where code between 1 and 3
group by name
having count(*) > 3)
and code between 1 and 3

How to merge tables in MySQL, although not just an outright merge?

I have three tables titled 'artist', 'album', and 'track' in MySQL, each containing information pertaining to my music library. 'artist' has the name of 4 artists, 'album' has the titles of 5 albums from those artists, and 'track' has the name of all tracks on said albums. I would like to combine the tables so the each track name has beside it the correct album title and artist name. Here are my tables:
artist
+-----------+-----------------+
| artist_id | artist_name |
+-----------+-----------------+
| 1 | Kacey Musgraves |
| 2 | The Lone Bellow |
| 3 | Ed Sheeran |
| 4 | The Civil Wars |
+-----------+-----------------+
album
+-----------+----------+-----------------------+
| artist_id | album_id | album_name |
+-----------+----------+-----------------------+
| 1 | 1 | Pageant Material |
| 2 | 1 | Then Came the Morning |
| 3 | 1 | + |
| 3 | 2 | x |
| 4 | 1 | The Civil Wars |
+-----------+----------+-----------------------+
track
+-----------+----------+----------+-------------------------------+------------+
| artist_id | album_id | track_id | track_name | track_time |
+-----------+----------+----------+-------------------------------+------------+
| 1 | 1 | 1 | Makin' Music for Money | 2.97 |
| 1 | 1 | 2 | Dime Store Cowgirl | 3.58 |
| 1 | 1 | 3 | Late to the Party | 3.63 |
| 1 | 1 | 4 | Pageant Material | 3.90 |
| 1 | 1 | 5 | This Town | 2.95 |
| 1 | 1 | 6 | Biscuits | 3.28 |
| 1 | 1 | 7 | Somebody to Love | 3.22 |
| 1 | 1 | 8 | Miserable | 3.00 |
| 1 | 1 | 9 | Die Fun | 3.47 |
| 1 | 1 | 10 | Family is Family | 2.53 |
| 1 | 1 | 11 | Good Ol' Boys Club | 3.27 |
| 1 | 1 | 12 | Cup of Tea | 2.67 |
| 1 | 1 | 13 | Fine | 7.88 |
| 2 | 1 | 1 | Then Came the Morning | 4.01 |
| 2 | 1 | 2 | Fake Roses | 3.57 |
| 2 | 1 | 3 | Marietta | 3.57 |
| 2 | 1 | 4 | Take My Love | 3.25 |
| 2 | 1 | 5 | Call to War | 3.47 |
| 2 | 1 | 6 | Watch Over Us | 3.57 |
| 2 | 1 | 7 | Diners | 4.40 |
| 2 | 1 | 8 | Heaven Don't Call Me Home | 2.60 |
| 2 | 1 | 9 | If You Don't Love Me | 3.23 |
| 2 | 1 | 10 | Telluride | 4.28 |
| 2 | 1 | 11 | To the Woods | 2.00 |
| 2 | 1 | 12 | Cold As It Is | 2.93 |
| 2 | 1 | 13 | I Let You Go | 3.42 |
| 3 | 1 | 1 | The A Team | 4.66 |
| 3 | 1 | 2 | Drunk | 4.32 |
| 3 | 1 | 3 | U.N.I. | 3.82 |
| 3 | 1 | 4 | Grade 8 | 3.00 |
| 3 | 1 | 5 | Wake Me Up | 3.83 |
| 3 | 1 | 6 | Small Bump | 4.32 |
| 3 | 1 | 7 | This | 3.27 |
| 3 | 1 | 8 | The City | 3.90 |
| 3 | 1 | 9 | Lego House | 3.08 |
| 3 | 1 | 10 | You Need Me, I Don't Need You | 3.67 |
| 3 | 1 | 11 | Kiss Me | 4.68 |
| 3 | 1 | 12 | Give Me Love | 8.77 |
| 3 | 2 | 1 | One | 4.22 |
| 3 | 2 | 2 | I'm A Mess | 4.08 |
| 3 | 2 | 3 | Sing | 3.92 |
| 3 | 2 | 4 | Don't | 3.67 |
| 3 | 2 | 5 | Nina | 3.77 |
| 3 | 2 | 6 | Photograph | 4.32 |
| 3 | 2 | 7 | Bloodstream | 5.00 |
| 3 | 2 | 8 | Tenerife Sea | 4.02 |
| 3 | 2 | 9 | Runaway | 3.42 |
| 3 | 2 | 10 | The Man | 4.17 |
| 3 | 2 | 11 | Thinking Out Loud | 4.70 |
| 3 | 2 | 12 | Afire Love | 5.23 |
| 3 | 2 | 13 | Take It BAck | 3.47 |
| 3 | 2 | 14 | Shirtsleeves | 3.17 |
| 3 | 2 | 15 | Even My Dad Does Sometimes | 3.82 |
| 3 | 2 | 16 | I See Fire | 4.98 |
| 4 | 1 | 1 | The One That Got Away | 3.55 |
| 4 | 1 | 2 | I Had Me a Girl | 3.75 |
| 4 | 1 | 3 | Same Old Same Old | 3.80 |
| 4 | 1 | 4 | Dust to Dust | 3.83 |
| 4 | 1 | 5 | Eavesdrop | 3.58 |
| 4 | 1 | 6 | Devil's Backbone | 2.48 |
| 4 | 1 | 7 | From This Valley | 3.55 |
| 4 | 1 | 8 | Tell Mama | 3.80 |
| 4 | 1 | 9 | Oh Henry | 3.55 |
| 4 | 1 | 10 | Disarm | 4.70 |
| 4 | 1 | 11 | Sacred Heart | 3.32 |
| 4 | 1 | 12 | D'Arline | 3.10 |
+-----------+----------+----------+-------------------------------+------------+
In 'track' each number under artist_id corresponds to the same numbered entry in 'artist', and the same goes for album_id and 'album'. Basically, I want to create a new table with every track in 'track', along with its respective album and artist.Any help would be appreciated!
Use JOIN in your SELECT query, like,
select * from track_name
join artist_name on track_name.artist_id = artist_name.artist_id
join album_name on track_name.album_id = album_name.album_id;
Now if you want this in new table you can insert this records in that table, or better if you want to run above query multiple time then you can create a VIEW.
MySQL Views
That sounds like a very basic join
select *
from track_name
join artist_name using (artist_id)
join album_name using (album_id)
I am writing this query by hoping there would be columns album_id and artist_id in the respective tables.
I also suggest you to change the column name track_name so to avoid conflict between table name and column name.
SELECT *
FROM `track_name` t1
LEFT JOIN `artist_name` a1 ON t1.`artist_id`=a1.`artist_id`
LEFT JOIN `album_name` a2 ON t1.`album_name`=a2.`album_name`
GROUP BY t1.`track_name`;