Sql server 2008 r2 如何修改此查询以显示每个活动中的座位数?

Sql server 2008 r2 如何修改此查询以显示每个活动中的座位数?,sql-server-2008-r2,Sql Server 2008 R2,我有以下数据库设计: Users Table: NetworkID, Name, BadgeNo, DepartmentCode Events Table: ID, Title, Description, Location, StartDateTime, EndDateTime, NumberOfSeats, IsActive BookingDetails Table: BookingID, EventID, NetworkID 我需要提出一个查询,显示每个活动的座位数、预订数和剩余(或可用)

我有以下数据库设计:

Users Table: NetworkID, Name, BadgeNo, DepartmentCode
Events Table: ID, Title, Description, Location, StartDateTime, EndDateTime, NumberOfSeats, IsActive
BookingDetails Table: BookingID, EventID, NetworkID
我需要提出一个查询,显示每个活动的座位数、预订数和剩余(或可用)座位数。我写了一个查询,显示了座位数量和预订数量。现在,我需要找到剩余(或可用)座位的数量,它等于(座位数量-预订数量)那么您能帮我修改一下以找到这个要求吗?

我的问题是:

SELECT     dbo.Events.Title, dbo.Events.Description, dbo.Events.Location, dbo.Events.NumberOfSeats, COUNT(DISTINCT dbo.BookingDetails.NetworkID) 
                      AS [Number of Bookings]
FROM         dbo.BookingDetails INNER JOIN
                      dbo.Users ON dbo.BookingDetails.NetworkID = dbo.Users.NetworkID RIGHT OUTER JOIN
                      dbo.Events ON dbo.BookingDetails.EventID = dbo.Events.ID
WHERE     (dbo.Events.IsActive = 1)
GROUP BY dbo.Events.Title, dbo.Events.Description, dbo.Events.Location, dbo.Events.NumberOfSeats

尝试此查询。

您已经有了座位数和预订数-只需减去这两个数字即可。谢谢您的帮助。我真的很感激。
SELECT     dbo.Events.Title, dbo.Events.Description, dbo.Events.Location,
           dbo.Events.NumberOfSeats, COUNT(DISTINCT dbo.BookingDetails.NetworkID) AS [Number of Bookings],
           dbo.Events.NumberOfSeats - COUNT(DISTINCT dbo.BookingDetails.NetworkID) AS RemainingSeats
FROM         dbo.BookingDetails INNER JOIN
                      dbo.Users ON dbo.BookingDetails.NetworkID = dbo.Users.NetworkID RIGHT OUTER JOIN
                      dbo.Events ON dbo.BookingDetails.EventID = dbo.Events.ID
WHERE     (dbo.Events.IsActive = 1)
GROUP BY dbo.Events.Title, dbo.Events.Description, dbo.Events.Location, dbo.Events.NumberOfSeats
SELECT result.t, result.d, result.l, result.c, result.r, result.c-result.r
 FROM (SELECT     dbo.Events.Title t, dbo.Events.Description d, dbo.Events.Location l, dbo.Events.NumberOfSeats c, COUNT(DISTINCT dbo.BookingDetails.NetworkID) r                       AS [Number of Bookings] FROM         dbo.BookingDetails INNER JOIN                       dbo.Users ON dbo.BookingDetails.NetworkID = dbo.Users.NetworkID RIGHT OUTER JOIN                       dbo.Events ON dbo.BookingDetails.EventID = dbo.Events.ID WHERE     (dbo.Events.IsActive = 1) GROUP BY dbo.Events.Title, dbo.Events.Description, dbo.Events.Location, dbo.Events.NumberOfSeats) result