DO I need a self-join?

Last post 06-21-2008, 8:55 AM by abev. 4 replies.
Sort Posts: Previous Next
  •  06-18-2008, 8:23 AM Post number 60044

    • abev is not online. Last active: 06-25-2008, 8:00 AM abev
    • Top 150 Contributor
    • Joined on 08-23-2007
    • Level 1: Deep thought

    DO I need a self-join?

    This is a fairly new concept to me. I am not sure if the problem I have is possible with tsql, but here's what I need.

    I have a table that stores game transactions (one transaction per game played) with 3 columns (for brevity)

    TransactionID, gameID, transdateandtime

    What I would like to do is get an output grouped by day, hour but having the sum of the game (count) in different columns for each game, like this:

    Month   Hour   PlaysGame1   PlaysGame2   PlaysGame3 .....
    3            11         15                     25                  10

    Is there a way to do this?

    I have the tsql that gives the the results I want, however the only way I know how to present the data is to group by is month, hour, game so it presents in three columns total. I need to move the count of games to separate columns. I would like to be able to compare the game transactions side by side.
  •  06-19-2008, 2:44 AM Post number 60121 in reply to post number 60044

    Re: DO I need a self-join?

    Yes, this is very simple to do in TSQL. You must group by day and hour. All other columns are all derived by using a SUM(CASE......END). The biggest labour in answering these questions is to have the build statement and the sample data (only a few lines) so one can test the answer out. So, if you are not sure how to do this, post the sample table and data and I'll do a worked example.

    I answered a similar question here http://www.simple-talk.com/community/forums/thread/59577.aspx . With any luck, that will show you how to do it!
  •  06-20-2008, 8:32 AM Post number 60193 in reply to post number 60121

    • abev is not online. Last active: 06-25-2008, 8:00 AM abev
    • Top 150 Contributor
    • Joined on 08-23-2007
    • Level 1: Deep thought

    Re: DO I need a self-join?

    Thanks, Phil. I think that gets me where I am going. I am going to play around with that today and see what happens. If I need you again (for this problem) I will post some sample data.

    I think my biggest problem with this type of idea is I just don't fully understand what SQL is doing. I need to find a way to picture what the SQL engine is doing. Am I being clear? I can read vb.net because it is laid out in more of a step by step fashion, but complex sql statements confuse me. I am not even sure how to search Google for this - any thoughts where I can go?
  •  06-20-2008, 11:43 AM Post number 60210 in reply to post number 60193

    Re: DO I need a self-join?

    You will never really see what SQL Server is actually doing because you're not involved in that process. You tell it what you want and SQL Server chucks it back at you. It is a very tricky idea to get across to a vb programmer!
    The only way to really understand group-by  statements is by trying them out in all their variety and just seeing what happens. Suddenly... Ping!!! You will have understood what aggregation really means and you'll be airborne.
  •  06-21-2008, 8:55 AM Post number 60299 in reply to post number 60210

    • abev is not online. Last active: 06-25-2008, 8:00 AM abev
    • Top 150 Contributor
    • Joined on 08-23-2007
    • Level 1: Deep thought

    Re: DO I need a self-join?

    Phil, It works like a charm and was relatively simple (after copying from your sample:)).

    SELECT
    'Hour' = DATEPART(hh, DATEADD(hh, -4, [Date AND Time])),
    'Month' = DATEPART(m, DATEADD(hh, -4, [Date AND Time])),
    'YEAR' = DATEPART(yy, DATEADD(hh, -4, [Date AND Time])),
    'Cage 1'= Sum (Case WHEN Tag=4157 THEN 1 ELSE 0 end),
    'Cage 2'= Sum (Case WHEN Tag=4158 THEN 1 ELSE 0 end),
    'Cage 3'= Sum (Case WHEN Tag=4159 THEN 1 ELSE 0 end),
    'Cage 4'= Sum (Case WHEN Tag=4160 THEN 1 ELSE 0 end),
    'Cage 5'= Sum (Case WHEN Tag=4161 THEN 1 ELSE 0 end),
    'Cage 6'= Sum (Case WHEN Tag=4162 THEN 1 ELSE 0 end),
    'Cage 7'= Sum (Case WHEN Tag=4163 THEN 1 ELSE 0 end),
    'Cage 8'= Sum (Case WHEN Tag=4164 THEN 1 ELSE 0 end),
    'Total' = count(*)
    FROM DC_Transaction
    WHERE  Tag > 4100
    GROUP BY    DATEPART(hh, DATEADD(hh, -4, [Date AND Time])), DATEPART(m, DATEADD(hh, -4, [Date AND Time])), DATEPART(yy, DATEADD(hh, -4, [Date AND Time]))
    WITH rollup

    One thing I was wondering though. If I wanted to:

     Coalesce('Hour' = DATEPART(hh, DATEADD(hh, -4, [Date AND Time])), 'Hour Total')

    SQL does not recognize the [Date And Time] when I Group By it. Meaning if I use that Coalesce function with the embedded Datepart and Dateadds, Sql thinks that I was not used the Date ANd Time column in the select list. Is there a way for SQL to recognize the column used within a coalesce when its buried in the function?
View as RSS news feed in XML