Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Server group by / count issue
    primarykey
    data
    text
    <p>I'm trying to count Holiday bookings (B.ID) for dates 2 days either side of today.</p> <p>It works but my results are separated as I have to introduce the end date of the holiday too, which varies for each start date (holidays have different durations).</p> <p>The separates out my counts. What I need is one count for each date. Is there a way of working round this? I kinda just want to exclude the vwReturnDate from the group by but have to put it there as I've used it in my count.</p> <p>In English I want - For each [date] count the number of [B.id] where [B.Depart] &lt;= [date] and [vwReturnDate] > [date]</p> <p><img src="https://i.stack.imgur.com/eUvqv.png" alt="enter image description here"></p> <pre><code>DECLARE @startDate DATE DECLARE @endDate DATE SET @startDate = Getdate()-2 SET @endDate = Getdate()+2; WITH dates(Date) AS ( SELECT @startdate as Date UNION ALL SELECT DATEADD(d,1,[Date]) FROM dates WHERE DATE &lt; @enddate ) SELECT [Date] as 'Calendar Date', --CONVERT(VARCHAR(10), [Date],103) AS 'Date' -- ,CONVERT(CHAR(2), [Date], 113) AS 'Day' -- ,CONVERT(CHAR(4), [Date], 100) AS 'Month' -- ,CONVERT(CHAR(4), [Date], 120) AS 'Year', Case when B.Depart &lt;= [date] AND vwR.ReturnDate &gt;=[date] then count (B.ID) end AS 'Number of holidays live on date' FROM [dates] left join Booking B on B.depart=[Date] inner join Quote Q on Q.ID=B.QuoteID inner join vwReturnDate vwR on vwR.ID=B.ID Group by [date], B.depart, vwR.ReturnDate order by [date] OPTION (MAXRECURSION 0) GO </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload