Solved

TSQL getting first date

Posted on 2012-04-11
6
372 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
  • 4
  • 2
6 Comments
 
LVL 6

Expert Comment

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

Author Comment

by:gvamsimba
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 6

Expert Comment

by:yawkey13
Comment Utility
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
Comment Utility
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
Comment Utility
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

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…
In this article I will describe the Detach & Attach 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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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