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 2005 Forums
 Transact-SQL (2005)
 Help with fitting two part of code together...

Author  Topic 

cidr
Posting Yak Master

207 Posts

Posted - 2009-04-09 : 10:23:51
Hi folks,

I wonder if I can get help with the code below.

I created two views one as a subquery and the other using that subquery to produce the final results. However, I'd like to create one view rather than two views for the final results. I'm not sure where to put the subquery.

Here's the first part:(this is the subquery used in the Main query, it's called vSumOfHoursSub)

SELECT  Hours, JobCode, TimeLineDate, EmployeeId
FROM time.TimeLines
WHERE (NOT (JobCode LIKE '90001'))


I created a View with that and used it with the View below:


SELECT TOP (100) PERCENT DATEADD(wk, DATEDIFF(wk, 0, time.vSumOfHoursSub.TimeLineDate), 5) AS WeekEnding,
time.Employees.Employee, SUM(time.vSumOfHoursSub.Hours) AS SumOfHours
FROM time.Employees LEFT OUTER JOIN
time.vSumOfHoursSub ON time.Employees.EmployeeId = time.vSumOfHoursSub.EmployeeId
GROUP BY DATEADD(wk, DATEDIFF(wk, 0, time.vSumOfHoursSub.TimeLineDate), 5), time.Employees.Employee


As you can see, I omit the value 90001 from the results by using the first view. I want to be able to use the first query directly in the Main View. I just don't know how to do this.

Does anyone have any ideas that could help me?

Thanks again.


:)

ayamas
Aged Yak Warrior

552 Posts

Posted - 2009-04-09 : 10:35:11
SELECT TOP (100) PERCENT DATEADD(wk, DATEDIFF(wk, 0, time.vSumOfHoursSub.TimeLineDate), 5) AS WeekEnding,
time.Employees.Employee, SUM(time.vSumOfHoursSub.Hours) AS SumOfHours
FROM time.Employees LEFT OUTER JOIN
(
SELECT Hours, JobCode, TimeLineDate, EmployeeId
FROM time.TimeLines
WHERE (NOT (JobCode LIKE '90001'))
)vSumOfHoursSub
ON time.Employees.EmployeeId = time.vSumOfHoursSub.EmployeeId
GROUP BY DATEADD(wk, DATEDIFF(wk, 0, time.vSumOfHoursSub.TimeLineDate), 5), time.Employees.Employee
Go to Top of Page

cidr
Posting Yak Master

207 Posts

Posted - 2009-05-05 : 07:26:58
Ayamas, Thanks for your help and apologies I didn't get back to you sooner.

It worked well

Paul :)
Go to Top of Page
   

- Advertisement -