使用Microsoft SQL Server Management Studio - SQL Server数据库。
我有两张桌子 - 第一张桌子是我们所有客户的桌子。第二个表,即我尝试加入的表,是一个包含客户档案的所有更改的表。
我想要的是最早的date_changed,以及与该记录相关联的信息,为每个客户提供。
特定客户端的客户端表包含:
client_code | first_name | last_name | zip_postal_code
------------|------------|-----------|----------------
168187 | Trees | Karoline | n
此客户的更改表包含许多记录:
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
运行查询:
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
返回:
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
而不是预期的结果:
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
答案 0 :(得分:2)
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
另一个:
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