简体   繁体   English

使用多个表并使用多个SUM函数的SQL

[英]SQL using multiple tables and using several SUM functions

I have looked over several posts that are relative but I cannot figure out how to apply them to my situation. 我查看了一些相对的帖子,但是我不知道如何将它们应用于我的情况。

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([LeisureActivities].[dbo].[tempbookings].[tempPlaces]) 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];

As you can see this refers to 3 tables, activities , bookings and tempbookings . 如您所见,这是指3个表, activitiesbookingstempbookings The tempbookings table also contains a tempReservedDate field which contains a datetime datatype, I need my SUM calculation for tempPlaces only to SUM those records that have a tempReservedDate in the future, those in the past just ignore (similar to how I'm already controlling the activities by displaying only those with a future date) the SUM also needs to group these by each activity. tempbookings表还包含一个tempReservedDate字段,该字段包含一个datetime数据类型,我需要对tempPlaces进行SUM计算,以便仅对那些将来具有tempReservedDate记录进行tempReservedDate ,而过去的记录则忽略(类似于我已经控制了通过仅显示具有未来日期的活动),SUM还需要按每个活动对它们进行分组。

I believe I need to put in my "WHERE" section something like 我相信我需要在“ WHERE”部分添加类似

[LeisureActivities].[dbo].[tempbookings].[tempReservedDate] > GetDate ()

I just don't know where to put it to make it work without applying that to my whole query as it only needs to apply to the tempPlaces . 我只是不知道在不将其应用于整个查询的情况下将其放置在哪里,因为它仅需要应用于tempPlaces

I hope I've managed to explain what I mean but please ask if it doesn't make sense, although I am aware that often you wouldn't format it how I have (with the [] etc.) but for the program that uses these queries it has to be done in this particular format. 我希望我已经能够解释我的意思了,但是请问这是否没有道理,尽管我知道通常您不会格式化它的格式(使用[]等),而是使用该程序使用这些查询,必须以这种特定格式进行。

Any help appreciated, thanks. 任何帮助表示赞赏,谢谢。

You need a conditional sum in the select clause: select子句中需要一个条件和:

SUM(case when [tempbookings].[tempReservedDate] > GetDate()
                then tempBookings.tempPlaces
           end) as FutureTempPlaces

Also, you should learn how to better format your code and use sensible aliases (such as tb for tempBookings ). 此外,您还应该学习如何更好地格式化代码并使用明智的别名(例如tempBookings tb )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM