User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 391,588 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,634 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MySQL advertiser:
Views: 180 | Replies: 1
Reply
Join Date: Jul 2008
Posts: 4
Reputation: Nuxxy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Nuxxy Nuxxy is offline Offline
Newbie Poster

Query...errr...query

  #1  
27 Days Ago
My SQL knowledge is a little rusty. While I can write simple queries, I have a complicated one that is giving me a headache. Any help?

I'm using the following tables (only showing the relevant fields)...

Test table
- TestID (pk)
- TestDesc

FAC table
- TestID (fk, Test)
- Modified (Date/Time)
* pk(TestID, Modified)

Results table
- SerialNo
- TestCount
* pk(SerialNo, TestCount)
- TestDateTime
- TestID

I need to write a query that shows the following:
1. Each TestID and corresponding TestDesc
2. The most recent Modified date from the FAC table for each TestID
3. The number of SerialNos in the Results table that have a TestDateTime after the Modified date in #2, for each TestID

I can write queries through joins to get 2, and 3 without the Modified data restriction, but it's tying it all together in one SELECT statement that's getting me confused.

Thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2008
Posts: 4
Reputation: Nuxxy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Nuxxy Nuxxy is offline Offline
Newbie Poster

Re: Query...errr...query

  #2  
26 Days Ago
Ok...so here's my code. It seems to work, but I'm not sure if it's the most efficient code ever. It'd be nice for some SQL syntax experts to take a look...

# *** query to combine the other subqueries ***
SELECT t.testid, t.testdesc, lf.lastfac, vc.vehcount
FROM test t, (
# *** lf 'table' - calculates the last fac date for each testid in the fac table ***
SELECT f.testid, MAX(f.modified) AS lastfac
FROM fac f
GROUP BY f.testid) AS lf, (
# *** vc 'table' - calculates the vehicle count per testid in the results table ***
SELECT tc.testid, COUNT(tc.testid) AS vehcount
FROM (
# *** tc 'table' - used in vc - calculates the serialno's per testid where the testdatetime is greater than the last fac modified date for that testid ***
SELECT r1.testid, r1.serialno, COUNT(r1.testid) AS testcount, MAX(r1.testdatetime) AS testdatetime
FROM results r1
GROUP BY r1.testid, r1.serialno
HAVING testdatetime > (
# *** simple subquery to calculate the matching last fac modifed value for the appropriate test id from tc ***
SELECT MAX(f1.modified)
FROM fac f1
WHERE f1.testid = r1.testid
GROUP BY f1.testid)
ORDER BY r1.serialno, r1.testid) AS tc
GROUP BY testid) AS vc
WHERE t.testid = lf.testid
AND t.testid = vc.testid
ORDER BY testid;
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MySQL Marketplace
Thread Tools Display Modes

Other Threads in the MySQL Forum

All times are GMT -4. The time now is 11:04 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC