If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > How to group DATE column from two or more tables?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-26-07, 21:54
sunnyside sunnyside is offline
Registered User
 
Join Date: Aug 2003
Posts: 30
Question How to group DATE column from two or more tables?

There are 2 tables (let's say 2 tables first), in each table there is a date field (YYYY-mm-dd). I want to Join these 2 tables together if they have same "classId", also I want the result group by date.

Example: I want to find the different date when classId = "windows"

table_1
-------------------------
2007-05-22 | windows |
2007-05-26 | office |
-------------------------

table_2
-------------------------
2007-05-22 | windows |
2007-05-24 | windows |
-------------------------

I want the result is: (group by the date)

2007-05-22 | windows
2007-05-24 | windows

is there any way can perform this? thanks

Last edited by sunnyside; 05-27-07 at 00:03.
Reply With Quote
  #2 (permalink)  
Old 05-26-07, 22:10
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
i'm quite certain there is a way to do it, but there are a number of things about your question that don't make sense

why do you want GROUP BY? what happened to the row for the 26th? why it is excluded by the 24th is included?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 05-27-07, 00:03
sunnyside sunnyside is offline
Registered User
 
Join Date: Aug 2003
Posts: 30
Quote:
Originally Posted by r937
i'm quite certain there is a way to do it, but there are a number of things about your question that don't make sense

why do you want GROUP BY? what happened to the row for the 26th? why it is excluded by the 24th is included?
Oh, I didn't make my self clear. My result is based on if I pick classId = "windows"
Reply With Quote
  #4 (permalink)  
Old 05-27-07, 06:09
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
i still don't understand what you want so i will guess

Code:
select date,class
  from table_1
 where class='windows'
union    
select date,class
  from table_2
 where class='windows'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 05-29-07, 04:06
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
Ooh, can i guess too?

Code:
SELECT classId
          ,date
FROM table_1 t1 
JOIN table_2 t2 ON t1.classId=t2.classId
ORDER BY date ASC
Please define what you mean by GROUP BY because GROUP BY is an SQL clause, and should ONLY be used with AGGREGATE functions, none of which you are implementing in your suggestion.

Clarify!
Reply With Quote
  #6 (permalink)  
Old 05-29-07, 04:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
aschk, how do you get the 24th from an inner join?

guess again, eh

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 05-29-07, 04:40
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
Ooh I get another guess :

Code:
SELECT * 
FROM planet_moon pm
LEFT JOIN star_system ss ON pm.interstellar_activity = ss.asteroid_range
WHERE asteroid_range > 3426 
AND intersteller_activity > 3
AND planets > 1
GROUP BY solar_system
ORDER BY cloud_dust_ratio

p.s. sorry for the cynical reply. Sunnyside needs to clarify
Reply With Quote
  #8 (permalink)  
Old 05-29-07, 04:53
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
It looks like he is looking to Join the two tables (on classId) as he described, then GROUP BY date. The problem is that is using the GROUP BY clause completely wrongly.

e.g.

SELECT t2.classId,t2.date
FROM table_1 t1
JOIN table_2 t2 ON t1.classId=t2.classId
GROUP BY date

For the datasets provided above :

table_1
-------------------------
2007-05-22 | windows |
2007-05-26 | office |
-------------------------

table_2
-------------------------
2007-05-22 | windows |
2007-05-24 | windows |
-------------------------

You will get a result of
JOIN ON "windows"
--------------------------------------------------
2007-05-22 | windows | 2007-05-22 | windows |
2007-05-24 | windows | 2007-05-22 | windows |
--------------------------------------------------

Select ONLY t2 result parts (date,classId)
----------------------
2007-05-22 | windows
2007-05-24 | windows
----------------------

GROUP BY date
----------------------
2007-05-22 | windows
2007-05-24 | windows
----------------------

Of course this is complete crap... because it uses the GROUP BY clause really badly.

What i'm most curious about is how these two tables exist concurrently because it seems they contain the same information...
Reply With Quote
  #9 (permalink)  
Old 05-29-07, 04:55
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by aschk
Code:
FROM planet_moon pm
LEFT JOIN star_system ss ON pm.interstellar_activity = ss.asteroid_range
WHERE asteroid_range > 3426 
AND intersteller_activity > 3
two comments -- you've defeated the purpose of the LEFT OUTER JOIN (this query will produce only inner join results), and the condition on interstellar_activity being greater than 3 is totally superfluous, redundant, unnecessary, and redundant

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 05-29-07, 23:35
sunnyside sunnyside is offline
Registered User
 
Join Date: Aug 2003
Posts: 30
Sorry, guys. I was too busy to come here these a few days. Actually, right now I used another way to figure this problem out. (can't just UNION or JOIN these tables, too complicated)

Thank you all!
Reply With Quote
  #11 (permalink)  
Old 05-30-07, 04:53
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
What problem? You still haven't explained what it was

"can't just UNION or JOIN these tables, too complicated"

That entirely depends on what you're looking for. And I would say that you CAN use both of those on your tables... I would also hope that you don't use UNION/JOIN interchangeably
Reply With Quote
  #12 (permalink)  
Old 05-30-07, 11:02
sunnyside sunnyside is offline
Registered User
 
Join Date: Aug 2003
Posts: 30
Quote:
Originally Posted by aschk
What problem? You still haven't explained what it was

"can't just UNION or JOIN these tables, too complicated"

That entirely depends on what you're looking for. And I would say that you CAN use both of those on your tables... I would also hope that you don't use UNION/JOIN interchangeably
No, of course I didn't use "union/join" together.

The entire problem is: I have 3 tables, each table contains a date field (2 of 3 are in format "YYYY-mm-dd", another one uses format as "YYYY/mm/ddThh:mm:ss"). Also all these tables have a Key field (classID). My query is : fetch all rows in these 3 tables which have the same "classID", also will group by date: for example: 2007-05-22 has two rows (from 2 tables), 2007-05-23 has 5 rows (from all these 3 tables).....

Hope you understand what I'm saying
Reply With Quote
  #13 (permalink)  
Old 05-30-07, 11:06
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by sunnyside
Hope you understand what I'm saying
nope, it's still fuzzy, and it still sounds like UNION

do you perhaps mean ORDER BY when all along you've been saying "group by"
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #14 (permalink)  
Old 05-30-07, 16:01
sunnyside sunnyside is offline
Registered User
 
Join Date: Aug 2003
Posts: 30
Quote:
Originally Posted by r937
nope, it's still fuzzy, and it still sounds like UNION

do you perhaps mean ORDER BY when all along you've been saying "group by"
No, it has to be "group by". One same date can contains multiple rows of info.
Reply With Quote
  #15 (permalink)  
Old 05-30-07, 16:12
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by sunnyside
No, it has to be "group by".
fine

would you be willing to walk us through your original sample data, then?

because it still doesn't make sense

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On