0

我在下面的查询是将它返回的最后一条记录的金额加倍。我有 3 张桌子 - 活动、预订和临时预订。查询需要列出活动和附加信息,并从每个活动的预订表中提取预订位置的总数(使用 SUM)(作为 BookingTotal),然后它需要计算相同的临时预订(作为 tempPlacesReserved)提供该表中的reservedate 字段是将来的。

但是第一个问题是,如果 tempbookings 表中没有活动记录,它根本不会返回该活动的任何记录,为了解决这个问题,我过去创建了虚拟记录,以便它仍然返回记录,但是如果我能做到,所以我不必这样做,我会更喜欢它!

我遇到的主要问题是,在返回结果的最终记录中,它使预订总数和保留的位置翻了一番,这当然使整个查询无用。

我知道我做错了什么我只是无法对其进行排序,我在网上搜索了类似的问题,但无法正确地将它们应用于我的情况。

任何帮助,将不胜感激。

PS我知道通常你不需要像我一样完全标记到数据库、表和字段的所有路径,但是对于我计划在其中使用它的程序,我必须这样做。

代码:

SELECT [LeisureActivities].[dbo].[activities].[activityID], 
    [LeisureActivities].[dbo].[activities].[activityName], 
    [LeisureActivities].[dbo].[activities].[activityDate], 
    [LeisureActivities].[dbo].[activities].[activityPlaces], 
    [LeisureActivities].[dbo].[activities].[activityPrice], 
    SUM([LeisureActivities].[dbo].[bookings].[bookingPlaces]) AS 'bookingTotal', 
    SUM (CASE WHEN[LeisureActivities].[dbo].[tempbookings].[tempReserveDate] > GetDate() THEN [LeisureActivities].[dbo].[tempbookings].[tempPlaces] ELSE 0 end) AS 'tempPlacesReserved'
FROM [LeisureActivities].[dbo].[activities], 
    [LeisureActivities].[dbo].[bookings], 
    [LeisureActivities].[dbo].[tempbookings]
WHERE ([LeisureActivities].[dbo].[activities].[activityID]=[LeisureActivities].[dbo].[bookings].[activityID] 
    AND [LeisureActivities].[dbo].[activities].[activityID]=[LeisureActivities].[dbo].[tempbookings].[tempActivityID]) 
    AND [LeisureActivities].[dbo].[activities].[activityDate] > GetDate ()
GROUP BY [LeisureActivities].[dbo].[activities].[activityID], 
    [LeisureActivities].[dbo].[activities].[activityName], 
    [LeisureActivities].[dbo].[activities].[activityDate],
     [LeisureActivities].[dbo].[activities].[activityPlaces], 
    [LeisureActivities].[dbo].[activities].[activityPrice];
4

2 回答 2

1

您当前的查询INNER JOIN在每个表之间使用一个,因此如果tempBookings表没有记录,您将不会返回任何内容。

我建议你开始使用JOIN语法。您可能还需要使用子查询来获取总数。

SELECT a.[activityID], 
    a.[activityName], 
    a.[activityDate], 
    a.[activityPlaces], 
    a.[activityPrice], 
    coalesce(b.bookingTotal, 0) bookingTotal, 
    coalesce(t.tempPlacesReserved, 0) tempPlacesReserved
FROM [LeisureActivities].[dbo].[activities] a
LEFT JOIN
(
  select activityID,
    SUM([bookingPlaces]) AS bookingTotal
  from [LeisureActivities].[dbo].[bookings]
  group by activityID
) b
  ON a.[activityID]=b.[activityID] 
LEFT JOIN
(
  select tempActivityID,
    SUM(CASE WHEN [tempReserveDate] > GetDate() THEN [tempPlaces] ELSE 0 end) AS tempPlacesReserved
  from [LeisureActivities].[dbo].[tempbookings]
  group by tempActivityID
) t
  ON a.[activityID]=t.[tempActivityID]
WHERE a.[activityDate] > GetDate();

注意:我使用别名是因为它更容易阅读

于 2013-03-21T17:54:50.607 回答
0

Use new SQL-92 Join syntax, and make join to tempBookings an outer join. Also clean up your sql with table aliases. Makes it easier to read. As to why last row has doubled values, I don't know, but on off chance that it is caused by extra dummy records you entered. get rid of them. That problem is fixed by using outer join to tempBookings. The other possibility is that the join conditions you had to the tempBookings table(t.tempActivityID = a.activityID) is insufficient to guarantee that it will match to only one record in activities table... If, for example, it matches to two records in activities, then the rows from Tempbookings would be repeated twice in the output, (causing the sum to be doubled)

SELECT a.activityID, a.activityName, a.activityDate, 
       a.activityPlaces, a.activityPrice, 
       SUM(b.bookingPlaces) bookingTotal, 
       SUM (CASE WHEN t.tempReserveDate > GetDate() 
                 THEN t.tempPlaces ELSE 0 end) tempPlacesReserved
FROM LeisureActivities.dbo.activities a
    Join LeisureActivities.dbo.bookings b
       On b.activityID = a.activityID
    Left Join LeisureActivities.dbo.tempbookings t
       On t.tempActivityID = a.activityID
WHERE a.activityDate > GetDate ()
GROUP BY a.activityID, a.activityName, 
    a.activityDate, a.activityPlaces, 
    a.activityPrice;
于 2013-03-21T17:56:12.853 回答