Basic JOIN Question

Alastair MacFarlane 26 Reputation points
2022-05-13T09:12:43.467+00:00

Hi,

Good morning. I am trying to join 2 sub-selects with table aliases but keep getting errors at the GROUP BY part of the code. I have slightly redacted the inner workings of the sub-selects.

Msg 156, Level 15, State 1, Line 63
Incorrect syntax near the keyword 'GROUP'.

I am trying to JOIN 2 sub selects

ON UsrActivity.UserID = UserAccList.UserID
ON UsrActivity.SiteID = UserAccList.SiteID

but I cannot get it to work. I feel there is an easy answer here but I am confused.

Thanks for any assistance.

Alastair

SELECT
UserAccList.SiteID,
UserAccList.Name,
UserAccList.UserID,
Count(UserAccList.Name) As TotalLogins,
FROM
(
SELECT
Evt1.Name,
Evt1.UserID,
Evt1.SiteID
FROM
dbo.eventlog1 As Evt1
) As UserAccList
INNER JOIN
(
SELECT
Evt2.Name,
Evt2.UserID,
Evt2.SiteID
FROM
dbo.eventlog2 As Evt2
) As UsrActivity
INNER JOIN UserAccList ON UsrActivity.UserID = UserAccList.UserID
INNER JOIN UserAccList ON UsrActivity.SiteID = UserAccList.SiteID
GROUP BY
UserAccList.SiteID,
UserAccList.UserID,
UserAccList.Name
ORDER BY
UserAccList.Name ASC

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Bert Zhou-msft 3,516 Reputation points
    2022-05-13T09:23:56.853+00:00

    Hi,@Alastair MacFarlane

    Welcome to Microsoft T-SQL Q&A Forum!

    Try this:

     ;With cte as  
     (  
      SELECT Name, UserID, SiteID FROM dbo.eventlog1   
      intersect   
      SELECT Name, UserID, SiteID FROM dbo.eventlog2   
     )  
     select * from cte  
     group by  SiteID, UserID, Name  
     order by Name asc  
    

    IF you want to know more about the usage of intersect,you can look this document.

    Best regards,
    Bert Zhou


    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".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


3 additional answers

Sort by: Most helpful
  1. Alastair MacFarlane 26 Reputation points
    2022-05-13T09:23:22.193+00:00

    Apologies I think I see an issue. I have an INNER JOIN between the sub-selects. What is the best way to code this?


  2. Alastair MacFarlane 26 Reputation points
    2022-05-13T09:25:12.33+00:00

    If I change it to:

    ) UsrActivity

    ON UsrActivity.UserID = UserAccList.UserID
    INNER JOIN UserAccList
    ON UsrActivity.SiteID = UserAccList.SiteID

    I get:

    Msg 208, Level 16, State 1, Line 1
    Invalid object name 'UserAccList'.

    0 comments No comments

  3. Jingyang Li 5,901 Reputation points Volunteer Moderator
    2022-05-13T13:20:44.667+00:00

    SELECT SiteID,Name,UserID,Count(*) As TotalLogins
    FROM
    (
    Select
    SELECT Name, UserID, SiteID FROM dbo.eventlog1
    intersect
    SELECT Name, UserID, SiteID FROM dbo.eventlog2
    ) t

    GROUP BY SiteID, UserID, Name
    ORDER BY Name

    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.