当除 ID 和时间戳之外的所有字段都相同时获取第一条记录?
Get first record when all fields are identical except ID and Timestamp?
这是我当前的查询:
SELECT TOP 6 *
FROM HS_IHE_ATNA_Repository.Aggregation
WHERE EventType = 'Retrieve Document Set'
它 return 的数据看起来与此类似:
ID TimeStamp tid Fruit Color User EventType
1 12:30:31 001 Apple Red Paul Retrieve Document Set
2 12:30:32 001 Apple Red Paul Retrieve Document Set
3 12:31:03 002 Orange Orange Steve Retrieve Document Set
4 12:31:04 002 Orange Orange Steve Retrieve Document Set
5 12:34:12 003 Banana Yellow Paul Retrieve Document Set
6 12:34:13 003 Banana Yellow Paul Retrieve Document Set
我希望我的查询仅 return 记录 1、3 和 5。本质上,所有内容都具有唯一的 tid。我该怎么做?
尝试:
SELECT distinct(*)
FROM HS_IHE_ATNA_Repository.Aggregation a
WHERE EventType = 'Retrieve Document Set'
AND TimeStamp = (select min(b.TimeStamp) from from HS_IHE_ATNA_Repository.Aggregation b
WHERE b.tid = a.tid)
ORDER BY ID asc
你需要创造性地使用 GROUP BY:
SELECT TOP 6 MAX(TimeStamp), *
FROM HS_IHE_ATNA_Repository.Aggregation
WHERE EventType = 'Retrieve Document Set'
GROUP BY tid||Fruit||Color||User
这会将具有相同 tid、Fruit、Color、User 的所有行分组。或者,您可以使用 concat 函数进行连接:
GROUP BY {fn CONCAT(tid,{fn CONCAT(Fruit,{fn CONCAT(Color,User)})})}
这是我当前的查询:
SELECT TOP 6 *
FROM HS_IHE_ATNA_Repository.Aggregation
WHERE EventType = 'Retrieve Document Set'
它 return 的数据看起来与此类似:
ID TimeStamp tid Fruit Color User EventType
1 12:30:31 001 Apple Red Paul Retrieve Document Set
2 12:30:32 001 Apple Red Paul Retrieve Document Set
3 12:31:03 002 Orange Orange Steve Retrieve Document Set
4 12:31:04 002 Orange Orange Steve Retrieve Document Set
5 12:34:12 003 Banana Yellow Paul Retrieve Document Set
6 12:34:13 003 Banana Yellow Paul Retrieve Document Set
我希望我的查询仅 return 记录 1、3 和 5。本质上,所有内容都具有唯一的 tid。我该怎么做?
尝试:
SELECT distinct(*)
FROM HS_IHE_ATNA_Repository.Aggregation a
WHERE EventType = 'Retrieve Document Set'
AND TimeStamp = (select min(b.TimeStamp) from from HS_IHE_ATNA_Repository.Aggregation b
WHERE b.tid = a.tid)
ORDER BY ID asc
你需要创造性地使用 GROUP BY:
SELECT TOP 6 MAX(TimeStamp), *
FROM HS_IHE_ATNA_Repository.Aggregation
WHERE EventType = 'Retrieve Document Set'
GROUP BY tid||Fruit||Color||User
这会将具有相同 tid、Fruit、Color、User 的所有行分组。或者,您可以使用 concat 函数进行连接:
GROUP BY {fn CONCAT(tid,{fn CONCAT(Fruit,{fn CONCAT(Color,User)})})}