Solved

SQL adding IF statement

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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