Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. count query

count query

Scheduled Pinned Locked Moved Database
database
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Deian
    wrote on last edited by
    #1

    Hello, I have table1 with fields id, date, usernamename. I can get the count of records created by user(s) for certaing date range, but if user 'A' has not inserted any records it is missing from result. What I want to achieve is to be able to display the username, with count=0. I know it should be some sort of self join but I can't manage to get it work. All suggestions appreciated!

    A 1 Reply Last reply
    0
    • D Deian

      Hello, I have table1 with fields id, date, usernamename. I can get the count of records created by user(s) for certaing date range, but if user 'A' has not inserted any records it is missing from result. What I want to achieve is to be able to display the username, with count=0. I know it should be some sort of self join but I can't manage to get it work. All suggestions appreciated!

      A Offline
      A Offline
      Arjan Einbu
      wrote on last edited by
      #2

      You can use GROUP BY with the HAVING clause.

      SELECT username
      FROM your_table
      GROUP BY username
      HAVING COUNT(*) = 0

      D 1 Reply Last reply
      0
      • A Arjan Einbu

        You can use GROUP BY with the HAVING clause.

        SELECT username
        FROM your_table
        GROUP BY username
        HAVING COUNT(*) = 0

        D Offline
        D Offline
        Deian
        wrote on last edited by
        #3

        I've tried this also, it does not return records in my case.

        A 1 Reply Last reply
        0
        • D Deian

          I've tried this also, it does not return records in my case.

          A Offline
          A Offline
          Arjan Einbu
          wrote on last edited by
          #4

          You're right! I've should've seen that. It can't possibly work... Will something like this work?

          SELECT y1.Username
          FROM your_table y1
          LEFT JOIN your_table y2
          ON y1.Username = y2.Username
          WHERE y2.date BETWEEN start AND end
          GROUP BY y1.Username
          HAVING COUNT(y2.Username) = 0

          The table alias y1 represent all usernames and y2 represent only the records within the requested range.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups