Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Query not pulling information from joined table

Posted on 2013-11-04
4
Medium Priority
?
163 Views
Last Modified: 2014-01-30
I have the following code in a stored procedure that sends an email with the information.  Everything works fine except that it's not getting any data from the tbl_ProjectTrackingContracts (as ct) table.  What am I missing?
0
Comment
Question by:Kevin Smith
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39623164
Did you forget to attach the SQL?
0
 

Author Comment

by:Kevin Smith
ID: 39623170
Oops..Ha...

SELECT
                td = p.JobNumber    , ''
                , td = ISNULL(pt.ProjectTitle,'')   ,  ''
                , td = ISNULL(l.PropertyTitle,'')   ,  ''
               , td = ISNULL(pt.MProjectNumber,'')   ,  ''
                , td = ISNULL(pm.PMName,'')   ,  ''
                , td = ISNULL(pm.PMCoordinator,'')   ,  ''
               
                , td = ISNULL(replace(convert(char(10), ct.ContractedExecuted, 101), '-', ' '),CONVERT(char(8),''))     ,  ''
                , td = ISNULL(ct.ContractExecutedSt,'')   ,  ''
                  , td = ISNULL(replace(convert(char(10), ct.COI, 101), '-', ' '),CONVERT(char(8),''))     ,  ''
                , td = ISNULL(ct.COISt,'')   ,  ''
                , td = replace(convert(char(10), pt.ConstStart, 101), '-', ' ')    ,  ''

                                           
              FROM (projmgmtsrv.dbo.tbl_ProjectTracking AS pt
              LEFT JOIN projmgmtsrv.dbo.tbl_Jobs AS p
              ON pt.ProjectID = p.ProjectID)
              LEFT JOIN projmgmtsrv.dbo.tbl_PM AS pm ON p.PM = pm.PMID                
              LEFT JOIN projmgmtsrv.dbo.tbl_Locations AS l ON p.Location = l.LocationID
              LEFT JOIN projmgmtsrv.dbo.tbl_ProjectTrackingContracts AS ct ON pt.ProjectID = ct.ProjectTrackingID
               where pt.ConstStart between getdate() and dateadd(dd,14,getdate()) and (ct.ContractExecutedSt = 'NO' OR ct.ContractExecutedSt = 'N/A' OR ct.ContractExecutedSt is null or ct.COIst = 'NO' or ct.COIst = 'N/A' or ct.COIst is null)
              order by pt.ConstStart asc
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39623195
This won't be the cause, but is there any reason you giving every column the same alias ('td').

The join is syntactically OK, the question is whether the conditions are correct.

Without having access to the data there probably isn't much anyone can do to help.

Have you run the query manually and played with the relevant conditions to see what is happening?
0
 
LVL 12

Accepted Solution

by:
Paul_Harris_Fusion earned 2000 total points
ID: 39623838
As Dale says,  tough without the data and your expected results.
That said, the where clause looks odd to me since you have some constraints on a table retrieved via a left join.

Your current SQL:
              LEFT JOIN projmgmtsrv.dbo.tbl_ProjectTrackingContracts AS ct ON pt.ProjectID = ct.ProjectTrackingID
               where pt.ConstStart between getdate() and dateadd(dd,14,getdate()) and (ct.ContractExecutedSt = 'NO' OR ct.ContractExecutedSt = 'N/A' OR ct.ContractExecutedSt is null or ct.COIst = 'NO' or ct.COIst = 'N/A' or ct.COIst is null)

Could that be a source of confusion?  
Should some of the conditions be in the ON clause?   This might produce different results since you are using an outer join.
e.g.

LEFT JOIN projmgmtsrv.dbo.tbl_ProjectTrackingContracts AS ct
ON pt.ProjectID = ct.ProjectTrackingID
  and (ct.ContractExecutedSt = 'NO' OR ct.ContractExecutedSt = 'N/A' OR  
  ct.ContractExecutedSt is null or ct.COIst = 'NO' or ct.COIst = 'N/A' or ct.COIst is null)
WHERE
  pt.ConstStart between getdate() and dateadd(dd,14,getdate())
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question