+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 16

Thread: Query table to compare last mo to current mo

  1. #1
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    Query table to compare last mo to current mo

    Hi All,

    Ok, I have a table that is similar to data below:

    EQ# - PN# - LocName - MonthYr
    111 - 202 - Hou - 11/1/2009
    111 - 202 - Hou - 12/1/2009
    333 - 203 - Hou - 12/1/2009 (new)
    345 - 123 - Hou - 11/1/2009
    345 - 123 - Hou - 12/1/2009
    433 - 323 - Hou - 12/1/2009 (new)

    When I look at this data I was to see if the EQ# existed in the previous month, if it does, I don't care about it, I only want to see new EQ# showing up. In example above, I put the (new) by the new records showing up in 12/1/2009 that didn't exist there in 11/1/2009.

    How can I query the above to only see:
    EQ# - PN# - LocName - MonthYr
    333 - 203 - Hou - 12/1/2009 (new)
    433 - 323 - Hou - 12/1/2009 (new)

    I tried using a Distinct and Unique but couldn't get that to work...

    Thanks for any help in advance!

  2. #2
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    This would be easy with the SQL EXCEPT command, but I don't think that exists in SQL 2000. In this case, you would need to do a self join using LEFT JOIN and only return the records where the joined-to values are NULL.

    That's as clear as mud, I know -- so let me work on a solution.

    [edit]
    I didn't try this out, but it should work or be close:
    Code:
    Select A.*, isNull(B.OldFlag, '(new)')
     From myTable A
     Left Join (
        Select EQ, 'Old' as OldFlag
         from myTable   
         Where MonthYr <= '2009-11-01'
        ) B
     On (A.EQ = B.EQ)
     Where A.MonthYr = '2009-12-01'
       And B.OldFlag is null
    
    Note that I just hard coded the dates in, not optimal. If you can, use a parameter for the starting date.
    Code:
    Select A.*, isNull(B.OldFlag, '(new)')
     From myTable A
     Left Join (
        Select EQ, 'Old' as OldFlag
         from myTable   
         Where MonthYr <= dateadd(m, -1, @rptMonth)
        ) B
     On (A.EQ = B.EQ)
     Where A.MonthYr = @rptMonth
       And B.OldFlag is null
    
    and set @rptMonth to something like '2009-12-01' for December.
    [/edit]
    Last edited by Wolffy; February 3rd, 2010 at 02:03 PM.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  3. #3
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    W0lffy,

    I can't seem to get past error, wrong number of arguements on this part:

    isNull(B.OldFlag, '(new)')

    Any clue?

  4. #4
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Goodness -- no bloody clue. That should work!

    For now, change it to just B.OldFlag to see if the rest of the query works -- perhaps there is a syntax error elsewhere that is confounding the parser.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  5. #5
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    Ok, I must be screwing up something here... ... I keep getting the death of access error saying it has encountered a problem and has to close and repair...

    Question, what field does B.OldFlag represent ?
    EQ# - PN# - LocName - MonthYr

  6. #6
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Ach! Sorry, I thought you were using SQL Server 2000 -- never looked at what subforum I was in. ISNULL don't work there the same way. Change it to iif(isNull(B.OldFlag), '(new)', B.OldFlag)

    OldFlag is a column created by the subquery. It can be any value you want. In fact, it really isn't necessary since you could do the same thing by using B.EQ from the subquery.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  7. #7
    Lightning Master AOG123 is a jewel in the rough AOG123 is a jewel in the rough AOG123 is a jewel in the rough AOG123 is a jewel in the rough AOG123's Avatar
    Join Date
    Mar 2008
    Location
    Fortress Of Solitude
    Posts
    394
    Rep Power
    7

    lol, im guessing you don't actually have (new) in your data.

    you were just pointing out the records in question,.

    As long as you have an auto number, we can get your result.
    If i helped you, make me famous by clicking the

  8. #8
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Yeah, as usual there are often several ways to do the same thing. After review, I realized that the Old/New flag is silly -- the only records returned are the new records anyway.
    Code:
    Select A.*
     From myTable A
     Left Join (
        Select EQ
         from myTable   
         Where MonthYr <= '2009-11-01'
        ) B
     On (A.EQ = B.EQ)
     Where A.MonthYr = '2009-12-01'
       And B.EQ is null
    
    Last edited by Wolffy; February 3rd, 2010 at 04:06 PM.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  9. #9
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    Thanks AOG and W0lffy...yup, no (new) in data, just in example on here.

    Ok, I am getting about 900 records now with the below, but I now need to tie in another table to linking by cost ctr to get it loc name. Ex: my tblLoc looks like (LocName - LocCostCtr), tblMain below has a field named CostCtr, but I tried this with the query below it gave me grief about having the * on A.*. Next I tried to just create a new query with the query below and tblLoc but when I pull in newquery below it was blank

    1)Should I create a make table from the results I get and then link to tblLoc?
    2) grrr...ok, sorry on the below I realized I need to only see if its new to it cost center/loc, example it could be new in the tblMain in 12/1/2009 but also see what cc it was/if it was new to its 12/1/2009 loc...let me think about this one some more...its confusing.

    Code:
    Select A.*
     From tblMain A
     Left Join (
        Select EQ
         from tblMain   
         Where MonthYr <=#11/1/2009#
        ) B
     On (A.EQ = B.EQ)
     Where A.MonthYr =#12/1/2009#
       And B.EQ is null
    
    Last edited by Rebelle; February 4th, 2010 at 09:58 AM. Reason: Added questions 1&2

  10. #10
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Yeah, not sure that Access likes the A.* syntax or not. If it continues to complain, just specify all the columns you need explicitly.

    You should be able to add the JOIN to tabloc to the query right before the Where clause. Just remember to include a table alias (C?).
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. SQL Query in Excel csv using MS Query
    By richyrich in forum Microsoft Office
    Replies: 2
    Last Post: August 11th, 2009, 11:50 AM
  2. Table or Query
    By gjh in forum Microsoft Access
    Replies: 2
    Last Post: March 5th, 2009, 05:09 PM
  3. Compare user entered date to date in table
    By tuxalot in forum Microsoft Access
    Replies: 1
    Last Post: February 27th, 2009, 10:31 AM
  4. compare data in two tables
    By tulz in forum SQL Development
    Replies: 3
    Last Post: February 5th, 2009, 06:25 AM
  5. Get Current Page Name
    By jmurrayhead in forum .NET Code Samples
    Replies: 0
    Last Post: December 2nd, 2008, 11:29 AM

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

SEO by vBSEO