Select 在外键上不同,内部连接另一个 table
Select distinct on foreign key, inner join another table
基本上我想做的是,select 用户最后执行的 3 个操作。但是 RelationId 和 innerjoin 权限都没有重复项,只是为了确保用户仍然有权执行相同的操作。
我唯一想要获得许可的是 RelationId。
不重复我的意思是如果有两行具有相同 RelationId 的 Action,应该选择接近顶部的那一行(按 TimeStamp 排序)。
到目前为止我想到了什么:
SELECT DISTINCT a.*, p.RelationId
FROM [Action] [a]
INNER JOIN [Permission] p
ON ([p].[RelationId] = [a].[RelationId]
AND [p].[RelationType] = [a].[RelationType]
AND [p].[UserId] = [a].[UserId]
AND [p].[Deleted] = 0)
WHERE [a].[ActionType] = 'Clicked'
AND [a].[RelationType] = 'Direct'
AND [a].[UserId] = 5
AND [a].[Deleted] = 0
ORDER BY [a].[TimeStamp] DESC
OFFSET 0 ROWS
FETCH NEXT 3 ROWS ONLY
分页需要使用OFFSET X ROWS和FETCH NEXT 3 ROWS ONLY
由于某些原因这不起作用,因为我在 RelationId 上得到了重复项。
没有错误。
示例数据:
action (
id INTEGER PRIMARY KEY,
ActionType VARCHAR(50) not null,
RelationId INTEGER ForeignKey,
Deleted Bit not null,
TimeStamp DATE not null,
UserId INTEGER ForeignKey
);
期望的结果:一位用户最近执行了 3 次操作,该用户对其具有不同的 RelationId 权限。
根据您对每个用户最后 3 个的定义(如果是最后 3 个,则每个 relationId 不能是一行):
with data as (
SELECT *,
row_number() over (partition by relationId order by [timeStamp] desc) as rNo
from action
where [ActionType] = 'Clicked' AND
[RelationType] = 'Direct' AND
[UserId] = 5 AND
[Deleted] = 0
)
select a.id,a.ActionType,a.RelationId,a.Deleted,a.TimeStamp,a.UserId,
p.RelationId as pRelId
FROM [data] [a]
INNER JOIN [Permission] p ON ([p].[RelationId] = [a].[RelationId] AND [p].[RelationType] = [a].[RelationType] AND [p].[UserId] = [a].[UserId])
WHERE p.[Deleted] = 0 and a.rNo <= 3
ORDER BY [a].[TimeStamp] DESC;
如果我明白了,您只需要为每个权限执行最新的操作,并从所有这些中保留最近的三行。这里:
select top 3 p.RelationId,top_action.*
from
[Permission] p
cross apply
(
select top 1 *
from [Action] [a]
where
[p].[RelationId] = [a].[RelationId] AND [p].[RelationType] = [a].[RelationType] AND [p].[UserId] = [a].[UserId]
and [a].[ActionType] = 'Clicked' AND [a].[RelationType] = 'Direct' AND [a].[UserId] = 5 AND [a].[Deleted] = 0
order by [a].[TimeStamp] DESC
)top_action
where [p].[Deleted] = 0
order by top_action.[TimeStamp] DESC
Distinct 将始终区分所有参数。选择 * 总是不同的。
您只需选择一个参数即可确定。
您可以使用分组依据,或者使用相同的 select 分两步进行分组。像这样:
select * from Action where id in (
SELECT DISTINCT a.id
FROM [Action] [a]
INNER JOIN [Permission] p
ON ([p].[RelationId] = [a].[RelationId]
AND [p].[RelationType] = [a].[RelationType]
AND [p].[UserId] = [a].[UserId]
AND [p].[Deleted] = 0)
WHERE [a].[ActionType] = 'Clicked'
AND [a].[RelationType] = 'Direct'
AND [a].[UserId] = 5
AND [a].[Deleted] = 0
ORDER BY [a].[TimeStamp] DESC
OFFSET 0 ROWS
FETCH NEXT 3 ROWS ONLY
)
通过这样做解决了它。虽然不知道它是否是最好的解决方案。
SELECT [a].*, [p].[Id]
FROM (SELECT *, ROW_NUMBER() OVER(PARTITION BY [RelationId] ORDER BY [TimeStamp] DESC) AS row from [Action]) a
INNER JOIN [Permission] p ON ([ep].[RelationId] = [a].[RelationId] AND [p].[RelationType] = [a].[RelationType] AND [p].[UserId] = [a].[UserId] AND [p].[Deleted] = 0)
WHERE row = 1 AND [a].[ActionType] = 'Clicked' AND [a].[RelationType] = 'Direct' AND [a].[UserId] = 5 AND [a].[Deleted] = 0
ORDER BY [a].[TimeStamp] DESC
OFFSET 0 ROWS
FETCH NEXT 3 ROWS ONLY
基本上我想做的是,select 用户最后执行的 3 个操作。但是 RelationId 和 innerjoin 权限都没有重复项,只是为了确保用户仍然有权执行相同的操作。
我唯一想要获得许可的是 RelationId。
不重复我的意思是如果有两行具有相同 RelationId 的 Action,应该选择接近顶部的那一行(按 TimeStamp 排序)。
到目前为止我想到了什么:
SELECT DISTINCT a.*, p.RelationId
FROM [Action] [a]
INNER JOIN [Permission] p
ON ([p].[RelationId] = [a].[RelationId]
AND [p].[RelationType] = [a].[RelationType]
AND [p].[UserId] = [a].[UserId]
AND [p].[Deleted] = 0)
WHERE [a].[ActionType] = 'Clicked'
AND [a].[RelationType] = 'Direct'
AND [a].[UserId] = 5
AND [a].[Deleted] = 0
ORDER BY [a].[TimeStamp] DESC
OFFSET 0 ROWS
FETCH NEXT 3 ROWS ONLY
分页需要使用OFFSET X ROWS和FETCH NEXT 3 ROWS ONLY
由于某些原因这不起作用,因为我在 RelationId 上得到了重复项。 没有错误。
示例数据:
action (
id INTEGER PRIMARY KEY,
ActionType VARCHAR(50) not null,
RelationId INTEGER ForeignKey,
Deleted Bit not null,
TimeStamp DATE not null,
UserId INTEGER ForeignKey
);
期望的结果:一位用户最近执行了 3 次操作,该用户对其具有不同的 RelationId 权限。
根据您对每个用户最后 3 个的定义(如果是最后 3 个,则每个 relationId 不能是一行):
with data as (
SELECT *,
row_number() over (partition by relationId order by [timeStamp] desc) as rNo
from action
where [ActionType] = 'Clicked' AND
[RelationType] = 'Direct' AND
[UserId] = 5 AND
[Deleted] = 0
)
select a.id,a.ActionType,a.RelationId,a.Deleted,a.TimeStamp,a.UserId,
p.RelationId as pRelId
FROM [data] [a]
INNER JOIN [Permission] p ON ([p].[RelationId] = [a].[RelationId] AND [p].[RelationType] = [a].[RelationType] AND [p].[UserId] = [a].[UserId])
WHERE p.[Deleted] = 0 and a.rNo <= 3
ORDER BY [a].[TimeStamp] DESC;
如果我明白了,您只需要为每个权限执行最新的操作,并从所有这些中保留最近的三行。这里:
select top 3 p.RelationId,top_action.*
from
[Permission] p
cross apply
(
select top 1 *
from [Action] [a]
where
[p].[RelationId] = [a].[RelationId] AND [p].[RelationType] = [a].[RelationType] AND [p].[UserId] = [a].[UserId]
and [a].[ActionType] = 'Clicked' AND [a].[RelationType] = 'Direct' AND [a].[UserId] = 5 AND [a].[Deleted] = 0
order by [a].[TimeStamp] DESC
)top_action
where [p].[Deleted] = 0
order by top_action.[TimeStamp] DESC
Distinct 将始终区分所有参数。选择 * 总是不同的。 您只需选择一个参数即可确定。
您可以使用分组依据,或者使用相同的 select 分两步进行分组。像这样:
select * from Action where id in (
SELECT DISTINCT a.id
FROM [Action] [a]
INNER JOIN [Permission] p
ON ([p].[RelationId] = [a].[RelationId]
AND [p].[RelationType] = [a].[RelationType]
AND [p].[UserId] = [a].[UserId]
AND [p].[Deleted] = 0)
WHERE [a].[ActionType] = 'Clicked'
AND [a].[RelationType] = 'Direct'
AND [a].[UserId] = 5
AND [a].[Deleted] = 0
ORDER BY [a].[TimeStamp] DESC
OFFSET 0 ROWS
FETCH NEXT 3 ROWS ONLY
)
通过这样做解决了它。虽然不知道它是否是最好的解决方案。
SELECT [a].*, [p].[Id]
FROM (SELECT *, ROW_NUMBER() OVER(PARTITION BY [RelationId] ORDER BY [TimeStamp] DESC) AS row from [Action]) a
INNER JOIN [Permission] p ON ([ep].[RelationId] = [a].[RelationId] AND [p].[RelationType] = [a].[RelationType] AND [p].[UserId] = [a].[UserId] AND [p].[Deleted] = 0)
WHERE row = 1 AND [a].[ActionType] = 'Clicked' AND [a].[RelationType] = 'Direct' AND [a].[UserId] = 5 AND [a].[Deleted] = 0
ORDER BY [a].[TimeStamp] DESC
OFFSET 0 ROWS
FETCH NEXT 3 ROWS ONLY