Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you need two separate queries. First one to bring back the customers, once a customer such as Bob is selected a second query is performed based on Bob's attributes. </p> <p>A simple example would be a forms application that has two grids. The first displays a list of the users. When you select one of the users the second grid is populated with the results of the second query.</p> <p>The second query would be something like:</p> <pre><code>SELECT NAME, AGE, ZIPCODE, INCOME, FAVECOLOR, FAVEFOOD, FAVEMUSIC FROM [Amazon].[dbo].[Customers] WHERE Age = @BobsAge AND ZipCode = @BobsZipCode AND Income = @BobsIncome </code></pre> <p>It sounds like you want a simple self-join:</p> <pre><code>SELECT MatchingCustomers.NAME, MatchingCustomers.AGE, MatchingCustomers.ZIPCODE, MatchingCustomers.INCOME, MatchingCustomers.FAVECOLOR, MatchingCustomers.FAVEFOOD, MatchingCustomers.FAVEMUSIC FROM [Amazon].[dbo].[Customers] SourceCustomer LEFT JOIN [Amazon].[dbo].[Customers] MatchingCustomers ON SourceCustomer.Age = MatchingCustomer.Age AND SourceCustomer.ZipCode = MatchingCustomer.ZipCode AND SourceCustomer.Income = MatchingCustomer.Income WHERE SourceCustomer.Book = 'X' </code></pre> <p>If you want to see the all source customers and all of their matches in a single result set you can remove the where clause and select data SourceCustomer also:</p> <pre><code>SELECT SourceCustomer.Name SourceName, SourceCustomer.Age SourceAge SourceCustomer.ZipCode SourceZipCode, SourceCustomer.Income SourceIncome, MatchingCustomers.NAME, MatchingCustomers.AGE, MatchingCustomers.ZIPCODE, MatchingCustomers.INCOME, MatchingCustomers.FAVECOLOR, MatchingCustomers.FAVEFOOD, MatchingCustomers.FAVEMUSIC FROM [Amazon].[dbo].[Customers] SourceCustomer LEFT JOIN [Amazon].[dbo].[Customers] MatchingCustomers ON SourceCustomer.Age = MatchingCustomer.Age AND SourceCustomer.ZipCode = MatchingCustomer.ZipCode AND SourceCustomer.Income = MatchingCustomer.Income WHERE SourceCustomer.Book = 'X' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload