|
-
Jan 27th, 2011, 11:57 PM
#1
Thread Starter
Fanatic Member
[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.
-
Jan 28th, 2011, 01:28 AM
#2
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 ??
.
-
Jan 28th, 2011, 01:49 AM
#3
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.
-
Jan 28th, 2011, 01:49 AM
#4
Thread Starter
Fanatic Member
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.
-
Jan 28th, 2011, 02:43 AM
#5
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.
-
Jan 28th, 2011, 03:25 AM
#6
Thread Starter
Fanatic Member
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.
-
Jan 28th, 2011, 03:50 AM
#7
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?
.
-
Jan 28th, 2011, 04:03 AM
#8
Thread Starter
Fanatic Member
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.
-
Jan 28th, 2011, 05:09 AM
#9
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?
-
Jan 30th, 2011, 09:41 PM
#10
Thread Starter
Fanatic Member
Re: Get the max value of two different data in two different tables
-
Jan 31st, 2011, 08:40 AM
#11
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
-
Jan 31st, 2011, 08:23 PM
#12
Thread Starter
Fanatic Member
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 ?
-
Feb 1st, 2011, 08:59 AM
#13
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
-
Feb 1st, 2011, 07:32 PM
#14
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|