2011-12-22 92 views
0

我試圖讓2頁不同的表中進行選擇,其導致這樣的輸出:如何以不同的條件多次選擇一列?

Punchdate | EmployeeCode | Name | Punch_in | Break | Lunch | Second_start | Break | Punch out 
--------------------------------------------------------------------------------------------- 
11.08.2005  1021 John Smith 7:00 9:00  12:00 1:00   3:10  4:30 

dbo.Punchintervals

employee(dbo.punchintervals.employee=dbo.employee.objid) 
intime 
outime 
department/*(dbo.punchintervals.department = dbo.emprole.objid*/ 
/*(intime column has all the punches form all the departments,they need to be sorted trough the department column)*/ 

dbo.employee

employee/*(dbo.employee.objid=dbo.punchinterval.employee)*/ 
name 
code 

dbo.emprole

objid/*objid is just a vule example 10 (dbo.emprole.objid = dbo.punchinterval.department)*/ 
name/*(name of the role example: quality control, punch out, lunch,break)*/ 

到目前爲止,這是我最後的學嘗試

SELECT  TOP (100) 
    PUNCHDATE,CODE,name 
     ,( 
      SELECT [sitewatch].[dbo].Punchinterval.INTIME 
      FROM [sitewatch].[dbo].Punchinterval AS PunchIn 
      WHERE [sitewatch].[dbo].Punchinterval.DEPARTMENT = '10' 

     ) AS Startime 
     ,( 
      SELECT [sitewatch].[dbo].Punchinterval.INTIME 
      FROM [sitewatch].[dbo].Punchinterval AS PunchIn 
      WHERE [sitewatch].[dbo].Punchinterval.DEPARTMENT = '11' 
     ) AS break2 

FROM [sitewatch].[dbo].Punchinterval,[sitewatch].[dbo].employee 

回答

0
SELECT * FROM dbo.employee 
JOIN dbo.Punchintervals ON dbo.punchintervals.employee=dbo.employee.objid 
JOIN dbo.emprole ON dbo.emprole.objid = dbo.punchinterval.department 
+0

聯接是一個偉大的想法,但我們需要的條件來獲取正確的啓動時間,其中部門= 10,11,12或dbo.employeerole.name ='質量控制','生產','銷售','管理' – 2011-12-22 20:56:53

+0

您可以將這些標準添加到您的where子句;-) – ChristopherS 2011-12-22 21:09:10