如何在 return 视图中使用一个函数来仅显示不同的行并集?

How do I use a function in a view to return only distinct union of rows?

我正在尝试使用 RadGrid 控件在网页上引用视图。我需要根据每个唯一项目 ID 在更改 table 中最后输入的 Changedate 获取我的项目 table 中所有记录的子集以及状态 table 中的状态以及详细信息项目。 ProjectId 和 StatusID 在变更 table 中作为外键。

这是我的看法。

CREATE VIEW [dbo].[Project-by_View]
AS
SELECT TOP (100) PERCENT
    dbo.Projects.Id,
    dbo.Projects.ProjectName,
    dbo.Department.Name,
    dbo.Designer.FName + ' ' + dbo.Designer.LName AS Designer,
    dbo.Changes.ChangeDate,
    dbo.Projects.DueDate,
    dbo.Projects.Instructions,
    dbo.Status.Description
FROM  dbo.Projects
INNER JOIN dbo.Department
   ON dbo.Department.ID = dbo.Projects.DeptID
INNER JOIN dbo.Designer 
   ON dbo.Designer.Id = dbo.Projects.DesignerID
INNER JOIN dbo.Changes
   ON dbo.Changes.ProjectID = dbo.Projects.ID 
INNER JOIN dbo.Status
   ON dbo.Changes.StatusID = dbo.Status.Id
WHERE EXIST (SELECT * 
             FROM GetAllLastChangeDatebyProjectIds(0))
ORDER BY dbo.Projects.ID DESC

GO

和我的函数

USE [FIDO_DEV]
GO
/****** Object:  UserDefinedFunction [dbo].[GetAllLastChangeDatebyProjectIds]
Script Date: 9/14/2015 4:31:22 PM ******/
SET ANSI_NULLS ON 
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author, Geoffrey Stephens>
-- Create date: <Create Date, 09/14/20115>
-- Description: <Description, table of the Last Change Date for each Project>
-- =============================================
ALTER FUNCTION [dbo].[GetAllLastChangeDatebyProjectIds]
(
@ProjectID int
)
RETURNS TABLE 
AS
RETURN
(
Select ProjectID, StatusID, ChangeDate from (
Select
    ProjectID, StatusID, ChangeDate,
    row_number() over(partition by ProjectID order by ChangeDate desc) as rn
from
    Changes
) t
where t.rn = 1)

GO

函数和视图分开工作,但结合起来我仍然得到返回的完整数据集。

这个块看起来很可疑:

WHERE EXIST (SELECT * 
             FROM GetAllLastChangeDatebyProjectIds(0))

它不应该更像:

WHERE EXIST (SELECT * 
             FROM GetAllLastChangeDatebyProjectIds(Projects.Id))

我没有看得更深,它就跳出来了。


试试这样的东西(重新格式化为我喜欢的样式)

SELECT          Projects.Id, 
                dbo.Projects.ProjectName, 
                dbo.Department.Name, 
                dbo.Designer.FName + ' ' + dbo.Designer.LName AS Designer, 
                dbo.Changes.ChangeDate, 
                dbo.Projects.DueDate, 
                dbo.Projects.Instructions, 
                dbo.Status.Description

FROM (          SELECT          ProjectID, 
                                StatusID, 
                                ChangeDate 

                FROM (          FROM            ProjectID, 
                                                StatusID, 
                                                ChangeDate,
                                                row_number() OVER (PARTITION BY ProjectID ORDER BY ChangeDate DESC) as rn
                                FROM            Changes) t
                WHERE           t.rn = 1)) AS Selection

INNER JOIN      dbo.Projects
    ON          dbo.Projects.ID = Selection.ProjectID 

INNER JOIN      dbo.Designer 
    ON          dbo.Designer.Id = dbo.Projects.DesignerID 

INNER JOIN      dbo.Department
    ON          dbo.Department.ID = dbo.Projects.DeptID 

INNER JOIN      dbo.Changes 
    ON          dbo.Changes.ProjectID = dbo.Projects.ID

INNER JOIN      dbo.Status
    ON          dbo.Changes.StatusID = dbo.Status.Id

WHERE           Selection.StatusID = dbo.Changes.StatusID 

ORDER BY        dbo.Projects.ID DESC

我重新排序了 Selects,所以我从 Distinct project.ID 开始,然后进行连接。然后在 Status.ID 上执行 where 以获取最新的不同记录集。

Select TOP 100 PERCENT Projects.Id, dbo.Projects.ProjectName, dbo.Department.Name, dbo.Designer.FName + ' ' + dbo.Designer.LName AS Designer, dbo.Changes.ChangeDate, dbo.Projects.DueDate, dbo.Projects.Instructions, dbo.Status.Description

From GetAllLastChangeDatebyProjectIds(0) INNER JOIN
dbo.Projects ON dbo.Projects.ID = dbo.GetAllLastChangeDatebyProjectIds.ProjectID INNER JOIN
dbo.Designer ON dbo.Designer.Id = dbo.Projects.DesignerID INNER JOIN
dbo.Department ON dbo.Department.ID = dbo.Projects.DeptID INNER JOIN
dbo.Changes ON dbo.Changes.ProjectID = dbo.Projects.ID INNER JOIN
dbo.Status ON dbo.Changes.StatusID = dbo.Status.Id

Where dbo.GetAllLastChangeDatebyProjectIds.StatusID = dbo.Changes.StatusID 

ORDER BY dbo.Projects.ID DESC