I have 2 tables, a calendar table and a vacation table. In the calendar table there is a date column and a slotsFull column. The slotsFull column represents how many people have taken a vacation for that particular time. In the vacation table there is a vacStart and a vacStop column that denotes when the vacation starts and stops.
I am trying to rewrite a procedure called updateSlots that will count the number of rows in the vacation table that span each time in the calendar and set the slotsFull column to that number. A version of this code was written by U.R. already that uses a subquery to update the data and it works correctly:
It provides the following results, which is correct:
Now... I can recreate the desired result set by adding a tinyint column to the vacation table to have a column to sum up the values and running the following query:
However, this brings up 2 issues. I'm running into an error that says "An aggregate may not appear in the set list of an UPDATE statement." when I try to run the following update:
So I have 2 questions. Can this update be ran using a join instead of the subquery? And if so, will I need to add the blah column to form a basis for the sum - or should I use a cast operator on data that already exists in the table?
Or, did U.R. already write the most efficient solution? I'm guessing that (as usual) this is not the case.
-kaht
Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
I am trying to rewrite a procedure called updateSlots that will count the number of rows in the vacation table that span each time in the calendar and set the slotsFull column to that number. A version of this code was written by U.R. already that uses a subquery to update the data and it works correctly:
Code:
[COLOR=blue]create[/color] [COLOR=blue]table[/color] #calendar (vacDate [COLOR=blue]smalldatetime[/color], slotsFull [COLOR=blue]tinyint[/color])
[COLOR=blue]set[/color] [COLOR=#FF00FF]nocount[/color] [COLOR=blue]on[/color]
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #calendar [COLOR=blue]select[/color] [COLOR=red]'2007-01-01 08:00'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #calendar [COLOR=blue]select[/color] [COLOR=red]'2007-01-01 08:15'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #calendar [COLOR=blue]select[/color] [COLOR=red]'2007-01-01 08:30'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #calendar [COLOR=blue]select[/color] [COLOR=red]'2007-01-01 08:45'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #calendar [COLOR=blue]select[/color] [COLOR=red]'2007-01-02 08:00'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #calendar [COLOR=blue]select[/color] [COLOR=red]'2007-01-02 08:15'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #calendar [COLOR=blue]select[/color] [COLOR=red]'2007-01-02 08:30'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #calendar [COLOR=blue]select[/color] [COLOR=red]'2007-01-02 08:45'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #calendar [COLOR=blue]select[/color] [COLOR=red]'2007-01-03 08:00'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #calendar [COLOR=blue]select[/color] [COLOR=red]'2007-01-03 08:15'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #calendar [COLOR=blue]select[/color] [COLOR=red]'2007-01-03 08:30'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #calendar [COLOR=blue]select[/color] [COLOR=red]'2007-01-03 08:45'[/color], 0
[COLOR=blue]create[/color] [COLOR=blue]table[/color] #vacation (vacStart [COLOR=blue]smalldatetime[/color], vacStop [COLOR=blue]smalldatetime[/color])
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #vacation [COLOR=blue]select[/color] [COLOR=red]'2007-01-01 08:00'[/color], [COLOR=red]'2007-01-01 09:00'[/color]
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #vacation [COLOR=blue]select[/color] [COLOR=red]'2007-01-01 08:00'[/color], [COLOR=red]'2007-01-01 08:30'[/color]
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #vacation [COLOR=blue]select[/color] [COLOR=red]'2007-01-02 08:30'[/color], [COLOR=red]'2007-01-02 08:45'[/color]
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #vacation [COLOR=blue]select[/color] [COLOR=red]'2007-01-03 08:15'[/color], [COLOR=red]'2007-01-03 08:45'[/color]
[COLOR=green]--update using crappy subquery
[/color][COLOR=blue]update[/color] #calendar
[COLOR=blue]set[/color] slotsFull = (
[COLOR=blue]select[/color] [COLOR=#FF00FF]count[/color](*)
[COLOR=blue]from[/color] #vacation
[COLOR=blue]where[/color] vacStart <= #calendar.vacDate
and vacStop > #calendar.vacDate
)
[COLOR=green]--display results
[/color][COLOR=blue]select[/color] * [COLOR=blue]from[/color] #calendar
[COLOR=blue]drop[/color] [COLOR=blue]table[/color] #calendar
[COLOR=blue]drop[/color] [COLOR=blue]table[/color] #vacation
It provides the following results, which is correct:
Code:
vacDate slotsFull
------------------------------------------------------ ---------
2007-01-01 08:00:00 2
2007-01-01 08:15:00 2
2007-01-01 08:30:00 1
2007-01-01 08:45:00 1
2007-01-02 08:00:00 0
2007-01-02 08:15:00 0
2007-01-02 08:30:00 1
2007-01-02 08:45:00 0
2007-01-03 08:00:00 0
2007-01-03 08:15:00 1
2007-01-03 08:30:00 1
2007-01-03 08:45:00 0
Now... I can recreate the desired result set by adding a tinyint column to the vacation table to have a column to sum up the values and running the following query:
Code:
[COLOR=blue]create[/color] [COLOR=blue]table[/color] #vacation (vacStart [COLOR=blue]smalldatetime[/color], vacStop [COLOR=blue]smalldatetime[/color], blah [COLOR=blue]tinyint[/color])
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #vacation [COLOR=blue]select[/color] [COLOR=red]'2007-01-01 08:00'[/color], [COLOR=red]'2007-01-01 09:00'[/color], 1
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #vacation [COLOR=blue]select[/color] [COLOR=red]'2007-01-01 08:00'[/color], [COLOR=red]'2007-01-01 08:30'[/color], 1
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #vacation [COLOR=blue]select[/color] [COLOR=red]'2007-01-02 08:30'[/color], [COLOR=red]'2007-01-02 08:45'[/color], 1
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #vacation [COLOR=blue]select[/color] [COLOR=red]'2007-01-03 08:15'[/color], [COLOR=red]'2007-01-03 08:45'[/color], 1
[COLOR=blue]select[/color] c.vacDate, sum([COLOR=#FF00FF]coalesce[/color](v.blah, 0))
[COLOR=blue]from[/color] #calendar c
[COLOR=#FF00FF]left[/color] [COLOR=blue]join[/color] #vacation v
[COLOR=blue]on[/color] v.vacStart <= c.vacDate
and v.vacStop > c.vacDate
[COLOR=blue]group[/color] [COLOR=blue]by[/color] c.vacDate
However, this brings up 2 issues. I'm running into an error that says "An aggregate may not appear in the set list of an UPDATE statement." when I try to run the following update:
Code:
[COLOR=blue]update[/color] #calendar
[COLOR=blue]set[/color] vacDate = sum([COLOR=#FF00FF]coalesce[/color](v.blah, 0))
[COLOR=blue]from[/color] #calendar c
[COLOR=blue]inner[/color] [COLOR=blue]join[/color] #vacation v
[COLOR=blue]on[/color] v.vacStart <= c.vacDate
and v.vacStop > c.vacDate
[COLOR=blue]group[/color] [COLOR=blue]by[/color] c.vacDate
So I have 2 questions. Can this update be ran using a join instead of the subquery? And if so, will I need to add the blah column to form a basis for the sum - or should I use a cast operator on data that already exists in the table?
Or, did U.R. already write the most efficient solution? I'm guessing that (as usual) this is not the case.
-kaht
Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson