SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    463
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    select where column returns more than one row

    hi all

    i want to select a count of all users that logged in more than once on the same day, same week, same month, etc
    i have this table that tracks users logging in and logging out

    the relevant fields are there i think
    startDateTime is a DateTime column that is inserted when a user logs in
    UserID is the user's ID and is also inserted when the user logs in

    Code:
         SELECT COUNT(sessiontrack_UserID) FROM trackuser_sessiontrack 
         		WHERE TO_DAYS(sessiontrack_StartDateTime) = TO_DAYS(NOW()) 
        		GROUP BY DATE(sessiontrack_DateTime) 
         		HAVING (COUNT(sessiontrack_UserID) > 1)
    but this is returning the count of the whole table
    rather than the number of users logged in more than once


    thanks
    dave
    Last edited by dtra; Dec 20, 2004 at 02:10.

  2. #2
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this:

    HAVING COUNT(CASE WHEN sessiontrack_UserID>1 THEN sessiontrack_UserID ELSE NULL END)

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,273
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    gavin, you are counting all userids except userid 1

    dave, what is the difference between sessiontrack_StartDateTime and sessiontrack_DateTime?

    Code:
    select count(*) 
      from (
           select sessiontrack_UserID
                , count(*) as logins
             from trackuser_sessiontrack 
            where to_days(sessiontrack_StartDateTime) 
                = to_days(now()) 
        	group by sessiontrack_UserID
           )
     where logins > 1
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Evangelist
    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    463
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the replies
    there's no difference, i'm just dumb, they're both meant to be startdatetime
    i'll try what you have said there r937

    i got this error message
    #1248 - Every derived table must have its own alias

    no idea what that means
    i tried select from trackuser_sessiontrack AS st, and put st.
    in front of all the other fields, but same error


  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,273
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    see the closing parenthesis for the derived table in the FROM clause, just ahead of the WHERE keyword?

    put as dt after the closing parenthesis

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Evangelist
    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    463
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, thanks mate that was it
    one more question
    if i want to select it over a period of days, rather than NOW()
    ie. DAYS(startdate) -1, DAYS(startdate) -2
    so it's between a startdate and an enddate
    i want it to return multiple rows like with a GROUP BY date
    is that possible, or will i need to use a php loop to select that out?

  7. #7
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,273
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Code:
    select sessiontrack_StartDateTime
         , count(*) 
      from (
           select sessiontrack_StartDateTime
                , sessiontrack_UserID
                , count(*) as logins
             from trackuser_sessiontrack 
            where to_days(sessiontrack_StartDateTime) 
                between to_days('startdate')
                    and to_days('enddate')
        	group 
        	   by sessiontrack_StartDateTime
                , sessiontrack_UserID
           ) as dt
     where logins > 1  
    group
        by sessiontrack_StartDateTime
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •