Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello good people,

I have a database with 8 tables. Each table has a column called "Country", and then some other columns like Population or GDP or whatever (depending on the table).

Now the problem is, not all tables have exactly the same list and order or countries. Some have less, some more.

I need to put together all this data in one table. I need a query that will return data based on the column "Country" across all tables. I can't find a way to get unions to do that.

Any suggestions? Have I been able to explain myself?

Thanks!

EDIT: Here's a picture of 4 tables to explain my point
<a href="https://drive.google.com/file/d/1F0YxG6c6rcLE3d9GIFnmXcjfYl3THwTm/view?usp=sharing"">EXAMPLE.png - Google Drive

What I have tried:

I've tried a select across all tables, but it's not working.
Posted
Updated 17-Nov-17 1:42am
v4
Comments
CHill60 17-Nov-17 5:42am    
Post the select you tried and explain what "not working" means

Also some sample data and what you expect would be helpful
Member 13526494 17-Nov-17 5:52am    
Hey there,

Thanks for your comment. Unfortunately, I didn't save the SELECT query I tried. But it was obviously wrong. I've added a picture of 4 tables. Please look at it and see if it makes sense. Thanks!
OriginalGriff 17-Nov-17 6:00am    
No, regenerate the query, test it, and tell us what it does that is wrong: we aren't here to do your homework for you! :laugh:
CHill60 17-Nov-17 6:01am    
Put the table structure and some sample data into your question. Google Drive is a blocked site for me
Member 13526494 17-Nov-17 6:02am    
Well if you really wanna look at this failure, here you go. I don't know why it doesn't return anything at all.

SELECT Group, GDP, Exportsgdp, gdpcapita, Literacy, Manfgdp, Populationdensity, Tax
FROM CONT, GDPMAIN, EXPORTS, GDPC, LITERACYRATE, MANGDP, POPULATION, TAXRATE
WHERE GDPMAIN.[Country]='Austria' And GDPC.Country='Austria' And GDPMAIN.[Country]='Austria' And LITERACYRATE.Country='Austria' And MANGDP.Country='Austria' And POPULATION.Country='Austria';

1 solution

SELECT CONT.GDPMAIN.Group, EXPORTS.GDP, LITERACYRATE.Exportsgdp, MANGDP.gdpcapita, LITERACYRATE.Literacy, MANGDP.Manfgdp, POPULATION.Populationdensity, TAXRATE.Tax
FROM CONT
LEFT OUTER JOIN GDPMAIN on CONT.Country = GDPMAIN.Country
LEFT OUTER JOIN EXPORTS on CONT.Country = EXPORTS.Country
LEFT OUTER JOIN GDPC on CONT.Country = GDPC.Country
LEFT OUTER JOIN LITERACYRATE on CONT.Country = LITERACYRATE.Country
LEFT OUTER JOIN MANGDP on CONT.Country = MANGDP.Country
LEFT OUTER JOIN POPULATION on CONT.Country = POPULATION.Country
LEFT OUTER JOIN TAXRATE on CONT.Country = TAXRATE.Country
WHERE CONT.Country = 'Austria'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900