locked
Problems with SQL query RRS feed

  • Question

  • User-1320437544 posted

    Hello everyone,

    I am having and issue with query I am trying to run any help would be greatly appreciated. So here is the query:

    Declare @FromDate datetime = '2018-01-01';
    Declare @ToDate datetime = '2018-12-12';

    Select u.RealName, r.Marketing, u.Email, (Select EntityName From dbo.Entities Where ID = r.EntityID) As Entity From dbo.Reservations As r Inner Join dbo.Users As u On u.ID = r.Marketing  Where r.TripDate >= @FromDate And r.TripDate <= @ToDate And r.Canceled = 0 Group By r.Marketing;

    What I am trying to do is to get the totals Count(*) of reservations by User Where Marketing is the User's ID from the Users table and grouped by User ID but distinct by EntityID.

    The error  I am getting:  Column 'dbo.Users.RealName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

    Monday, September 3, 2018 8:02 PM

Answers

  • User-1320437544 posted

    Anyhow I've figured it out myself with small modification. Here it is anyway hopefully could help someone else.

    Select Count(*) As Totals, u.RealName, r.Marketing, u.Email From dbo.Reservations As r Inner Join dbo.Users As u On u.ID = r.Marketing
    Where r.TripDate >= @FromDate And r.TripDate <= @ToDate And r.Canceled = 0 Group By r.Marketing, u.RealName, u.Email Order By RealName;

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 3, 2018 8:13 PM