How to find different names of cities via several tables

silent_hunter
:

I have five tables that contain artificial data about bank details.

My intention is to select only transactions that took place in a different city than that of the customer they belong to.

In order to do this, I wrote this query:

SELECT  c.Cityid,c.FirstName,c.LastName,ci.Name
FROM dbo.Customer c
INNER JOIN dbo.City ci ON ci.ID=c.Cityid
INNER JOIN dbo.Account acc ON acc.CustomerId=c.Id
INNER JOIN dbo.AccountDetails accde ON accde.AccountId=acc.Id
INNER JOIN dbo.Location lo ON lo.LocationTypeId=accde.LocationId
WHERE c.Cityid <>lo.CityId

But unfortunately I have results that are not good and my code listed data which is identical like city of customer, not city of transaction. Probably I have some mistake with WHERE expression.

So can anybody help me how to fix this problem ?

Also here in attachment there is diagram of tables in database.

Additionally here I put screen-shot from Customer table.In last column here there is CityId marked with yellow color.

Each of this customer have some transaction in City which is diffrent than their cities.For example for customer with Id 1 I need to have all other cities that this customer have transaction.In order to achive this I connect customer table with account details and account table.And the end I need to have city for each cusmtomer which is diffrent than their city. Also here I will put Location table

Bohemian
:

If you want to display city of transaction too, join the city table again:

SELECT
    c.FirstName,
    c.LastName,
    ci.Name as customer_city,
    tci.Name as transaction_city
FROM dbo.Customer c
JOIN dbo.City ci ON ci.ID=c.Cityid
JOIN dbo.Account acc ON acc.CustomerId=c.Id
JOIN dbo.AccountDetails accde ON accde.AccountId=acc.Id
JOIN dbo.Location lo ON lo.LocationTypeId=accde.LocationId
JOIN dbo.City tci ON tci.ID=lo.Cityid -- Added join to city from location
WHERE c.Cityid <>lo.CityId

I added another join to City from Location.

I also removed the CityId column, because generally surrogate id’s are meaningless.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How specify join variables with different names in different MySQL tables

From Dev

How to open several tables in different tabs in SQL Developer

From Dev

How to join different tables in sqlalchemy query having same column names?

From Dev

How to identify identical columns with different names across tables?

From Dev

PostgreSQL: union of several tables using dynamic names

From Java

How to find all the tables in MySQL with specific column names in them?

From Dev

How to find all the tables in database Teradata with specific column names in them?

From Dev

How do I sum the values of columns in several tables if tables have different lengths?

From Dev

How to Find Distance Between Cities In Prolog?

From Dev

How to change several directories' names?

From Dev

Names of all cities

From Dev

How to find file/directory names that are the same, but with different capitalization/case?

From Dev

How to find ONLY duplicate files that have different names?

From Dev

How to find directories containing two different file names?

From Dev

Intersecting several tables of different lengths in Matlab

From Dev

How to Autocomplete several Tables with DBEdit

From Dev

How to find orphaned records that point to different tables (paramerization required)?

From Dev

How to find MIN and MAX of two columns from TWO different tables?

From Dev

How to find different rows in two tables with same columns?

From Dev

Joining tables based on different column names

From Dev

Get the different column names on two tables MySQL

From Dev

Search multiples tables with different column names for a string

From Dev

Get the different column names on two tables MySQL

From Dev

SQL UNION two tables with different column names

From Dev

javascript: how to calculate the exact date and time for different cities around the globe

From Dev

How to run several times the same program with different inputs via Python script?

From Dev

How to get names of relevant ID's from 2 different tables and display them

From Dev

How to check if a given data exists in multiple tables(all different column names)

From Dev

How to append tables in Power Query while merging similar columns which have different names, types and order?

Related Related

  1. 1

    How specify join variables with different names in different MySQL tables

  2. 2

    How to open several tables in different tabs in SQL Developer

  3. 3

    How to join different tables in sqlalchemy query having same column names?

  4. 4

    How to identify identical columns with different names across tables?

  5. 5

    PostgreSQL: union of several tables using dynamic names

  6. 6

    How to find all the tables in MySQL with specific column names in them?

  7. 7

    How to find all the tables in database Teradata with specific column names in them?

  8. 8

    How do I sum the values of columns in several tables if tables have different lengths?

  9. 9

    How to Find Distance Between Cities In Prolog?

  10. 10

    How to change several directories' names?

  11. 11

    Names of all cities

  12. 12

    How to find file/directory names that are the same, but with different capitalization/case?

  13. 13

    How to find ONLY duplicate files that have different names?

  14. 14

    How to find directories containing two different file names?

  15. 15

    Intersecting several tables of different lengths in Matlab

  16. 16

    How to Autocomplete several Tables with DBEdit

  17. 17

    How to find orphaned records that point to different tables (paramerization required)?

  18. 18

    How to find MIN and MAX of two columns from TWO different tables?

  19. 19

    How to find different rows in two tables with same columns?

  20. 20

    Joining tables based on different column names

  21. 21

    Get the different column names on two tables MySQL

  22. 22

    Search multiples tables with different column names for a string

  23. 23

    Get the different column names on two tables MySQL

  24. 24

    SQL UNION two tables with different column names

  25. 25

    javascript: how to calculate the exact date and time for different cities around the globe

  26. 26

    How to run several times the same program with different inputs via Python script?

  27. 27

    How to get names of relevant ID's from 2 different tables and display them

  28. 28

    How to check if a given data exists in multiple tables(all different column names)

  29. 29

    How to append tables in Power Query while merging similar columns which have different names, types and order?

HotTag

Archive