Query to select related data from two tables in which one table has no related fields in third table

Thunder Blade

I have three tables in my Oracle db:

Peoples:
IdPerson PK
Name
Surname

Earnings:
IdEarning PK
IdPerson
EarningValue

Awards:
IdAward PK
IdPerson FK
AwardDescription

So one person can have many earnings, one earning or can have no any earnings. Also one person can have many awards, one award, or no any award.

I have to select Surname and AwardDescription but only for people who have any earnings, because it is possible to have some award but, also don't have any earning!

My problem is to make a correct group by statement. I use query posted below and I am selecting surname of person with a description of award, but it is duplicating for each row in Earnings for this person.

SELECT AwardDescription, Surname
FROM Awards
INNER JOIN People ON People.IdPerson= Awards.IdPerson
INNER JOIN Earnings ON Earnings .IdPerson= People.IdPerson;

How to group it and avoid duplicating rows for each earning of person? One person can be in many rows, but with different awards.

sgeddes

You could add DISTINCT to your query:

SELECT DISTINCT AwardDescription, Surname
FROM Awards
    INNER JOIN People ON People.IdPerson= Awards.IdPerson
    INNER JOIN Earnings ON Earnings .IdPerson= People.IdPerson;

Or another option is to use EXISTS:

SELECT AwardDescription, Surname
FROM Awards
    INNER JOIN People P ON P.IdPerson= Awards.IdPerson
WHERE EXISTS (
    SELECT 1
    FROM Earnings E
    WHERE P.IdPerson = E.IdPerson);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Django - OR filter on two fields, one of which is in related table

From Dev

How to retrieve data from two tables related using a third table, SQL Server

From Dev

How to retrieve data from two tables related using a third table, SQL Server

From Dev

MS SQL retrieve related data from same table in one query

From Dev

SQL query to select values from a table where the condition is from related tables

From Dev

Two fields in a table related to a field in another table

From Dev

Updating data using a SELECT query (with two related tables)

From Dev

DB Query related to get data from 2 tables which are not null

From Dev

Query to get the data in related table

From Dev

Query to get the data in related table

From Dev

running a query selecting data from one table and one of two possible other tables depending on data in the first table

From Dev

Set relations on two fields related to the same table

From Dev

Select all entries from one table which has two specific entries in another table

From Dev

MySql multiple select from two tables and join their results to a third table

From Dev

Mysql Insert data in third table based on data from two tables

From Dev

Use sqlalchemy to select only one row from related table

From Dev

SQL Server: Select hierarchically related items from one table

From Dev

Query to join two tables by mapping third table without returning all records from third table in Oracle

From Dev

Select data from 3rd related table

From Dev

How to get data from related table in Laravel (one to many)?

From Dev

Select from related tables

From Dev

SQL query to select row from one table which is not in another table

From Dev

counting related fields in two tables

From Dev

join two tables data in third table

From Dev

SQL Query to get the related data in different table

From Dev

SQL Query to get the related data in different table

From Dev

How to generate a query which consider data from one table and correspondingly fill 0 for non matching fields in mysql

From Dev

How to generate a query which consider data from one table and correspondingly fill 0 for non matching fields in mysql

From Dev

Select columns from two different table with different fields in one query Access database

Related Related

  1. 1

    Django - OR filter on two fields, one of which is in related table

  2. 2

    How to retrieve data from two tables related using a third table, SQL Server

  3. 3

    How to retrieve data from two tables related using a third table, SQL Server

  4. 4

    MS SQL retrieve related data from same table in one query

  5. 5

    SQL query to select values from a table where the condition is from related tables

  6. 6

    Two fields in a table related to a field in another table

  7. 7

    Updating data using a SELECT query (with two related tables)

  8. 8

    DB Query related to get data from 2 tables which are not null

  9. 9

    Query to get the data in related table

  10. 10

    Query to get the data in related table

  11. 11

    running a query selecting data from one table and one of two possible other tables depending on data in the first table

  12. 12

    Set relations on two fields related to the same table

  13. 13

    Select all entries from one table which has two specific entries in another table

  14. 14

    MySql multiple select from two tables and join their results to a third table

  15. 15

    Mysql Insert data in third table based on data from two tables

  16. 16

    Use sqlalchemy to select only one row from related table

  17. 17

    SQL Server: Select hierarchically related items from one table

  18. 18

    Query to join two tables by mapping third table without returning all records from third table in Oracle

  19. 19

    Select data from 3rd related table

  20. 20

    How to get data from related table in Laravel (one to many)?

  21. 21

    Select from related tables

  22. 22

    SQL query to select row from one table which is not in another table

  23. 23

    counting related fields in two tables

  24. 24

    join two tables data in third table

  25. 25

    SQL Query to get the related data in different table

  26. 26

    SQL Query to get the related data in different table

  27. 27

    How to generate a query which consider data from one table and correspondingly fill 0 for non matching fields in mysql

  28. 28

    How to generate a query which consider data from one table and correspondingly fill 0 for non matching fields in mysql

  29. 29

    Select columns from two different table with different fields in one query Access database

HotTag

Archive