Skip to content
Advertisement

Query for two tables with similar information

I am working on some baseball stats to improve my database abilities I have data for hitters from 2017 and 2018 MLB seasons. what I hope to accomplish is to average the number of games played, at bats and hit over the two seasons. Eventually I want to do it over five seasons but I figure I need to start somewhere. The problem is that both tables have exactly the same names for the columns. I think I need to do a UNION but I am not sure.

I am using SSMS, the Express version. I have tried a simple query, thinking it would do some thing but obviously, I should have known better. I tried:

this is the Excel file example. Both 2017 and 2018 have these fields exactly as shown, well, with about ten more fields that I am not using yet.

So what I want, is to combine both of the seasons into one two year total and then average them.

Advertisement

Answer

Try a union query:

The idea behind the union query is that it brings the hit data for each player, for both 2017 and 2018, into a single intermediate table, which we can then aggregate to find the averages.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement