?
Solved

Compare and update sql server query

Posted on 2016-08-04
5
Medium Priority
?
66 Views
Last Modified: 2016-08-09
I have a table with following fields

updateto   hb     AD   AdDate               Hub    HubDate

                    1        5         8/4/2016          null    null
                    6        null     null                   8         8/5/2016
                    7        7          8/1/2016         5         8/4/2016
                    11      11        8/3/2016         11       5/4/2016

so we will need to update the "updateto" field following way

for row 1) we compare AD and hub and since hub is null updateto = AD
row 2 ) we compare AD with hub and since AD is null   updateto = hub
row 3 ) we compare AD and hub with hb . since AD matches hb  updateto = AD
row 4 ) we compare AD and hub with hb and since they all three are same we compare addate with hubdate and since hubdate is higher  updateto = hub


there are more then 4 rows in the table. I have just given an example over here.

how do i write a sql statement to make this happen.
0
Comment
Question by:PratikShah111
[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
5 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 41742573
Give this a whirl..
UPDATE YourTable
SET updateto = CASE 
   WHEN hub IS NULL THEN AD
   WHEN AD IS NULL THEN hub
   WHEN AD = hub AND hub <> hb THEN AD
   WHEN AD = hub AND hub = hb AND addate < hubdate THEN hub END

Open in new window

For more info on CASE blocks check out SQL Server CASE Solutions
0
 
LVL 27

Expert Comment

by:Zberteoc
ID: 41742575
Try this:
--/*
if object_id('tempdb..#temp') is not null
    drop table #temp
create table #temp(updateto varchar(100), hb int, AD int, AddDate date, Hub int, HubDate date)
insert into #temp(hb, AD, AddDate, Hub, HubDate)
select * from
    (values
	   (1,        5,         '8/4/2016',          null,    null),
	   (6,        null,     null,                   8,         '8/5/2016'),
	   (7,        7,          '8/1/2016',         5,         '8/4/2016'),
	   (11,      11,        '8/3/2016',         11,       '5/4/2016')
    ) a(hb, AD, AdDate, Hub, HubDate)
--*/

select * from #temp

update #temp set
    updateto=
	   case 
		  when hub is null then AD
		  when AD is null then Hub
		  when AD=Hub then AD
		  when AD=Hub and Hub=hb and HubDate>AddDate then Hub 
		  else AD
    end


select * from #temp

Open in new window

0
 
LVL 30

Expert Comment

by:Olaf Doschke
ID: 41742754
In regard to the rules theres just one case missing, what if AD = hub AND hub = hb AND addate >= hubdate, I'd say that'll mean AD again. So

WHEN AD = hub AND hub = hb AND addate < hubdate THEN hub ELSE AD END

Bye, Olaf.
0
 
LVL 27

Expert Comment

by:Zberteoc
ID: 41742778
@Olaf,

This has nothing to do with the question.

Every time I read that "Bye, Olaf" ending formula it feels so definitive, like you will never come back, but still you do. :o)))

Talk to you later, Zberteoc. :o)
0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 41743997
What's happening when AD and HUB are both NULL?
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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…
Suggested Courses

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