0

我有以下数据库设计:

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
4

2 回答 2

2
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
于 2012-08-14T06:28:40.990 回答
0
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

试试这个查询。

于 2012-08-14T06:31:22.620 回答