Solved

TSQL getting first date

Posted on 2012-04-11
6
378 Views
Last Modified: 2012-04-12
Hi, below is my sample data from a table.I just want to create a new column
named 'Full_Date' which is nothing but the startdate from the below table
where typeid has been First changed to typeid 1 from typeid 2.

So, for the below date my new date from custid 1 should be 10/11/2002
and custid 2 should be 09/08/2004  as the column typeid was first changed to
typeid 1 on these dates.


Custid   Startdate   typeid
1        01/01/2001   2
1        09/10/2001   2
1        10/11/2002   1
2        09/10/2001   2
2        09/08/2004   1

Many Thanks
0
Comment
Question by:gvamsimba
[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
  • 2
6 Comments
 
LVL 6

Expert Comment

by:yawkey13
ID: 37833475
select custid, min(startdate) as full_date
from myTable
where typeid = 1
group by custid
0
 

Author Comment

by:gvamsimba
ID: 37833742
hi yawkey13,

but I want another column to be added to my table with all the data...but
i just want the min(startdate) when it was changed to 1 per custid...as per below-

Custid   Startdate   typeid  Full_Date
1        01/01/2001   2     10/11/2002
1        09/10/2001   2     10/11/2002
1        10/11/2002   1     10/11/2002
2        09/10/2001   2     09/08/2004
2        09/08/2004   1     09/08/2004

Thanks
0
 
LVL 6

Accepted Solution

by:
yawkey13 earned 500 total points
ID: 37833859
update myTable
set myTable.full_date = temp.full_date
from
      myTable join
      (
            select custid, max(startdate) as full_date
            from myTable
            where typeid = 1
            group by custid
      ) as temp
            on myTable.custid = temp.custid
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 6

Expert Comment

by:yawkey13
ID: 37833879
The above query is assuming that you have already added the full_date column to your table.  If you have not already added the column you will need to run the following first.

alter table myTable
add full_date DATETIME;
0
 
LVL 6

Expert Comment

by:yawkey13
ID: 37833913
Just out of curiosity, why are you wanting to add this data to your table?  In my opinion, for what it's worth, I would rather compute this data on output instead of saving the redundant data in the database.

Also, is this a one time update or do you have some mechanism for keeping this updated?  If you need to keep it updated and you don't already have code to do it, I would probably create a trigger if it needs to be updated in real time.  If it doesn't need to be updated real time, I'd create a job to run each night and update the column using the query above.
0
 

Author Comment

by:gvamsimba
ID: 37836892
update myTable
set myTable.full_date = temp.full_date
from
      myTable join
      (
            select custid, min(startdate) as full_date
            from myTable
            where typeid = 1
            group by custid
      ) as temp
            on myTable.custid = temp.custid
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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