Solved

SQL adding IF statement

Posted on 2011-03-06
4
349 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
[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
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:
Ephraim Wangoya 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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Suggested Solutions

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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