Solved

SQL adding IF statement

Posted on 2011-03-06
4
353 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

705 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