Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 subqueries?

Author  Topic 

dlorenc
Posting Yak Master

172 Posts

Posted - 2011-04-28 : 14:23:58
How do I merge these two queries into a single query to return a single dataset? The problem Im trying to solve is to look at a table containing historical project information (I pull a snapshot every morning and add a datetime stamp), and identify the extent projects have slipped from their inception to current.

-- find the current plannedenddate
SELECT distinct pin, max(datamartdate) AS CurrentRecord,max(plannedenddate)as currentenddate
FROM epm_project
where projectstate='active' and plannedenddate is not null
group by pin order by pin

-- find the original plannedenddate
SELECT DISTINCT pin, min(datamartdate) as OldestRecords,min(plannedenddate) AS OldestPEndDate
FROM epm_project
where projectstate='active' and plannedenddate is not null
group by pin order by pin

once I get these two datasets together, I can calculate the enddate variance (slippage)....

dlorenc
Posting Yak Master

172 Posts

Posted - 2011-04-28 : 14:31:40
I'm getting dangerous...here is my solution...seems to work ok for me...

-- find Project slippage
SELECT distinct pin, max(datamartdate) AS CurrentRecord,max(plannedenddate)as currentenddate,min(datamartdate) as OldestRecords,min(plannedenddate) AS OldestPEndDate
, cast(max(plannedenddate)-min(plannedenddate) as int) as EndDateVariance
FROM epm_project
where projectstate='active' and plannedenddate is not null
group by pin order by pin
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-04-28 : 15:34:08
distinct doesn't do anything here.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

dlorenc
Posting Yak Master

172 Posts

Posted - 2011-04-29 : 12:24:03
the group by does the work?...thank you!
Go to Top of Page

dlorenc
Posting Yak Master

172 Posts

Posted - 2011-06-15 : 16:23:24
I found a problem with this approach...its in the max min... what I actually want is the plannedenddate from the oldest record in a group...and the plannedenddate from the newest record in a group...tell me where it started, and where it current is set for plannedenddate...

sooo..how do I select a value from the oldest member of a group..and a value from the youngest member of a group?.
Go to Top of Page
   

- Advertisement -