Share via

Help with query construction

Darryl Hoar 201 Reputation points
2024-06-27T21:40:07.0166667+00:00

Need some help with query construction.

database has the following table

table name events

columns: location type int, edate type date, user type varchar, visits type small int.

example records look like

1 2024-01-01 'Bob' 3

1 2024-01-01 'Mary' 13

2 2024-06-01 'Joe' 2

2 2024-06-07 'Joe' 8

I need to sum the visits for a specific location,edate. so the query should return

2024-01-01 16

2024-06-01 2

2024-06-07 8

I tried Select location,edate,sum(visits) from events group by location,edate,visits

but I don't think that's correct.

thanks for any guidance.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

2 answers

Sort by: Most helpful
  1. LiHongMSFT-4306 31,621 Reputation points
    2024-06-28T01:17:38.2666667+00:00

    Hi @Darryl Hoar

    I need to sum the visits for a specific location,edate.

    Then you just need to Group By these two specificlocation and edate. As answered above, just remove the visits from the Group By clause.

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Was this answer helpful?

    0 comments No comments

  2. Erland Sommarskog 134.3K Reputation points MVP Volunteer Moderator
    2024-06-27T22:00:46.06+00:00

    I tried Select location,edate,sum(visits) from events group by location,edate,visits

    Remove visits from GROUP BY.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.