Solved

Sql query for update and inner join

Posted on 2011-02-21
4
393 Views
Last Modified: 2012-05-11
Hi,
I have a inner join sql query to get startDate from table Personal_details and EndDate from table'CU_CABLES_PRES_PEN_DETAIL'

The requirement is to have start date one day after end date.If a null Date is found for end date I need to insert the value in it.


How can I create a query to achieve this

Query inner join:

SELECT a.refno, a.DATELEFT as dateleft , b.CU_CABPPD_StartDate as startDate FROM PERSONAL_DETAILS as a INNER JOIN CU_CABLES_PRES_PEN_DETAIL as  b  ON a.REFNO = b.refno

The data current has some null end dates which need to be filled in appropriately.
Some records where end date is equal to startdate
Some records where end date is after startdate


Many thanks in advance

Cheers


Refno       StartDate                      EndDate
10001      01/08/2005 00:00      NULL
10008      01/08/2005 00:00      NULL
10009      12/11/2001 00:00      11/11/2001 00:00
10010      18/08/2001 00:00      17/08/2001 00:00
10015      01/08/2005 00:00      NULL
10017      01/02/2002 00:00      31/01/2002 00:00
10018      01/08/2005 00:00      31/07/2005 00:00
10020      01/11/2002 00:00      31/10/2002 00:00
10021      05/02/2001 00:00      04/02/2001 00:00
10022      06/02/2001 00:00      06/02/2001 00:00
10023      07/02/2001 00:00      08/02/2001 00:00
0
Comment
Question by:RIAS
4 Comments
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 166 total points
ID: 34942058
I think you query must be like this
SELECT a.refno, a.DATELEFT as startdate, b.CU_CABPPD_StartDate as enddate FROM PERSONAL_DETAILS as a INNER JOIN CU_CABLES_PRES_PEN_DETAIL as  b  ON a.REFNO = b.refno

then

Update CU_CABLES_PRES_PEN_DETAIL
set CU_CABPPD_StartDate = (
SELECT dateadd("day" ,1, a.DATELEFT) FROM PERSONAL_DETAILS Where a.REFNO = CU_CABLES_PRES_PEN_DETAIL.refno
)
0
 
LVL 11

Accepted Solution

by:
rajvja earned 167 total points
ID: 34942082
UPDATE mytab SET mytab.EndDate = CASE WHEN mytab.EndDate IS NULL THEN mytab.DATEADD(dd,1,mytab.StartDate) ELSE mytab.EndDate END FROM mytable mytab
INNER JOIN
(
SELECT a.refno, a.DATELEFT as dateleft , b.CU_CABPPD_StartDate as startDate FROM PERSONAL_DETAILS as a INNER JOIN CU_CABLES_PRES_PEN_DETAIL as  b  ON a.REFNO = b.refno
) tab
ON mytab.REFNO = tab.REFNO
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 167 total points
ID: 34942099
I have a nice article for this kind of request to learn how to write it:
http://www.experts-exchange.com/A_1517.html
0
 

Author Closing Comment

by:RIAS
ID: 34942134
Cheers mates!!!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now