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. complicated select

complicated select

Scheduled Pinned Locked Moved Database
databasehelptutorialquestion
6 Posts 4 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.
  • B Offline
    B Offline
    benams
    wrote on last edited by
    #1

    I'm not familiar enough with SQL syntax, and I need to make a little complicated SELECT. I have a table with 3 fields: contentID, date and readsCounter. I have to get the top 2 read contents for the last 2 days. For example: contentID: 0, date: 1-1-2000, readsCounter: 10 contentID: 0, date: 2-1-2000, readsCounter: 80 contentID: 1, date: 1-1-2000, readsCounter: 40 contentID: 1, date: 2-1-2000, readsCounter: 5 contentID: 2, date: 1-1-2000, readsCounter: 20 contentID: 2, date: 2-1-2000, readsCounter: 30 the lines above are 6 records from my db, contents 0 and 2 are the contents that need to be selected(the contents and the reads sum for the last 2 days). I'm working with MSACCESS DB. someone can help me?

    C M 2 Replies Last reply
    0
    • B benams

      I'm not familiar enough with SQL syntax, and I need to make a little complicated SELECT. I have a table with 3 fields: contentID, date and readsCounter. I have to get the top 2 read contents for the last 2 days. For example: contentID: 0, date: 1-1-2000, readsCounter: 10 contentID: 0, date: 2-1-2000, readsCounter: 80 contentID: 1, date: 1-1-2000, readsCounter: 40 contentID: 1, date: 2-1-2000, readsCounter: 5 contentID: 2, date: 1-1-2000, readsCounter: 20 contentID: 2, date: 2-1-2000, readsCounter: 30 the lines above are 6 records from my db, contents 0 and 2 are the contents that need to be selected(the contents and the reads sum for the last 2 days). I'm working with MSACCESS DB. someone can help me?

      C Offline
      C Offline
      Chris Meech
      wrote on last edited by
      #2

      The dates are from 10 years ago, so how would they be considered for the last 2 days. :confused:

      Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

      J 1 Reply Last reply
      0
      • C Chris Meech

        The dates are from 10 years ago, so how would they be considered for the last 2 days. :confused:

        Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

        J Offline
        J Offline
        Jorgen Andersson
        wrote on last edited by
        #3

        Maybe it's an old book.

        List of common misconceptions

        1 Reply Last reply
        0
        • B benams

          I'm not familiar enough with SQL syntax, and I need to make a little complicated SELECT. I have a table with 3 fields: contentID, date and readsCounter. I have to get the top 2 read contents for the last 2 days. For example: contentID: 0, date: 1-1-2000, readsCounter: 10 contentID: 0, date: 2-1-2000, readsCounter: 80 contentID: 1, date: 1-1-2000, readsCounter: 40 contentID: 1, date: 2-1-2000, readsCounter: 5 contentID: 2, date: 1-1-2000, readsCounter: 20 contentID: 2, date: 2-1-2000, readsCounter: 30 the lines above are 6 records from my db, contents 0 and 2 are the contents that need to be selected(the contents and the reads sum for the last 2 days). I'm working with MSACCESS DB. someone can help me?

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          Use a combination of Row_Number and Partition. You need to create a 2 query select, the first injects the row_number based on the partition of contentid and date, ordered by readscounter. The outer query selects anything with a row_number < 3.

          Never underestimate the power of human stupidity RAH

          B 1 Reply Last reply
          0
          • M Mycroft Holmes

            Use a combination of Row_Number and Partition. You need to create a 2 query select, the first injects the row_number based on the partition of contentid and date, ordered by readscounter. The outer query selects anything with a row_number < 3.

            Never underestimate the power of human stupidity RAH

            B Offline
            B Offline
            benams
            wrote on last edited by
            #5

            can you add an example code for what you have suggested? I'm afraid I don't completely understand how to implement this.

            M 1 Reply Last reply
            0
            • B benams

              can you add an example code for what you have suggested? I'm afraid I don't completely understand how to implement this.

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              Then I found a simpler way when I actually worked the query.

              SELECT TOP 2
              contentID,
              RC
              FROM
              (SELECT
              contentID,
              SUM(readsCounter) RC
              FROM
              Book1 AS B
              --WHERE ReadDate BETWEEN this AND that
              GROUP BY
              contentID) D
              ORDER BY RC desc

              Note the where clause will be required when the data set is larger

              Never underestimate the power of human stupidity RAH

              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