LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-05-2006, 06:55 AM   #1
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
MySQL query


First of all, I'm not a database designer but I have sufficient knowledge to be dangerous with them
Second, not sure if it belongs here or in software. If in the wrong forum, can a moderator please move it.

I'm using the following query to retrieve some data from multiple tables. It lists the affected services per rfs.
Code:
select distinct rfs.PK_rfs,rfs.status,rfs.datim_open,
rfs.datim_resolved,unix_timestamp(rfs.datim_resolved)-unix_timestamp(rfs.datim_open) as lapse_resolved,
rfs.datim_closed,unix_timestamp(rfs.datim_closed)-unix_timestamp(rfs.datim_open) as lapse_closed,
services.service

from rfs 
left join rfsaffected on rfs.PK_rfs=rfsaffected.FK_rfs
left join group_srvc on group_srvc.PK_grpsrvc=rfsaffected.grpsrvc 
left join services on services.PK_srvc=group_srvc.FK_srvc 

where rfsaffected.active='y' AND datim_open>='2006-08-02 00:00:00' AND datim_open<='2006-09-05 08:53:34'

order by rfs.PK_rfs,service,datim_open
I think that the database structure can be derived from this query; if not, I can try to draw or explain it.

The main tables are 'rfs' and 'rfsaffected' (one-to-many relationship). The other tables are basically help tables.

This query contains two calculated colums. I like to sum the values of one of those columns grouped by service. The result only has to contain the service and the total lapse_resolved or total lapse_closed.

Is this possible by modifying the given query? My attempts till now have failed (either mysql is not happy with the syntax or I get incorrect results). I think that the problem is that I don't know how to combine the aggregate function with the distinct keyword.

For now, I've figured out how to use a temporary table and run the aggregate query on that table. Unfortunately I have to give the enduser permission to create tables, something that makes me feel uncomfortable.

The MySQL version is 4.0.23a and all tables are MyISAM (just in case the latter is relevant).

Last edited by Wim Sturkenboom; 09-05-2006 at 06:57 AM.
 
Old 09-05-2006, 07:44 AM   #2
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Genarally, if this type of query happens often and you need to report from some or all the fields in it, it might be advantageous to create a view with the above structure and then write your aggregate function from it.

Let me add that I have no exposure to mySQL but I expect views will work.

End
 
Old 09-05-2006, 08:02 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
Thanks, but views only came in with version 5.0.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 06:36 PM
Need mysql query help DropHit Linux - Software 7 04-01-2005 11:32 AM
Mysql Error: Lost Connection to Mysql during query ramnath Programming 5 11-18-2003 12:27 PM
mysql query ? shaahul Linux - Software 1 09-06-2003 03:35 AM
mysql query !!! hitesh_linux Linux - General 1 02-03-2003 03:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration