1

我有一个视图可以获取每个场地的每个类别的状态。

我需要显示以上所有内容,但只显示每个场地每个类别的最新状态。

一般的想法可能是,对于商店中的每件商品,都有一个状态。所以A店的黄瓜字母西红柿的状态分别是好坏。同样适用于商店 B、C 等。现在这些状态可以随时更新 - 但只应显示最新更新。

我试图创建如下视图:

SELECT
       dbo.vwVenues.HospitalID
     , dbo.vwVenues.Hospital AS hospitalName
     , dbo.tblVenueStatus.id AS statusId
     , dbo.tblVenueStatusName.statusName
     , dbo.tblVenueStatusCategories.categoryName
     , dbo.tblVenueStatus.dateAdded AS statusDateAdded
     , dbo.tblVenueStatus.loggedBy AS statusLoggedBy
     , dbo.tblVenueStatusNotes.noteContent
     , dbo.tblVenueStatusNotes.dateAdded AS noteDateAdded
     , dbo.tblVenueStatusNotes.removed AS noteRemoved
     , dbo.tblVenueStatus.resolved AS statusResolved
     , dbo.tblVenueStatus.resolveDate AS statusResolveDate
     , dbo.tblVenueStatus.removed AS statusRemoved
     , dbo.tblVenueStatus.dateRemoved AS statusRemovedDate
     , dbo.tblVenueStatus.categoryId
FROM 
    dbo.tblVenueStatusName
  RIGHT OUTER JOIN dbo.tblVenueStatusNotes
  RIGHT OUTER JOIN dbo.tblVenueStatus
    ON dbo.tblVenueStatusNotes.id = dbo.tblVenueStatus.noteId
  LEFT OUTER JOIN dbo.tblVenueStatusCategories
    ON dbo.tblVenueStatus.categoryId = dbo.tblVenueStatusCategories.id
    ON dbo.tblVenueStatusName.id = dbo.tblVenueStatus.statusNameId
  FULL OUTER JOIN dbo.vwVenues
    ON dbo.tblVenueStatus.venuId = dbo.vwVenues.HospitalID

我得到了所有的场地,但我得到了每个类别的所有状态,而不仅仅是最新的。即使试图正确地表达这个问题也很困难——所以我目前的搜索没有任何结果。

有人可以帮忙吗? 编辑

CREATE TABLE [dbo].[tblVenueStatus](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [statusNameId] [int] NOT NULL,
    [venuId] [int] NOT NULL,
    [categoryId] [int] NOT NULL,
    [loggedBy] [nvarchar](50) NOT NULL,
    [noteId] [int] NOT NULL,
    [dateAdded] [datetime] NOT NULL,
    [resolved] [tinyint] NOT NULL,
    [resolveDate] [datetime] NULL,
    [removed] [tinyint] NOT NULL,
    [dateRemoved] [datetime] NULL,
 CONSTRAINT [PK_tblSiteVenuStatus] PRIMARY KEY CLUSTERED 

CREATE TABLE [dbo].[tblVenueStatusCategories](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [categoryName] [nvarchar](50) NOT NULL,
    [categoryDescription] [nvarchar](150) NULL,
 CONSTRAINT [PK_tblVenueStatusCategories] PRIMARY KEY CLUSTERED 

CREATE TABLE [dbo].[tblVenueStatusName](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [statusName] [nvarchar](50) NOT NULL,
    [statusDescription] [nchar](150) NULL,
 CONSTRAINT [PK_tblVenuStatusName] PRIMARY KEY CLUSTERED 

CREATE TABLE [dbo].[tblVenueStatusNotes](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [noteContent] [nvarchar](500) NULL,
    [dateAdded] [datetime] NOT NULL,
    [removed] [tinyint] NOT NULL,
    [dateRemoved] [datetime] NULL,
 CONSTRAINT [PK_tblVenueStatusNotes] PRIMARY KEY CLUSTERED 

期望的输出将是:

Venu Name  |  Category 1 Status  |  Category 2 Status
4

1 回答 1

1

尝试这样的事情。由于您说您需要在样本中看到的内容,因此很大程度上是sumerized。试试这个,让我知道它是否有帮助。您可以在此处添加您需要的其他列,它获取每个医院每个类别的最新状态,并以易于阅读的网格显示所有医院和类别。

SELECT * FROM           
(
    SELECT VwV.HospitalID, vwV.Hospital, ISNULL(vStatusName.statusName, '') AS statusName, vStatusCategories.categoryName 
    FROM            
    (
        SELECT topStatus.HospitalID, topStatus.categoryId, sta2.id 
        FROM            
        (
            SELECT HospitalID, categoryId, MAX(sta.dateAdded) AS DateAdded 
            FROM vwVenues 
            INNER JOIN tblVenueStatus sta ON sta.venuId = dbo.vwVenues.HospitalID
            GROUP BY HospitalID, categoryId
        ) topStatus 
        INNER JOIN tblVenueStatus sta2 ON sta2.dateAdded = topStatus.DateAdded AND sta2.venuId = topStatus.HospitalID) Statuss 
        FULL OUTER JOIN vwVenues vwV ON vwV.HospitalID = Statuss.HospitalID 
        LEFT OUTER JOIN tblVenueStatus vStatus ON vStatus.id = Statuss.id 
        LEFT OUTER JOIN tblVenueStatusName vStatusName ON vStatusName.id = vStatus.statusNameId 
        LEFT OUTER JOIN tblVenueStatusCategories vStatusCategories ON vStatus.categoryId = vStatusCategories.id AND vStatusName.id = vStatus.statusNameId 
        LEFT OUTER JOIN tblVenueStatusNotes vStatusNotes ON vStatusNotes.id = vStatus.noteId
) AS s     
PIVOT (MIN(StatusName) 
FOR [CategoryName] IN ([Category1],[Category2])) AS PTbl
于 2013-05-22T13:39:04.803 回答