I don't want to write the same query over and over again because the query is redundant.

Asked 1 years ago, Updated 1 years ago, 76 views

Yes, it is possible to do it as it is, but I was wondering what to do to avoid redundancy, so I asked you.
I tried "my_goals" - "enemy_goals" but now it's "Unknown columns".

The original SQL is the correct part of the link below.Do you summarize everything in the from clause?
The version is 8.0.19

SELECT p1.kickoff, c1.name AS my_country, c2.name AS enemy_country, 
c1.ranking AS my_ranking, c2.ranking AS enemy_ranking,
(SELECT COUNT (g1.id) FROM goals g1 WHERE p1.id=g1.pairing_id) AS my_goals,
(
SELECT COUNT (g2.id) 
FROM goals g2 
LEFT JOIN pairing p2 ON p2.id=g2.pairing_id
WHERE p2.my_country_id =p1.enemy_country_id AND p2.enemy_country_id =p1.my_country_id
AS enemy_goals,
-- Add From Here
(SELECT COUNT (g1.id) FROM goals g1 WHERE p1.id = g1.pairing_id )-( 
SELECT COUNT (g2.id) 
FROM goals g2 
LEFT JOIN pairing p2 ON p2.id=g2.pairing_id
WHERE p2.my_country_id =p1.enemy_country_id AND p2.enemy_country_id =p1.my_country_id
AS goal_diff
-- Additional Up to here
FROM pairings p1
LEFT JOIN COUNTRIES c1 ON c1.id = p1.my_country_id
LEFT JOIN COUNTRIES c2 ON c2.id = p1.enemy_country_id
WHERE c1.group_name = 'C'AND c2.group_name = 'C'
ORDER BY p1.kickoff, c1.ranking

SQL Practice Questions – Question 17 | TECH Projin

Problem: Add score difference to the results of question 16.

US>Columns to display
·Date and time of kickoff
·Name of your country
·The name of the opponent country
·Domestic FIFA Rank
·Opponent country FIFA rank
·Number of goals scored in your own country
·Number of goals scored by the opposing countries
·Point difference (*Additional!)

Sort by
·Date and time of kickoff
·Domestic FIFA Rank

Enter a description of the image here

mysql sql

2022-09-30 21:48

1 Answers

Answer the sub-question in the linked best answer for the my_goals and enemy_goals columns in relation to the part copied to the goal_diff column.

I tried "my_goals" - "enemy_goals" but now it's "Unknown columns".

Yes.
You cannot use the my_goals column found in the select clause in the same select clause.(SQL below is an error)

SELECT1AS a,
       2-a ASb -- Unknown columns!
FROM hoge;--- dummy table

One of the correct answers is to summarize everything in the from clause to avoid redundancy.
You can get my_goals-enemy_goals outside of SQL that you want to change in parentheses.
Redundancy has decreased, but the SQL hierarchy has deepened due to sub-queries.

SELECT t.*, my_goals-enemy_goals AS goal_diff
FROM(
SELECT p1.kickoff, c1.name AS my_country, c2.name AS enemy_country, 
    c1.ranking AS my_ranking, c2.ranking AS enemy_ranking,
    (SELECT COUNT (g1.id) FROM goals g1 WHERE p1.id=g1.pairing_id) AS my_goals,
    (
        SELECT COUNT (g2.id) 
        FROM goals g2 
        LEFT JOIN pairing p2 ON p2.id=g2.pairing_id
        WHERE p2.my_country_id =p1.enemy_country_id AND p2.enemy_country_id =p1.my_country_id
    AS enemy_goals
FROM pairings p1
LEFT JOIN COUNTRIES c1 ON c1.id = p1.my_country_id
LEFT JOIN COUNTRIES c2 ON c2.id = p1.enemy_country_id
WHERE c1.group_name = 'C'AND c2.group_name = 'C'
ORDER BY p1.kickoff, c1.ranking
ASt;

Another correct answer is to use the with clause to separate the query to get the number of goals per game/team.
You can write a subquestion over the select clause, although it is not very short.

with goal_count as(
    select p.id id, count(g.id)value
    from pairing p, goalsg
    where p.id=g.pairing_id
    group by p.id)
SELECT p1.kickoff, c1.name AS my_country, c2.name AS enemy_country,
    c1.ranking AS my_ranking, c2.ranking AS enemy_ranking,
    ifnull(g1.value,0)AS my_goals,
    ifnull(g2.value,0)AS enemy_goals,
    ifnull(g1.value,0) - ifnull(g2.value,0)AS goal_diff
FROM pairings p1
LEFT JOIN COUNTRIES c1 ON c1.id = p1.my_country_id
LEFT JOIN goal_count g1 ON g1.id = p1.id,
     pairings p2
LEFT JOIN COUNTRIES c2 ON c2.id = p2.my_country_id
LEFT JOIN goal_count g2 ON g2.id = p2.id
WHERE p1.my_country_id = p2.enemy_country_id
AND p1.enemy_country_id=p2.my_country_id
AND c1.group_name = 'C'AND c2.group_name = 'C'
ORDER BY p1.kickoff, c1.ranking;

By the way, if it becomes too complicated to get everything in one SQL, I will consider splitting SQL and issuing multiple queries or programming them.
For example, goal_diff is not retrieved in SQL, but is retrieved in a program loop.

SQL is not very complicated this time, and the above response does not meet the requirements because it is a practice issue, but I think avoiding complexity in addition to redundancy is important as a simple SQL creation perspective.


2022-09-30 21:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.