Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

TSQL getting first date

Posted on 2012-04-11
6
Medium Priority
?
383 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 2000 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

715 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