+ Reply to Thread
Page 2 of 2 FirstFirst 1 2
Results 11 to 16 of 16

Thread: Query table to compare last mo to current mo

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

    Ok, I'm struggling with this one, still not getting all the results I need...

    Looking back at AOG comment, AOG you mean an autonumber on my table with all my months data? If so, is it too late to add one?

    What happens is .... each month I get a new load of data (there will be new EQ#'s and EQ#'s that disappear from month to month). There are 2 things I would like to see, if it was there but with a different cost center # than in the current/latest month (looking at specific cost center)....then secondly, if it didn't exist in previous months' data but now in the specific cost center.

    Hope that makes sense....from month to month some EQ#'s may move from one cost center to another (I only want to see a cost center for current month but if the previous cost center was different). this will tell me it wasn't in the new cost center until now, leaving out all the ones that were already in the current cost center. Just new EQ#'s (either from another cost center or weren't in the previous months' data at all).

    Maybe this can't be done in one query?

  2. #12
    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

    Are the EQ# unique? How about the Cost Center Numbers? That is, in the same month can an EQ# appear in more than 1 Cost Center?
    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. #13
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    EQ# is unique with the month. So it can only exist one time in the month of 11/1/2009.

    Nope the EQ# will only have 1 cost center assigned each month.

    so one month (11/1/2009) EQ# 1133 - CC# 1001110122- (HOU)
    then next month it may be the same in 12/1/2009, if it is the same I don't care about it, but if it does change like...

    12/1/2009 EQ#1133 - CC#1001210147-(DAL)

    So now this EQ# now shows in Dallas Cost center so its new to Dallas even though in November it was in Houston. This one I would want to see for list of new for Dallas.

    Then there may be some EQ# that didn't exist in Novembers data (11/1/2009) but now exist in 12/1/2009 under Dallas, ex:

    12/1/2009 EQ#1145 - CC#1001210148-(DAL), this one I want to show up in list new to Dallas.

    What I don't want to see are records that existed in 11/1/2009 Dallas Cost center and 12/1/2009 Dallas Cost center because this tells me it was already in Dallas and was not new to Dallas in Dec.

    Right now with the query I'm working with it pulls if it didn't exist in Nov but now exists but I'm not getting the items that may have been in another cost center in Nov and now new to its new cost center.

    Here is what it looks like:

    Code:
    SELECT A.EQ, A.[Func Loc], A.[Cost Ctr], C.LocName, C.LocCC
    FROM (tblMain AS A LEFT JOIN (SELECT EQ FROM tblMain WHERE MonthYr=#11/1/2009#) AS B ON A.EQ=B.EQ) 
    INNER JOIN tblLocCC AS C ON A.[Cost Ctr]=C.LocCC
    WHERE (((A.MonthYr)=#12/1/2009#) AND ((B.EQ) Is Null));
    
    Everytime I try to add in something for cost center <> cost center I get errors.

  4. #14
    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

    Consider this approach. Create a new table that contains the following fields:
    Code:
    rptYear rptMonth EQno CCno
    
    Then, add the EQno/CCno pair to this table each month, but only if the most recent EQno/CCno pair does not already exist. For example
    Code:
    2009 11 1113 1001210147
    2009 11 1145 1001210148
    
    Now, assume the following data for December
    Code:
    2009 12 1113 1001210148
    2009 12 1145 1001210148
    2009 12 1168 1001210147
    
    So, after 'upserting' this data, you would have
    Code:
    2009 11 1113 1001210147
    2009 11 1145 1001210148
    2009 12 1113 1001210148
    2009 12 1168 1001210147
    
    The record for 1145 would not be included in December. Now, its a fairly simple query to determine the changed/new EQno/CCno pairs for any month. Note that you could add a Changed/New flag as well if it is important to determine new EQno's for any given month.
    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. #15
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    Hey W0lffy,

    Thanks for the reply, I was thinking about what you last reply suggested but I use the table to calculate each months' data for other fields in the table, so I don't think I want to lose any records or else I'd have to make a new table all together.

    I was tinkering around with this again right now and I put the following together to attempt to get the records that I was missing from the other query, can you take alook and see if it looks good to you? This seems to get me the records that were in another cost center in previous month and now in current month. So I need to see if I can combine the other query you helped me with with this one into one query...

    Code:
    SELECT A.EQ, A.[Cost Ctr], A.MonthYr, B.EQ, B.[Cost Ctr], B.MonthYr
    FROM tblMain AS A INNER JOIN tblMain AS B ON A.EQ = B.EQ
    WHERE (((A.[Cost Ctr]) Not Like [enter Not Like CC:] & "*") AND 
    ((A.MonthYr)=[Enter Previous MonthDate:]) AND ((B.[Cost Ctr]) Like [enter like CC:] & "*") 
    AND ((B.MonthYr)=[Enter Current MonthDate:]));
    

  6. #16
    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

    Oh, my head. I'll take a look when I get a few minutes -- probably over my Jimmy John's today.

    Not sure why you would 'lose records' in my approach. Think of the tables as a helper table -- I'm not suggesting that you move the EQ and CC values from your original table. They would be duplicated in this table, but this is a case when some 'denormalization' might be warranted.
    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 2 of 2 FirstFirst 1 2

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