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 > query doubt

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-23-07, 03:34
adorearun adorearun is offline
Registered User
 
Join Date: Nov 2006
Posts: 34
query doubt

Hi All,
I have three tables ......
table 1 in which we have
---------
id | name

table 2
---------
id | name | value | time

table 3
-------------
id | name | avg val |c1 | c2 | c3 | c4 | c5


Table one is resource table ,Table two is resource table with values for time say for one hour we have five entries and table three is archiving table where we archive the value for one hour.
Now we use
insert into t3 select value from t2 group by resid
by this query the value in t2 table is averaged to one hour and inserted into t3.

my requirement is we need to insert the all five entries in the respective column c1,c2,c3,c4,c5 for respective resid while inserting the archived entry.

one way of doing this query the resid from t1 and then query the t2 table with the result set obtained in the previous query and then insert into t3 ......

this approach will use three queries .........

is there any other approach to achieve this using single query.....

your help is appreciated
sorry for the long mail

Thanx n advance,
Arun
Reply With Quote
  #2 (permalink)  
Old 07-23-07, 07:02
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
insert 
  into t3 
     ( id , name , avgval , c1 , c2 , c3 , c4 , c5 )
select id
     , name
     , AVG(value)
     , ( select MIN(c1) from t2 where id = 1 )
     , ( select MIN(c2) from t2 where id = 2 )
     , ( select MIN(c3) from t2 where id = 3 )
     , ( select MIN(c4) from t2 where id = 4 )
     , ( select MIN(c5) from t2 where id = 5 )
  from t2 
group 
    by id
     , name
if this is not what you want, then it is because you've done such a wonderful job hiding your real table and column names and explaining the situation

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 07-23-07, 08:40
adorearun adorearun is offline
Registered User
 
Join Date: Nov 2006
Posts: 34
thanks for your reply and compliment.
In your query you have hardcoded the id but in my case i dono the id ,the id will be present in t1 and t2.
now what happens in my case is ....
1) we are inserting hourly archiving value (meaning for every hour) in t3 from t2 using t1 (we use group by id by comparing t1.id=t2.id).

requirement:

2) I need to insert the raw value in the t3 along with the above insert .......for specific id ,but i dono the which id they are inserting for every hour in 1)...if i know the id then only i can query the t2 and get the raw value.

thanks
Arun
Reply With Quote
  #4 (permalink)  
Old 07-23-07, 08:59
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
you have lost me completely

if you wish to proceed, please show your real queries
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 07-24-07, 00:52
adorearun adorearun is offline
Registered User
 
Join Date: Nov 2006
Posts: 34
Hi,
Im sorry for not posting the real query,may be it create some problems for me.But i thank you very much for your intention to help.

Thanks for your help
Arun
Reply With Quote
  #6 (permalink)  
Old 07-24-07, 08:25
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
Please don't waste people's time by posting a query that has nothing to do with your real queries.
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