开发者

Cross Joining a Table on itself with Count

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].Merged开发者_如何学JAVASessionsID 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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜