Results 1 to 15 of 15
  1. #1
    JRCharlie's Avatar
    JRCharlie is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2011
    Posts
    20

    query readings from current and previous month

    I have the following table in access.


    Click image for larger version. 

Name:	table.JPG 
Views:	21 
Size:	23.4 KB 
ID:	33556
    I would like to query a table to show the readings from the the current month and the previous month as per next table.
    Click image for larger version. 

Name:	qurery.JPG 
Views:	20 
Size:	24.8 KB 
ID:	33557
    Thanks in advance.

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    you need a more complete description - what if there is no reading this month? do you still need to see last months reading?

  3. #3
    Join Date
    Apr 2017
    Posts
    1,776
    And is it possible, that for same customer are several readings in same month, or not? And when this is possible, what will be the reading for month?

  4. #4
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,130
    Have a look at the meter readings example here http://allenbrowne.com/subquery-01.html
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  5. #5
    JRCharlie's Avatar
    JRCharlie is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2011
    Posts
    20
    Every month must have areading the only time the query would return a Null value would be on the first month a reading will be entered

  6. #6
    JRCharlie's Avatar
    JRCharlie is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2011
    Posts
    20
    No it is not possible a customer to have more than one reading per month.

  7. #7
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,130
    Fine. So have you looked at the link I provided as the example is identical to what you want.
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  8. #8
    JRCharlie's Avatar
    JRCharlie is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2011
    Posts
    20
    Thanks Ridders52

    I found the slution in your link.

    Code:
    SELECT Reading_tbl.rdID, Reading_tbl. Reading_Date, Reading_tbl.Reading,    (SELECT TOP 1  DUPE.Reading
       FROM Reading_tbl AS Dupe                     
       WHERE Dupe. Customer = Reading_tbl. Customer
         AND Dupe. Reading_Date < Reading_tbl. Reading_Date
       ORDER BY Dupe. Reading_Date DESC, Dupe. rdID)   AS PriorRead 
    FROM Reading_tbl;
    Im getting the following invalid errorClick image for larger version. 

Name:	error.JPG 
Views:	11 
Size:	19.3 KB 
ID:	33581 with this query.
    When I copy this query onto FlySpeed it works fine.

  9. #9
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    from your error message it looks like you have unnecessary spaces between reading_tbl. and Reading_Date.

  10. #10
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,130
    No idea what FlySpeed is!

    The error shown is caused by the space after tbl. in Reading_tbl. Reading_Date

    There are a total of 6 spaces to remove (though these may be forum glitches rather than in your code)
    Try this:

    Code:
    SELECT Reading_tbl.rdID, Reading_tbl. Reading_Date, Reading_tbl.Reading,
       (SELECT TOP 1 Dupe.Reading
       FROM Reading_tbl AS Dupe                     
       WHERE Dupe.Customer = Reading_tbl.Customer
         AND Dupe.Reading_Date < Reading_tbl.Reading_Date
       ORDER BY Dupe.Reading_Date DESC, Dupe.rdID) AS PriorRead  
       FROM Reading_tbl;




    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  11. #11
    JRCharlie's Avatar
    JRCharlie is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2011
    Posts
    20
    Thanks for your help
    Its woring now
    After removing the spaces and also coping your query the problem still precisted, I rewrote the query now its working.
    So for hlping some one else with a similar problem I realy don't know what was causing the error.

  12. #12
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,130
    To help others reading this thread, please post the corrected version that works for you
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  13. #13
    JRCharlie's Avatar
    JRCharlie is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2011
    Posts
    20
    This is the working version.

    Code:
    SELECT Reading_tbl.rdID, Reading_tbl.Reading_Date, Reading_tbl.Reading, (SELECT TOP 1 Dupe.Reading   FROM Reading_tbl AS Dupe                     
       WHERE Dupe.Customer = Reading_tbl.Customer         AND Dupe.Reading_Date < Reading_tbl.Reading_Date
       ORDER BY Dupe.Reading_Date DESC, Dupe.rdID) AS PriorRead
    FROM Reading_tbl;

  14. #14
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,130
    Unless I'm missing something, that's exactly what I posted as the correct version in post 10!
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  15. #15
    JRCharlie's Avatar
    JRCharlie is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2011
    Posts
    20
    You are correct.
    Like I previouly posted when I copied and pasted your code the same error had acured.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 08-17-2016, 07:13 AM
  2. Query criteria for current month
    By JimO in forum Queries
    Replies: 4
    Last Post: 03-24-2016, 04:16 PM
  3. Replies: 2
    Last Post: 02-12-2015, 05:14 AM
  4. Replies: 6
    Last Post: 05-05-2014, 10:51 PM
  5. Current Month/Year Query
    By Roadbeer in forum Queries
    Replies: 3
    Last Post: 06-17-2013, 01:20 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums