Click here to Skip to main content
15,906,285 members
Home / Discussions / Database
   

Database

 
GeneralRe: Networked SQL DB Pin
Garth J Lancaster26-May-04 13:00
professionalGarth J Lancaster26-May-04 13:00 
GeneralSQL Nested queries syntax probs Pin
partt25-May-04 6:48
partt25-May-04 6:48 
GeneralRe: SQL Nested queries syntax probs Pin
Michael Potter25-May-04 11:25
Michael Potter25-May-04 11:25 
GeneralRe: SQL Nested queries syntax probs Pin
partt26-May-04 3:56
partt26-May-04 3:56 
GeneralRe: SQL Nested queries syntax probs Pin
partt26-May-04 3:57
partt26-May-04 3:57 
GeneralRe: SQL Nested queries syntax probs Pin
Michael Potter26-May-04 6:43
Michael Potter26-May-04 6:43 
GeneralRe: SQL Nested queries syntax probs Pin
partt26-May-04 7:50
partt26-May-04 7:50 
GeneralRe: SQL Nested queries syntax probs Pin
Michael Potter26-May-04 9:07
Michael Potter26-May-04 9:07 
I would normally break a query that is getting this large. It really depends upon the size of your data set and your indexes. Only testing can tell.

Given the current query you can simply add a correlated sub query to the WHERE Clause.

AND tGroup.ScheduleStamp  = 
    (SELECT MAX(cor_tGroup.ScheduleStamp)            
    FROM tTester cor_tTester           
    INNER JOIN tResultQue cor_tResultQue                                   
        ON (cor_tTester.TesterID = cor_tResultQue.TesterID)            
    INNER JOIN tGroup cor_tGroup                              
        ON (cor_tResultQue.GroupID = cor_tGroup.GroupID)            
    WHERE cor_tTester.TesterID = tTester.TestID)


I think I would use a memory temp table to solve the total issue (syntax not tested).

DECLARE @tbl TABLE
(
    TesterID INTEGER NOT NULL,
    ScheduleStamp DATETIME NOT NULL
)
--*******************************************************
-- find the correct testers and their last ScheduleStamp
--*******************************************************
INSERT INTO @tbl
(
    TesterID,
    ScheduleStamp 
)
SELECT
    tTester.TesterID,
    MAX(tGroup.ScheduleStamp)
FROM tGroup     
INNER JOIN tResultQue         
    ON (tGroup.GroupID = tResultQue.GroupID)    
INNER JOIN tTester         
    ON (tResultQue.TesterID = tTester.TesterID)
GROUP BY TesterID
HAVING MAX(tGroup.ScheduleStamp) <= @DD
--***************************
-- final select for results
--***************************
SELECT 
    @tbl.ScheduleStamp,         
    @tbl.TesterID,         
    tTester.LastName,        
    tTester.FirstName,         
    tTester.WorkNo,         
    tTester.WorkExtension,         
    tTester.HomeNo,        
    tTester.SexID,         
    tTester.Birth,         
    tTester.RaceID,         
    tTester.SiteID
FROM @tbl
INNER JOIN tTester
    ON (@tbl.TesterID = tTester.TesterID)
WHERE tTester.RaceID = @RaceID AND         
    tTester.Birth >= @LBirth AND         
    tTester.Birth <= @UBirth AND         
    tTester.SiteID = @SiteID AND 


These two queries can even be put into one if you want. I find it easier to maintain this way. Testing for speed might change my mind.
GeneralRe: SQL Nested queries syntax probs Pin
partt26-May-04 9:36
partt26-May-04 9:36 
GeneralRe: SQL Nested queries syntax probs Pin
Michael Potter26-May-04 9:44
Michael Potter26-May-04 9:44 
GeneralRe: SQL Nested queries syntax probs Pin
partt26-May-04 10:12
partt26-May-04 10:12 
GeneralAttach SQL 2000 mdf on MSDE 2000 Pin
Aryo Handono25-May-04 4:17
professionalAryo Handono25-May-04 4:17 
GeneralRe: Attach SQL 2000 mdf on MSDE 2000 Pin
Hesham Amin25-May-04 23:55
Hesham Amin25-May-04 23:55 
GeneralRe: Attach SQL 2000 mdf on MSDE 2000 Pin
RichardGrimmer26-May-04 4:18
RichardGrimmer26-May-04 4:18 
GeneralNew Row with Id Pin
Anonymous25-May-04 4:15
Anonymous25-May-04 4:15 
GeneralRe: New Row with Id Pin
Michael Potter25-May-04 11:35
Michael Potter25-May-04 11:35 
GeneralProblem about MSDE installation Pin
wk_vigorous25-May-04 0:47
wk_vigorous25-May-04 0:47 
GeneralRe: Problem about MSDE installation Pin
RichardGrimmer26-May-04 4:23
RichardGrimmer26-May-04 4:23 
Generaloptimize this query Pin
varun8025-May-04 0:25
varun8025-May-04 0:25 
GeneralRe: optimize this query Pin
Grimolfr25-May-04 3:31
Grimolfr25-May-04 3:31 
GeneralRe: optimize this query Pin
varun8025-May-04 19:34
varun8025-May-04 19:34 
GeneralRe: optimize this query Pin
Grimolfr26-May-04 4:42
Grimolfr26-May-04 4:42 
Generalquery inside a dataset Pin
fuel2run24-May-04 21:27
fuel2run24-May-04 21:27 
GeneralRe: query inside a dataset Pin
ekolis25-May-04 9:02
ekolis25-May-04 9:02 
GeneralRe: query inside a dataset Pin
fuel2run25-May-04 21:08
fuel2run25-May-04 21:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.