选择与另一个表的foreach值不同

时间:2013-05-22 07:02:35

标签: sql-server

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

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

一般的想法可能是,对于商店中的每件商品,都有一个状态。因此,对于商店A中的黄瓜字母 tomato ,状态将分别为好坏。对于商店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

1 个答案:

答案 0 :(得分:1)

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

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