Combining Query Results

  • Hi guys,

    I have a table with fields "To User", "From User" and others (which I'm not going to use at this time).

    Basically this is a messages table that stores when messages have come from some particular user to another particular user, and it allows reversals.

    So data may look like

    From User To User

    user1 user2

    user2 user1

    user1 user2

    user1 user3

    user3 user1

    As you can see it allows duplication (theres no primary key) simply because people can send multiple messages to one another.

    I'm trying to write a query that will list all of the possible pairs of members (but combines the reversals!!!) and the number of messages they have sent to each other.

    I wrote a simple query:

    SELECT [From User], [To User], Count(*) AS "Count"

    FROM Messages

    GROUP BY [From User], [To User]

    ORDER BY "Count"

    And this query works, but of course it is returning unique results, so where you have

    "From User" user1 "To User" user2, it will list separately from the opposite which is "From User" user2 "To User" user1.

    But yeah, anyone know if there is a way to combine those results and hence show the TOTAL number of messages passed between user1 and user2, user1 and user3, etc (all the possible pairings)?

    I hope my description wasn't too confusing, its a relatively simple idea.

    As always, any help is appreciated.

  • Sure...

    [font="Courier New"]--=====&nbspThis&nbspis&nbspjust&nbspto&nbspbuild&nbspsome&nbspsample&nbspdata&nbspand&nbspis&nbspNOT&nbsppart&nbspof&nbspthe&nbspsolution

    DECLARE&nbsp@yourtable&nbspTABLE&nbsp(FromUser&nbspVARCHAR(10),&nbspToUser&nbspVARCHAR(10))

    &nbspINSERT&nbspINTO&nbsp@yourtable&nbsp(FromUser,&nbspToUser)

    &nbspSELECT&nbsp'user1','user2'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'user2','user1'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'user1','user2'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'user1','user3'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'user3','user1'

    --=====&nbspThis&nbspis&nbspthe&nbspsolution

    &nbspSELECT&nbspd.FromUser,&nbspd.ToUser,&nbspCOUNT(*)&nbspAS&nbspTimeConnected

    &nbsp&nbsp&nbspFROM&nbsp(--====&nbspDerived&nbsptable&nbspswaps&nbspusers&nbspwhen&nbspneeded

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSELECT&nbspFromUser,&nbspToUser

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspFROM&nbsp@yourtable

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWHERE&nbspFromUser&nbsp<&nbspToUser

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspUNION&nbspALL

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSELECT&nbspToUser&nbspAS&nbspFromUser,

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspFromUser&nbspAS&nbspToUser

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspFROM&nbsp@yourtable

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWHERE&nbspToUser&nbsp<=&nbspFromUser

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp)&nbspd

    &nbsp&nbspGROUP&nbspBY&nbspd.FromUser,&nbspd.ToUser[/font]

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Awesome, that works as far as I can tell, thank you very much Jeff!

  • Here's an extended problem (which I was unable to solve and not sure if it can be at all).

    So Jeff's code works, but another field that I have in the Messages table is "Time Sent" which obviously records when the message was sent.

    I want to be able to group by month the total transactions between any two pairs of users. This sounds difficult and note sure if it can be done with the way I structured my table and how Jeff wrote the previous query, but none the less if anyone has any ideas let me know.

    Results of it should look something like

    From User To User Total Messages Month

    user1 user2 5 January

    user1 user2 10 February

    user1 user3 15 February

    and so on.

  • Heh... you just need to look it in the eye... 😉

    [font="Courier New"]

    --=====&nbspThis&nbspis&nbspjust&nbspto&nbspbuild&nbspsome&nbspsample&nbspdata&nbspand&nbspis&nbspNOT&nbsppart&nbspof&nbspthe&nbspsolution

    DECLARE&nbsp@yourtable&nbspTABLE&nbsp(FromUser&nbspVARCHAR(10),&nbspToUser&nbspVARCHAR(10),&nbspDate&nbspDATETIME)

    &nbspINSERT&nbspINTO&nbsp@yourtable&nbsp(FromUser,&nbspToUser,&nbspDate)

    &nbspSELECT&nbsp'user1','user2',&nbsp'20070115'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'user2','user1',&nbsp'20070116'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'user1','user2',&nbsp'20070117'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'user1','user3',&nbsp'20070118'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'user3','user1',&nbsp'20070118'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'user1','user2',&nbsp'20070201'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'user2','user1',&nbsp'20070205'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'user1','user2',&nbsp'20070301'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'user1','user3',&nbsp'20070310'&nbspUNION&nbspALL

    &nbspSELECT&nbsp'user3','user1',&nbsp'20070311'

    --=====&nbspThis&nbspis&nbspthe&nbspsolution

    &nbspSELECT&nbspd.FromUser,&nbspd.ToUser,&nbspd.YrMon,&nbspCOUNT(*)&nbspAS&nbspTimesConnected

    &nbsp&nbsp&nbspFROM&nbsp(--====&nbspDerived&nbsptable&nbspswaps&nbspusers&nbspwhen&nbspneeded

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSELECT&nbspFromUser,&nbsp

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspToUser,

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspREPLACE(CONVERT(CHAR(7),Date,102),'.','-')&nbspAS&nbspYrMon

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspFROM&nbsp@yourtable

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWHERE&nbspFromUser&nbsp<&nbspToUser

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspUNION&nbspALL

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSELECT&nbspToUser&nbspAS&nbspFromUser,

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspFromUser&nbspAS&nbspToUser,

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspREPLACE(CONVERT(CHAR(7),Date,102),'.','-')&nbspAS&nbspYrMon

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspFROM&nbsp@yourtable

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWHERE&nbspToUser&nbsp<=&nbspFromUser

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp)&nbspd

    &nbsp&nbspGROUP&nbspBY&nbspd.FromUser,&nbspd.ToUser,&nbspd.YrMon

    [/font]

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply