Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7175663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:20:53+00:00 2026-05-28T16:20:53+00:00

I have the following sql which returns Projects and related Contractors as well as

  • 0

I have the following sql which returns Projects and related Contractors as well as document size info from three different tables. The sequence is: First Contractors are entered in the system, then Projects are entered for each Contractors, during with document sizes (sheets) are entered. So far so good: The following sql returns all projects along with appropriate Contractors and Sheets fine:

SELECT 
   dbo.generalcontractors.name,
   dbo.Projects.*,
   dbo.sheets.sheetsize,
   dbo.sheets.sheetid
FROM 
   dbo.generalcontractors 
INNER JOIN dbo.Projects ON (dbo.generalcontractors.uid = dbo.Projects.generalcontractorid)
INNER JOIN dbo.sheets ON (dbo.Projects.sheetid = dbo.sheets.sheetid) 
ORDER BY 
    dbo.Projects.projectdate DESC

Now, what needs to happen is that documents for individual projects are entered in a ProjectDocuments table. I have the following code which checks to see documents exist in the ProjectsDocuments table and return appropriate values; currently, the project id is hard-coded and it work.

Question is: how can I combine the following sql into the sql above? I am looking for one query result which will show any matching rows from the ProjectsDocument table and if no matching then will show ‘Not Found’.

This is for SQL Server 2005.

Thanks!

IF (SELECT COUNT(*) FROM dbo.projectsdocuments, dbo.Projects 
    WHERE projectsdocuments.projectid = Projects.projectid 
    AND Projects.projectid = 5) > 0 
    SELECT * 
    FROM projectsdocuments 
ELSE 
    SELECT 'Not Found' AS HighPrice
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:20:54+00:00Added an answer on May 28, 2026 at 4:20 pm

    One option would be to just simply add the ProjectDocuments table to your query, using a LEFT OUTER JOIN.

    If there are no matching rows in the ProjectDocuments table – then all columns from that table will be NULL – so capturing one of the columns and using the ISNULL(<colname>, 'Not Found') construction, you’d be displaying ‘Not Found’ when no values have been found in ProjectDocuments:

    SELECT 
       gc.name,
       pr.ProjectDate, pr.Column1, pr.Column2, ....., pr.ColumnN,
       s.sheetsize,
       s.sheetid,
       ISNULL(pd.Name, 'Not Found') AS 'ProjectDocumentName',
       pd.Column1, ..., pd.ColumnN
    FROM 
       dbo.GeneralContractors gc 
    INNER JOIN 
       dbo.Projects pr ON gc.uid = pr.GeneralContractorID
    INNER JOIN 
       dbo.Sheets s ON pr.SheetID = s.SheetID
    LEFT OUTER JOIN
       dbo.ProjectsDocuments pd ON pr.ProjectID = pd.ProjectID
    ORDER BY 
        pr.ProjectDate DESC
    

    Two things I’d recommend doing:

    • use meaningful table aliases (like gc for GeneralContractor, pr for Project and so forth) – it makes reading the queries just soooo much easier….

    • explicitly specify the columns you want to to retrieve from the tables – that prevents unwanted surprises when suddenly your table has five BLOB columns more that get returned – even though you don’t need them!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a following problem. I have a ListView which returns data from SQL
I have the following which works in SQL Query Analyzer . select oh.* from
I have the following SQL statement which returns a single record as expected: select
I have the following SQL statement which returns Not unique table/alias . I'm fairly
I have the following statement SELECT id, descr from mytable which returns 1, 'Test1'
I have following somewhat complex sql query which has horrible performance, 'certainly' due to
I have the following query in iSeries SQL which I output to a file.
I have a sql table which have the following data, Id City Country ---
I have the following Linq to SQL query, in which I'm trying to do
I have the following piece of SQL: select DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)) which comes through as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.