If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Access > complicated select statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-04-11, 13:36
benams benams is offline
Registered User
 
Join Date: Mar 2010
Posts: 3
complicated select statement

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?
Reply With Quote
  #2 (permalink)  
Old 03-07-11, 02:55
MStef-ZG MStef-ZG is offline
Registered User
 
Join Date: Apr 2005
Location: Zagreb - Croatia
Posts: 344
Tell as which result you want to get ?
Reply With Quote
  #3 (permalink)  
Old 03-07-11, 06:38
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by benams View Post
I have to get the top 2 read contents for the last 2 days.
please explain "last 2 days"
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 03-07-11, 08:14
benams benams is offline
Registered User
 
Join Date: Mar 2010
Posts: 3
The result I want

contentID: 0, readsCounter: 90
contentID: 2, readsCounter: 50

as you can see, I want the ID off the most read pages(during the last 2 days) and the sum of the reads

the "last 2 days" int the example are: 1-1-2000 and 2-1-2000 ( in the real program it changes every day... )
Reply With Quote
  #5 (permalink)  
Old 03-07-11, 08:28
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
try this (untested) --
Code:
SELECT TOP 2
       contentID
     , SUM(readsCounter) AS total
  FROM daTable
 WHERE date IN
       ( SELECT TOP 2
                distinct_date
           FROM ( SELECT date AS distinct_date
                    FROM daTable
                  GROUP
                      BY date ) AS d
         ORDER
             BY distinct_date DESC )
GROUP
    BY contentID
ORDER
    BY total DESC
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Tags
access, count, select

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On