Sir My Procedure
USE [PGATE_GITS]
GO
/****** Object: StoredProcedure [dbo].[usp_GetTimeReport_Weekly1] Script Date: 03/29/2012 15:14:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[usp_GetTimeReport_Weekly1]
@pi_empId int,
--@pi_v_ep_id int,
--@pi_ep_id int,
@pi_rptFromDate varchar(10),
@pi_rptToDate varchar(10)
AS
BEGIN
declare @si_sql VARCHAR(1000)
SET @si_sql = 'SELECT EP_FIRST_NAME + '' '' + EP_LAST_NAME as [Employee_Name],'
SET @si_sql = @si_sql + 'CONVERT(VARCHAR(3),CONVERT(VARCHAR(3),
(SUM(DATEPART(HH,CONVERT (VARCHAR,GTT_NO_OF_HOURS,108))) + (SUM(DATEPART(MI,CONVERT (VARCHAR,GTT_NO_OF_HOURS,108)))/60))))+'':''+ CONVERT(VARCHAR(2),CONVERT(VARCHAR(2),
SUM(DATEPART(MI,CONVERT (VARCHAR,GTT_NO_OF_HOURS,108)))- 60 * (SUM(DATEPART(MI,CONVERT(VARCHAR,GTT_NO_OF_HOURS,108)))/60))+''0'') AS [No_of_Hours] '
--SET @si_sql = @si_sql + 'CONVERT(VARCHAR(3),CONVERT(VARCHAR(3),
-- (SUM(DATEPART(HH,CONVERT (VARCHAR,GTT_NO_OF_HOURS,108))) + (SUM(DATEPART(MI,CONVERT (VARCHAR,GTT_NO_OF_HOURS,108)))/60))))+'':''+ CONVERT(VARCHAR(2),CONVERT(VARCHAR(2),
-- SUM(DATEPART(MI,CONVERT (VARCHAR,GTT_NO_OF_HOURS,108)))- 60 * (SUM(DATEPART(MI,CONVERT(VARCHAR,GTT_NO_OF_HOURS,108)))/60))+''0'') - DATEDIFF(day,v_start_date,v_end_date)* 8 AS [twh] from PGATEINTRA.dbo.EMPLOYEE_VACATIONS '
SET @si_sql = @si_sql + ' FROM dbo.gitsTimeTracker '
SET @si_sql = @si_sql + ' inner JOIN dbo.vw_GetUserDetails ON gtt_gu_id = gu_id '
SET @si_sql = @si_sql + ' inner JOIN PGATEINTRA.dbo.EMPLOYEE_VACATIONS ON V_EP_ID = EP_ID '
SET @si_sql = @si_sql + ' WHERE GTT_WORK_DT BETWEEN ''' + @pi_rptFromDate + ''' AND ''' + @pi_rptToDate + ''' '
if isnull(@pi_empId, 0) > 0
SET @si_sql = @si_sql + ' AND gtt_gu_id = ' + CONVERT(varchar(3), @pi_empId)
SET @si_sql = @si_sql + ' GROUP BY [EP_FIRST_NAME], [EP_LAST_NAME] '
print @si_sql
exec(@si_sql)
--Record update process ends here
IF (@@ERROR = 0)
return @@ERROR
ELSE
return 1
END
我得到了上述程序的输出
Employee_Name No_of_hours
-------------------------------------------
Aditya Sastry A 96:00
Suresh A 18:00
Prakash Ajjarapu 12:40
我写了一个选择语句
SELECT DATEDIFF(day,v_start_date,v_end_date)* 8 AS twh from PGATEINTRA.dbo.EMPLOYEE_VACATIONS
我正在获得输出
twh
----
56
8
0
8
8
8
0
16
0
16
0
0
8
8
24
0
88
8
0
56
0
0
0
16
0
8
0
0
0
48
0
8
0
0
24
我需要从twh中减去No_of_hours,这必须在上面的过程中完成,并且必须在列中显示为twh
怎么做
答案 0 :(得分:0)
您的代码相当不清楚且难以理解,但是当您需要组合两个“复杂”查询的结果时,通常快速而肮脏的方法是将它们插入临时表,然后加入临时表。当然,这假定您包含一些合适的值来加入表,在这种情况下,它看起来像是某种员工ID。
换句话说,你的目标是能够写出这样的东西:
select
wh.employee_name,
wh.working_hours,
h.holiday_hours,
wh.working_hours - h.holiday_hours as 'total_hours'
from
#working_hours wh
join #holidays h
on wh.employee_id = h.employee_id
我认为没有人可以为您编写查询,因为它需要大量关于表和数据的知识,因此这只是您可以采用的一种方法的一般建议。