简体   繁体   中英

Only Return First Dated Row From Joined Table

Using Microsoft SQL Server Management Studio - SQL Server database.

I have two tables - the first one is a table with all of our clients. The second table, the one I'm trying to join, is a table with all of the changes to clients profiles.

What I want is the earliest date_changed, and the info associated with that record, for each and every client.

The client table, for a specific client, contains:

client_code | first_name | last_name | zip_postal_code
------------|------------|-----------|----------------
168187      | Trees      | Karoline  | n

The changes table for this client has many records:

client_code | clerk_changed | date_changed
------------|---------------|------------------------
168187      | taranab       | 2017-03-18 10:37:00.000
168187      | taranab       | 2017-03-18 10:37:00.000
168187      | taranab       | 2017-03-18 10:37:00.000
168187      | taranab       | 2017-03-18 10:37:00.000
168187      | taranab       | 2017-03-18 10:37:00.000
168187      | taranab       | 2017-03-18 10:37:00.000
168187      | taranab       | 2017-03-18 10:37:00.000
168187      | taranab       | 2017-03-18 10:37:00.000
168187      | taranab       | 2017-03-18 10:37:00.000
168187      | taranab       | 2017-03-18 10:37:00.000
168187      | saml          | 2017-04-21 09:36:00.000
168187      | matijay       | 2017-04-22 06:22:00.000

Running the query:

SELECT client.client_code, client.first_name, client.last_name, client.zip_postal_code, clerk.clerk_changed, clerk.date_changed
FROM maclient client
LEFT JOIN (
    SELECT MIN(date_changed) AS 'date_changed', client_code, clerk_changed
    FROM maclntchg
    GROUP BY client_code, clerk_changed
) AS clerk ON client.client_code = clerk.client_code
WHERE client.client_code > '104' AND ((client.country_code = 'ca' AND LEN(client.zip_postal_code) <> 7) OR (client.country_code = 'us' AND LEN(client.zip_postal_code) <> 5)) AND client.zip_postal_code <> 'N/A' AND client.client_code = '168187'
ORDER BY clerk.clerk_changed, clerk.date_changed, client.last_name

Returns:

client_code | first_name | last_name | zip_postal_code | clerk_changed | date_changed
------------|------------|-----------|-----------------|---------------|------------------------
168187      | Trees      | Karoline  | n               | matijay       | 2017-04-22 06:22:00.000
168187      | Trees      | Karoline  | n               | saml          | 2017-04-21 09:36:00.000
168187      | Trees      | Karoline  | n               | taranab       | 2017-03-18 10:37:00.000

Instead of the expected result:

client_code | first_name | last_name | zip_postal_code | clerk_changed | date_changed
------------|------------|-----------|-----------------|---------------|------------------------
168187      | Trees      | Karoline  | n               | taranab       | 2017-03-18 10:37:00.000
SELECT client.client_code, client.first_name, client.last_name, client.zip_postal_code, clerk.clerk_changed, clerk.date_changed
FROM maclient client
OUTER APPLY (
    SELECT TOP 1 date_changed AS 'date_changed', client_code, clerk_changed
    FROM maclntchg clerk
    WHERE client.client_code = clerk.client_code
    ORDER BY date_changed ASC
) 
WHERE client.client_code > '104' AND ((client.country_code = 'ca' AND LEN(client.zip_postal_code) <> 7) OR (client.country_code = 'us' AND LEN(client.zip_postal_code) <> 5)) AND client.zip_postal_code <> 'N/A' AND client.client_code = '168187'
ORDER BY clerk.clerk_changed, clerk.date_changed, client.last_name

Another one:

SELECT client.client_code, client.first_name, client.last_name, client.zip_postal_code, clerk.clerk_changed, clerk.date_changed
FROM maclient client
LEFT JOIN (
    SELECT ROW_NUMBER() OVER(PARTITION BY client_code ORDER BY date_changed ASC) row_num, date_changed AS 'date_changed', client_code, clerk_changed
    FROM maclntchg 
) clerk ON client.client_code = clerk.client_code AND clerk.row_num = 1
WHERE client.client_code > '104' AND ((client.country_code = 'ca' AND LEN(client.zip_postal_code) <> 7) OR (client.country_code = 'us' AND LEN(client.zip_postal_code) <> 5)) AND client.zip_postal_code <> 'N/A' AND client.client_code = '168187'
ORDER BY clerk.clerk_changed, clerk.date_changed, client.last_name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM