Results 1 to 6 of 6

Thread: [RESOLVED] [SQL Server 2005] SQL question Complicated join

  1. #1

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Resolved [RESOLVED] [SQL Server 2005] SQL question Complicated join

    Ok here is where I am at. I have the following tables
    LabAddress
    --LabAddressPK
    --etc...

    ACToAddress
    --ACToAddressPK
    --LabAddressPK
    --AnalysisCodePK

    SampleAC
    --SampleACPK
    --SamplePK
    --AnalysisCodePK

    Samples
    --SamplePK
    --etc...

    Essentially what it is is that I have Labs that can have multiple addresses. Assigned to these addresses are multiple Analysis Codes they can perform.

    I then have samples that have specific Analysis Codes assigned to them.

    What I need to be able to return is the samples that a specific lab address can analyze. So the input would be the LabAddressPk and I need to return all the SamplePK that that lab address can perform. So I am kind of lost on how to do this and any help would be appreciated.

    I hope I explained what I am trying to do here well enough and if not please let me know.

    Thanks,

    Brian
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [SQL Server 2005] SQL question Complicated join

    By simply joining one table at a time, I came up with this:
    Code:
    SELECT S.SamplePK
    FROM LabAddress LA 
    INNER JOIN ACToAddress AA ON (LA.LabAddressPK = AA.LabAddressPK)
    INNER JOIN SampleAC SA ON (SA.AnalysisCodePK = AA.AnalysisCodePK)
    INNER JOIN Samples S ON (S.SamplePK = SA.SamplePK)
    WHERE LA.LabAddressPK = ?

  3. #3

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [SQL Server 2005] SQL question Complicated join

    For some reason that is returning results it shouldn't. For example SamplePk 28 has analysis codes 9 and 4 assigned to it, and labAddressPK 4 can do 1 and 8 and when I run that with LabAddressPK = 4 it returns SamplePK 28 as acceptable yet it is not, because address 4 can not perform analysis code 9 or 4.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [SQL Server 2005] SQL question Complicated join

    Based purely on the table structure you showed (and assumptions of how they are supposed to link together), the Joins I posted are correct.. is there anything else we should know about the table structure?

    To check that the joins are correct, I would recommend checking some data from each table, by adding more fields to the Select clause, eg:
    Code:
    SELECT S.SamplePK, SA.SampleACPK, AA.ACToAddressPK, LA.LabAddressPK
    FROM ...

  5. #5

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [SQL Server 2005] SQL question Complicated join

    I am marking this resolved because I was able to write a stored procedure to do what I was trying to accomplish. Below is the Stored Procedure I wrote
    Code:
    CREATE PROCEDURE [dbo].[sp_GetLocsForAddress]
    	@addresspk INT
    AS
    	DECLARE @spks VARCHAR(MAX), @acpk INT
    	SELECT SamplePK, AnalysisCodePK INTO #t_Samples FROM SampleAC WHERE SamplePK IN (SELECT SamplePK FROM Samples WHERE DateSentToLab IS NULL  AND DateCollected IS NOT NULL);
    	DELETE FROM #t_Samples WHERE SamplePK IN 
    	(SELECT SamplePK FROM #t_Samples WHERE AnalysisCodePK NOT IN 
    	(SELECT AnalysisCodePK FROM AcToAddress WHERE LabAddressPK = @addresspk));
    
    	SELECT LocPK,LocationName 
    	FROM Locations WHERE LocPK IN 
    	(SELECT LocPK FROM Samples WHERE SamplePK IN
    	(SELECT SamplePK FROM #t_Samples));
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] [SQL Server 2005] SQL question Complicated join

    Ah.. there are extra conditions!

    Here's how you could merge the Select Into and the Delete:
    Code:
    SELECT SAC.SamplePK, SAC.AnalysisCodePK 
    INTO #t_Samples 
    FROM SampleAC SAC
    INNER JOIN Samples S ON (SAC.SamplePK = S.SamplePK)
    WHERE S.DateSentToLab IS NULL
    AND S.DateCollected IS NOT NULL
    AND AnalysisCodePK NOT IN (SELECT AnalysisCodePK FROM AcToAddress WHERE LabAddressPK = @addresspk)
    ..and how you could merge that with the final Select:
    Code:
    SELECT L.LocPK, L.LocationName
    FROM Locations L 
    INNER JOIN Samples S ON (L.LocPK = S.LocPK)
    INNER JOIN SampleAC SAC ON (SAC.SamplePK = S.SamplePK)
    WHERE S.DateSentToLab IS NULL  
    AND S.DateCollected IS NOT NULL
    AND AnalysisCodePK NOT IN (SELECT AnalysisCodePK FROM AcToAddress WHERE LabAddressPK = @addresspk)
    Note that this may return slightly different results as it is no longer using an IN clause.. I think it should be OK tho.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width