Solved

SQL If NUll on Unpivot

Posted on 2014-12-19
1
82 Views
Last Modified: 2015-01-15
So wondering if this sql code can have a statemet put in it that will do the following.

It runs the query as printed below but it checks to see if a specific feild is null.

So for instance this query currently does not have the field [appraisefinal]. I want it to while running this query do some if statements and say if appraisefinal is null then insert [appraisal ordered] into the item of outlookreport.dbo.calendar.

;WITH MyCTE AS
(
    SELECT    * 
    FROM      (
                  SELECT   *
                  FROM      emdb.dbo.calendarview 
              )p
    UNPIVOT 
    ( 
        EventDate FOR DateDescription in ([Appraisal Ordered],[Closing Date],[Application Signed],[Appraisal Recieved] ,[Approval To Close],[Insurance Ordered],[Title Ordered])
    ) as unpvt
)
insert into outlookreport.dbo.calendar (eventdate,item,xrefid)
SELECT    
          M.EventDate,
          M.DateDescription,
          T.xrefid
FROM      emdb.dbo.calendarview T 
          JOIN MyCTE M
              ON T.xrefid = M.xrefid
WHERE NOT EXISTS(SELECT 1 FROM outlookreport.dbo.calendar WHERE eventdate = M.EventDate AND xrefid = T.xrefid);

Open in new window

0
Comment
Question by:desiredforsome
1 Comment
 
LVL 33

Accepted Solution

by:
ste5an earned 500 total points
ID: 40510753
hmm..

 
WITH Data AS 
	(
		SELECT	[Appraisal Ordered],
				[Closing Date],
				[Application Signed],
				[Appraisal Recieved] ,
				[Approval To Close],
				[Insurance Ordered],
				[Title Ordered],
				COALESCE(appraisefinal, [Appraisal Ordered]) AS appraisefinal
		FROM	emdb.dbo.calendarview 
	)
SELECT	* 
FROM	Data
UNPIVOT ( EventDate FOR DateDescription IN ( appraisefinal, [Appraisal Ordered], [Closing Date], [Application Signed], [Appraisal Recieved], [Approval To Close], [Insurance Ordered], [Title Ordered] ) ) U;

Open in new window

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

762 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