Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4609082
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:56:11+00:00 2026-05-22T00:56:11+00:00

I need to join a table on itself while joining other tables and doing

  • 0

I need to join a table on itself while joining other tables and doing count calculations on them. I’m using SQL Server 2008 R2 by the way.

Current State – I have three tables MergedSessionsID, EventsSeparated and EventsDescriptions.

MergedSessionsID, this is where I manipulate the data – the sessions essentially by specifying the Company Name and Session Time (see below for example of data).

EventsSeparated table links up with MergedSessionsID. Basically a Session can have one or many Events.

EventsDescription table contains the description of the event, e.g. this and that happend to the user.

My goal here is to understand the total number of events that happened for a certain period of time. Exhibit 1 is the result of running the query below.

However I also want to compare the old data to new data and see how the number of events changed. So as in Exhibit 2, this is what I imagine the view would like – showing the PreCounter for data in March, 2210 and PostCounter for data in April 2210.

What is the best way to implement this?

SELECT E.[Event]
      ,COUNT(1) AS Counter
      ,ED.[Event Description]
FROM [ShipCompanies].[dbo].MergedSessionsID AS S
    INNER JOIN [ShipCompanies].dbo.EventsSeparated AS E
        ON S.ID = E.ID
    INNER JOIN [ShipCompanies].dbo.EventDescription AS ED
        ON E.[Event] = ED.[Unique ID]
WHERE S.Company = 'SuperSpaceShipOne'
  AND S.[Session Time] BETWEEN '2210-03-02' AND '2210-03-05'
GROUP BY E.[Event], ED.[Event Description]
ORDER BY E.[Event]


SELECT * FROM MergedSessionsID
ID   Company             Session Time
1   SuperSpaceShipOne   2210-03-05
2   SuperSpaceShipOne   2210-03-03 
3   SuperSpaceShipOne   2210-03-02 
4   SuperSpaceShipOne   2210-04-01
5   SuperSpaceShipOne   2210-04-02
6   SuperSpaceShipOne   2210-04-03


SELECT * FROM EventsSeparated
ID  Event
1   2000
2   2001
2   2002
3   2001
4   2002
5   2002
6   2002

SELECT * FROM EventDescription
Unique ID   Event Description
2000        'User Entered the Pod'
2001        'User Initiated Launching Sequence'
2002        'User Error encountered'

Exhibit 1. Merged Output Summary Table (What I have now)

Event   Counter  Event Description
2000    1        'User Entered the Pod'
2001    2        'User Initiated Launching Sequence'
2002    1        'User Error encountered'

Exhibit 2. Merged Output Summary Table (What I want to get to)

Event  PreCounter   Event Description                  PostCounter
2000   1            'User Entered the Pod'              0
2001   2            'User Initiated Launching Sequence' 0
2002   1            'User Error encountered'            3
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:56:11+00:00Added an answer on May 22, 2026 at 12:56 am
    Select E.[Event]
        , Sum( Case 
               When S.[Session Time] Between '2210-03-02' And '2210-03-05' Then 1 
               Else 0 End ) As PreCounter
        , Sum( Case 
               When S.[Session Time] Between '2210-04-02' And '2210-04-05' Then 1 
               Else 0 End ) As PostCounter
          , ED.[Event Description]
    From [ShipCompanies].[dbo].MergedSessionsID As S
        Join [ShipCompanies].dbo.EventsSeparated As E
            On S.ID = E.ID
        Join [ShipCompanies].dbo.EventDescription As ED
            On E.[Event] = ED.[Unique ID]
    Where S.Company = 'SuperSpaceShipOne'
        And (
            S.[Session Time] Between '2210-03-02' And '2210-03-05'
            Or S.[Session Time] Between '2210-04-02' And '2210-04-05'
            )
    Group By E.[Event], ED.[Event Description]
    Order By E.[Event]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got several function where I need to do a one-to-many join, using count(),
I need to do a join with a table/result-set/whatever that has the integers n
i dont remember how to join a table to itself.. my table is: |
I am trying to write a SQL query where I am joining two tables
given the first table ,i need to join this table with the same table
I need to join two binary files with a *.bat script on Windows. How
I have huge number of Word files I need to merge (join) into one
I need to do a join across two different database servers (IPs 10.0.0.50 and
I need to do a LINQ2DataSet query that does a join on more than
I'm trying to build a navigation system using categories table with hierarchies. Normally, the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.