Solved

SQL adding IF statement

Posted on 2011-03-06
4
344 Views
Last Modified: 2012-05-11
Hi-
I'm trying to write a sql statement  and I'm having trouble adding an if statement -
I think it needs to be a case statement but I'm not sure how to do this....


This is the part - I need help with:
if isnull(dbo.CompanyStatus_Date10_ReEngage.ReEngageDate1) then MAX(dbo.cstd_company_status.creation_date) else  ((MAX(dbo.cstd_company_status.creation_date)
                      <= dbo.CompanyStatus_Date10_ReEngage.ReEngageDate1)

SELECT     TOP (100) PERCENT dbo.oncd_company.company_id, dbo.oncd_company.company_name_1, dbo.onca_company_status.translation AS Status3, 
                      MAX(dbo.cstd_company_status.creation_date) AS StatusDate3, dbo.onca_company_status.status_sequence, 
                      dbo.CompanyStatus_Date10_ReEngage.ReEngageDate1
FROM         dbo.CompanyStatus_Date10_ReEngage RIGHT OUTER JOIN
                      dbo.oncd_company ON dbo.CompanyStatus_Date10_ReEngage.company_id = dbo.oncd_company.company_id LEFT OUTER JOIN
                      dbo.onca_company_status RIGHT OUTER JOIN
                      dbo.cstd_company_status ON dbo.onca_company_status.company_status_code = dbo.cstd_company_status.company_status_code ON 
                      dbo.oncd_company.company_id = dbo.cstd_company_status.company_id
GROUP BY dbo.oncd_company.company_id, dbo.oncd_company.company_name_1, dbo.onca_company_status.translation, 
                      dbo.onca_company_status.status_sequence, dbo.CompanyStatus_Date10_ReEngage.ReEngageDate1
HAVING      (dbo.onca_company_status.status_sequence = N'03') AND if isnull(dbo.CompanyStatus_Date10_ReEngage.ReEngageDate1) then MAX(dbo.cstd_company_status.creation_date) else  ((MAX(dbo.cstd_company_status.creation_date)
                      <= dbo.CompanyStatus_Date10_ReEngage.ReEngageDate1)
ORDER BY dbo.oncd_company.company_name_1

Open in new window

0
Comment
Question by:stacydr
4 Comments
 
LVL 5

Expert Comment

by:logideepak
ID: 35047216
case when dbo.CompanyStatus_Date10_ReEngage.ReEngageDate1 is null then MAX(dbo.cstd_company_status.creation_date) else (MAX(dbo.cstd_company_status.creation_date)
 end as testcolumn

The complete query could be as follows:

SELECT     TOP (100) PERCENT dbo.oncd_company.company_id, dbo.oncd_company.company_name_1, dbo.onca_company_status.translation AS Status3,
                      MAX(dbo.cstd_company_status.creation_date) AS StatusDate3, dbo.onca_company_status.status_sequence,
                      dbo.CompanyStatus_Date10_ReEngage.ReEngageDate1,
case when dbo.CompanyStatus_Date10_ReEngage.ReEngageDate1 is null then MAX(dbo.cstd_company_status.creation_date) else (MAX(dbo.cstd_company_status.creation_date)
 end as testcolumn
FROM         dbo.CompanyStatus_Date10_ReEngage RIGHT OUTER JOIN
                      dbo.oncd_company ON dbo.CompanyStatus_Date10_ReEngage.company_id = dbo.oncd_company.company_id LEFT OUTER JOIN
                      dbo.onca_company_status RIGHT OUTER JOIN
                      dbo.cstd_company_status ON dbo.onca_company_status.company_status_code = dbo.cstd_company_status.company_status_code ON
                      dbo.oncd_company.company_id = dbo.cstd_company_status.company_id
GROUP BY dbo.oncd_company.company_id, dbo.oncd_company.company_name_1, dbo.onca_company_status.translation,
                      dbo.onca_company_status.status_sequence, dbo.CompanyStatus_Date10_ReEngage.ReEngageDate1
HAVING      (dbo.onca_company_status.status_sequence = N'03') AND if isnull(dbo.CompanyStatus_Date10_ReEngage.ReEngageDate1) then MAX(dbo.cstd_company_status.creation_date) else  ((MAX(dbo.cstd_company_status.creation_date)
                      <= dbo.CompanyStatus_Date10_ReEngage.ReEngageDate1)
ORDER BY dbo.oncd_company.company_name_1
0
 
LVL 11

Expert Comment

by:N R
ID: 35047225
Need to fill in the ?'s, but here you go.

 
case when isnull(dbo.CompanyStatus_Date10_ReEngage.ReEngageDate1) = ?then MAX(dbo.cstd_company_status.creation_date) 
else  when ((MAX(dbo.cstd_company_status.creation_date)
                      <= dbo.CompanyStatus_Date10_ReEngage.ReEngageDate1) then ?
end

Open in new window

0
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 35047339
Use case
SELECT     TOP (100) PERCENT dbo.oncd_company.company_id, dbo.oncd_company.company_name_1, dbo.onca_company_status.translation AS Status3, 
                      MAX(dbo.cstd_company_status.creation_date) AS StatusDate3, dbo.onca_company_status.status_sequence, 
                      dbo.CompanyStatus_Date10_ReEngage.ReEngageDate1
FROM         dbo.CompanyStatus_Date10_ReEngage RIGHT OUTER JOIN
                      dbo.oncd_company ON dbo.CompanyStatus_Date10_ReEngage.company_id = dbo.oncd_company.company_id LEFT OUTER JOIN
                      dbo.onca_company_status RIGHT OUTER JOIN
                      dbo.cstd_company_status ON dbo.onca_company_status.company_status_code = dbo.cstd_company_status.company_status_code ON 
                      dbo.oncd_company.company_id = dbo.cstd_company_status.company_id
GROUP BY dbo.oncd_company.company_id, dbo.oncd_company.company_name_1, dbo.onca_company_status.translation, 
                      dbo.onca_company_status.status_sequence, dbo.CompanyStatus_Date10_ReEngage.ReEngageDate1
HAVING (dbo.onca_company_status.status_sequence = N'03') AND 
       case 
         when dbo.CompanyStatus_Date10_ReEngage.ReEngageDate1 IS null then 
            MAX(dbo.cstd_company_status.creation_date) 
         else  
            MAX(dbo.cstd_company_status.creation_date)
        end  <= dbo.CompanyStatus_Date10_ReEngage.ReEngageDate1
ORDER BY dbo.oncd_company.company_name_1

Open in new window

0
 
LVL 2

Author Closing Comment

by:stacydr
ID: 35047450
Thanks!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

18 Experts available now in Live!

Get 1:1 Help Now