Solved

SQL query with secondary filter clause

Posted on 2013-12-19
4
249 Views
Last Modified: 2014-01-03
Hi:

I want to join tableA with table B, like the following:

select a.*, b.col1
from tableA a
left join tableB b on a.col1=b.col1 and a.col2=b.col2    --col2 is a datetime column

If the above brings back a matched (non-null) b.col1, I am fine, otherwise, I need to try to get it by:

select a.*, b.col1
from tableA a
left join tableB b on a.col1=b.col1 and a.col2=dbo.fnPriorBusinessDate(b.col2)

Anyway to combine the above two into one queries and without join tableB twice? TableB is a huge table, anything I tried that involves joining it twice takes too long to run.

Thanks.
0
Comment
Question by:sdc248
  • 2
  • 2
4 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 39730422
select a.*, b.col1

Should that be col2?  col1 is the same in both tables.

If you don't need to list a specific value, just need to check if a match exists, you can use an EXISTS() instead of a join.
0
 

Author Comment

by:sdc248
ID: 39730462
Sorry, it should be b.col3.

I need a value from col3 of tableB.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 39730488
Hopefully SQL can figure out the best plan possible for the code below.  If not, if it's still too slow, post definitions for all indexes on tableB.


select a.*,
    (SELECT TOP (1) b.col3
     FROM tableB b
     WHERE a.col1=b.col1 and a.col2 in (b.col2, dbo.fnPriorBusinessDate(b.col2))
     ORDER BY CASE WHEN a.col2 = b.col2 THEN 1 ELSE 2 END
    ) AS col3    
from tableA a
0
 

Author Comment

by:sdc248
ID: 39755110
Thanks. Didn't have chance to try your approach though. Used a workaround and got acceptable performance.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error message when scheduling a job using a linked Server 12 55
How to use TOP 1 in a T-SQL sub-query? 14 41
CROSS APPLY 4 45
Need help debbuging stored procedure 21 34
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now