Results 1 to 14 of 14

Thread: [RESOLVED] Get the max value of two different data in two different tables

  1. #1

    Thread Starter
    Fanatic Member aNubies's Avatar
    Join Date
    Aug 2008
    Posts
    558

    Resolved [RESOLVED] Get the max value of two different data in two different tables

    Table1

    ID | xDATE | NAME

    1 | 9/20/11 | Cool
    2 | 9/20/11 | Woo
    1 | 9/20/12 | Cool
    2 | 9/20/12 | Woo


    Table2

    ID | xDATE | NAME

    1 | 9/20/12 | Cool
    2 | 9/20/12 | Woo


    I'm having a problem figuring this out, all I want to get is get the max value of those 2 different data in two different tables. The result that I wanted is

    ID | xDATE | NAME

    1 | 9/20/12 | Cool
    2 | 9/20/12 | Woo


    Help here please.

  2. #2
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Re: Get the max value of two different data in two different tables

    Try a typical SELECT MAX query on a UNION of both these tables ??

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get the max value of two different data in two different tables

    The max what? Your example is useless because it doesn't exemplify anything.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member aNubies's Avatar
    Join Date
    Aug 2008
    Posts
    558

    Re: Get the max value of two different data in two different tables

    It seems the result is not what I expect sir.
    @jm - sorry I forgot, max base on dates.

    TABLE 1

    ID | DATE | NAME

    1 | 03/01/11 | SAMP1
    2 | 03/01/11 | SAMP2
    3 | 03/01/11 | SAMP3
    4 | 03/01/11 | SAMP4


    TABLE 2

    ID | DATE | NAME

    2 | 03/01/10 | SAMP2
    4 | 03/01/10 | SAMP4


    Here's another set of data:
    The result should be the same with table 2. That is because those 2 records also in table1. But if one of those are not present in table 1 then the result might probably 1 record only.
    Last edited by aNubies; Jan 28th, 2011 at 01:58 AM.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get the max value of two different data in two different tables

    You still haven't provided a proper explanation or a decent example. For a start, you say that you want to get the "max base on dates". That doesn't really make sense. Please provide a PROPER description. Also, your example has the same date in every record, so how does that help us see what you want.

    Are you maybe saying, without actually saying, that you want one record for each ID that appears in both where the value for the date is the maximum from the two tables? If so, please say so. It's very frustrating when you're trying to help but you have to guess what's actually required. You know and we don;t, so you need to tell us. Assume that two many words are better than not enough.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member aNubies's Avatar
    Join Date
    Aug 2008
    Posts
    558

    Re: Get the max value of two different data in two different tables

    My apology, sorry for my bad english, anyway, here's another set of data:

    Table 1

    ID | DATE | NAME
    2011-0002 | 2011-01-26 | Charles
    2004-0001 | 2011-01-03 | Shanovski
    2004-0002 | 2011-01-03 | Clayton
    2011-0001 | 2011-01-18 | Crislie

    Table 2


    ID | DATE | NAME
    2004-0001 | 2010-01-26 | Shanovski
    2008-0002 | 2010-01-26 | Grey
    2011-0001 | 2011-01-26 | Crislie

    --------------

    What Im trying to do is display the records by joining those two tables wherein a record is present on both tables using the Maximum of there dates MAX(DATE). In table 1 a user can appear multiple times but in table 2 user can only appears once. Now if I just going to Inner Join them too many records will be display, but all I need is base on their maximum dates. Hope it give you a hint what im trying to do.

  7. #7
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Re: Get the max value of two different data in two different tables

    If table 2 will contain only one record per user, imo the max(date) won't make sense on this table.

    Is this what you want:

    Get each user record from table1 where the corresponding date is the max(date) for that user and then join this record with table2 data?

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  8. #8

    Thread Starter
    Fanatic Member aNubies's Avatar
    Join Date
    Aug 2008
    Posts
    558

    Re: Get the max value of two different data in two different tables

    By doing that,

    Code:
    SELECT MAX(DATE) FROM Table1
    It only display one record, but they are lot of different records which they have their own maximum dates.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get the max value of two different data in two different tables

    So, every ID in table 2 will appear in table 1, and every ID will always have the same name in both tables?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Fanatic Member aNubies's Avatar
    Join Date
    Aug 2008
    Posts
    558

    Re: Get the max value of two different data in two different tables

    @jm - yes indeed sir.

  11. #11
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Get the max value of two different data in two different tables

    I'm struggling with your English a bit as well (good effort, though ) so I'm not sure this is the answer you want but I think you're after this:-

    Code:
    Select ID, Max(Date), Name
    From 
      (Select ID, Date, Name From Table1
      Union
      Select ID, Date, Name From Table2)
    Group By ID, Name
    The inner query 'combines' the two tables as HoneyBee suggested. The outer takes that combined table and returns you the highest date for each ID and Name combination.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  12. #12

    Thread Starter
    Fanatic Member aNubies's Avatar
    Join Date
    Aug 2008
    Posts
    558

    Re: Get the max value of two different data in two different tables

    I'm struggling with your English a bit as well (good effort, though )
    Yeah, I know I'm still worst in English . But, gee thank you, FunkyDexter. That query is what I'm looking for . BTW is there any other alternatives rather than UNION ?

  13. #13
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Get the max value of two different data in two different tables

    Not that I can think of. Union is the correct SQL keyword for combining two sets of data together 'vertically' (Joins combine them 'horizontally'). why don't you want to use it?
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  14. #14

    Thread Starter
    Fanatic Member aNubies's Avatar
    Join Date
    Aug 2008
    Posts
    558

    Re: Get the max value of two different data in two different tables

    Nah just looking for other alternatives. But anyway thanks .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width