如何组合这两个存储过程

时间:2015-05-18 09:09:54

标签: sql sql-server stored-procedures

我正在尝试将这两个存储过程组合在一起,以便在它们自己的标题/列下返回两者的值,结果如下所示“支持工作时间”“支持工作时间”“工作开发时间”和“开发时间充电“

查询一个

USE [Database]
GO
/****** Object:  StoredProcedure [dbo].[usp_JobTimeSystem_FetchSupport] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[usp_JobTimeSystem_FetchSupport]

@FromDate                           datetime,
@ToDate                             datetime,
@SystemUserID                       uniqueidentifier

AS

;WITH cte AS (

SELECT
       DATEPART(Year, StartTime) AS YearNumber,
       DATEPART(Month, StartTime) AS MonthNumber,
       DateName(Month, StartTime) + ' ' + CAST(DatePart(Year, StartTime) AS nvarchar(50)) AS TimePeriod,
       DATEADD(day, DATEDIFF(Day, 0, StartTime), 0) AS FromDate,
       DateDiff(minute, StartTime, EndTime) AS JobTime,
       tblJobWorkLog.ChargeableTime

FROM
       tblJobWorkLog
       INNER JOIN tblJob ON tblJobWorkLog.JobID = tblJob.JobID
       INNER JOIN tblContact ON tblJob.ContactID = tblContact.ContactID

WHERE
       tblJobWorkLog.StartTime >= @FromDate
       AND tblJobWorkLog.EndTime <= @ToDate
       AND (WorkLogJobTypeID = 'FA5E6979-D228-44B7-A91B-8DDC8DDC709B' OR WorkLogJobTypeID = '3171B295-60E9-4724-95A3-04FA182D7D43' OR WorkLogJobTypeID = '52c2691f-ff0a-4263-a440-8a309f868f93')
       AND SystemUserID = @SystemUserID

)

SELECT
       FromDate,
       (SUM(JobTime) / 60.0) HoursWorked,
       SUM(ChargeableTime) AS HoursCharged

FROM
       cte

GROUP BY
       FromDate
ORDER BY
       FromDate

查询二

USE [Database]
GO
/****** Object:  StoredProcedure [dbo].[usp_JobTimeSystem_FetchDevelopment]    Script Date: 18/05/2015 09:23:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[usp_JobTimeSystem_FetchDevelopment]

@FromDate                           datetime,
@ToDate                             datetime,
@SystemUserID                       uniqueidentifier

AS

;WITH cte AS (

SELECT
       DATEPART(Year, StartTime) AS YearNumber,
       DATEPART(Month, StartTime) AS MonthNumber,
       DateName(Month, StartTime) + ' ' + CAST(DatePart(Year, StartTime) AS nvarchar(50)) AS TimePeriod,
       DATEADD(day, DATEDIFF(Day, 0, StartTime), 0) AS FromDate,
       DateDiff(minute, StartTime, EndTime) AS JobTime,
       tblJobWorkLog.ChargeableTime

FROM
       tblJobWorkLog
       INNER JOIN tblJob ON tblJobWorkLog.JobID = tblJob.JobID
       INNER JOIN tblContact ON tblJob.ContactID = tblContact.ContactID

WHERE

       tblJobWorkLog.StartTime >= @FromDate
       AND tblJobWorkLog.EndTime <= @ToDate
       AND (WorkLogJobTypeID = 'D0E910B1-B4BD-430C-AD04-EB4E67946806' OR WorkLogJobTypeID = 'B0BBF362-294D-4262-BED8-EDA7EE74745B' OR WorkLogJobTypeID = '1E333ADC-E4F2-4042-8B65-E25F2770D59F'
       OR WorkLogJobTypeID = 'A445B7CE-E9E4-48E6-B5AA-83C83F045315' OR WorkLogJobTypeID = '1D83F510-87FA-446E-9337-3D0376210D57' OR WorkLogJobTypeID = 'B59C1596-E1D0-4118-A805-65208E27AFB5' OR
       WorkLogJobTypeID = 'F44A4B3C-B149-45A8-A9F0-5A57883482FD')
       AND SystemUserID = @SystemUserID
)

SELECT
       FromDate,
       (SUM(JobTime) / 60.0) HoursWorked,
       SUM(ChargeableTime) AS HoursCharged

FROM
       cte

GROUP BY
       FromDate
ORDER BY
       FromDate

我知道肯定有一种方法将这些放在一起,但我几周前才开始使用SQL,并且真的不知道如何去做,一些指针甚至意味着很多,谢谢。

4 个答案:

答案 0 :(得分:0)

欢迎来到SQL的精彩世界!希望以下代码可以帮助您。

基本上使用两个查询之间的公共选择创建一个查询,并使用case语句从这两个查询中进一步筛选出您想要的唯一结果:

WITH cte AS (

SELECT
       DATEPART(Year, StartTime) AS YearNumber,
       DATEPART(Month, StartTime) AS MonthNumber,
       DateName(Month, StartTime) + ' ' + CAST(DatePart(Year, StartTime) AS nvarchar(50)) AS TimePeriod,
       DATEADD(day, DATEDIFF(Day, 0, StartTime), 0) AS FromDate,
       DateDiff(minute, StartTime, EndTime) AS JobTime,
       tblJobWorkLog.ChargeableTime,
       WorkLogJobTypeID

FROM
       tblJobWorkLog
       INNER JOIN tblJob ON tblJobWorkLog.JobID = tblJob.JobID
       INNER JOIN tblContact ON tblJob.ContactID = tblContact.ContactID

WHERE
       tblJobWorkLog.StartTime >= @FromDate
       AND tblJobWorkLog.EndTime <= @ToDate
       AND SystemUserID = @SystemUserID

)

SELECT
       FromDate,
       Case when WorkLogJobTypeID = 'FA5E6979-D228-44B7-A91B-8DDC8DDC709B' OR WorkLogJobTypeID = '3171B295-60E9-4724-95A3-04FA182D7D43' OR WorkLogJobTypeID = '52c2691f-ff0a-4263-a440-8a309f868f93' then (SUM(JobTime) / 60.0) end as SupportHoursWorked,
       Case when WorkLogJobTypeID = 'FA5E6979-D228-44B7-A91B-8DDC8DDC709B' OR WorkLogJobTypeID = '3171B295-60E9-4724-95A3-04FA182D7D43' OR WorkLogJobTypeID = '52c2691f-ff0a-4263-a440-8a309f868f93' then SUM(ChargeableTime) end AS SupportHoursCharged,
       Case when WorkLogJobTypeID = 'D0E910B1-B4BD-430C-AD04-EB4E67946806' OR WorkLogJobTypeID = 'B0BBF362-294D-4262-BED8-EDA7EE74745B' OR WorkLogJobTypeID = '1E333ADC-E4F2-4042-8B65-E25F2770D59F' OR WorkLogJobTypeID = 'A445B7CE-E9E4-48E6-B5AA-83C83F045315' 
       OR WorkLogJobTypeID = '1D83F510-87FA-446E-9337-3D0376210D57' OR WorkLogJobTypeID = 'B59C1596-E1D0-4118-A805-65208E27AFB5' OR WorkLogJobTypeID = 'F44A4B3C-B149-45A8-A9F0-5A57883482FD' then (SUM(JobTime) / 60.0) end as DevelopmentHoursWorked,
       Case when WorkLogJobTypeID = 'D0E910B1-B4BD-430C-AD04-EB4E67946806' OR WorkLogJobTypeID = 'B0BBF362-294D-4262-BED8-EDA7EE74745B' OR WorkLogJobTypeID = '1E333ADC-E4F2-4042-8B65-E25F2770D59F' OR WorkLogJobTypeID = 'A445B7CE-E9E4-48E6-B5AA-83C83F045315' 
       OR WorkLogJobTypeID = '1D83F510-87FA-446E-9337-3D0376210D57' OR WorkLogJobTypeID = 'B59C1596-E1D0-4118-A805-65208E27AFB5' OR WorkLogJobTypeID = 'F44A4B3C-B149-45A8-A9F0-5A57883482FD' then SUM(ChargeableTime) end as DevelopmentHoursCharged

FROM
       cte

GROUP BY
       FromDate, WorkLogJobTypeID
ORDER BY
       FromDate

答案 1 :(得分:0)

您可以在一个查询中定义2个CTE,然后将它们连接到UserID上,如下所示:

USE [Database]
GO
/****** Object:  StoredProcedure [dbo].[usp_JobTimeSystem_FetchSupport] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[usp_JobTimeSystem]

@FromDate                           datetime,
@ToDate                             datetime,
@SystemUserID                       uniqueidentifier

AS

;WITH cte AS (

SELECT
       DATEPART(Year, StartTime) AS YearNumber,
       DATEPART(Month, StartTime) AS MonthNumber,
       DateName(Month, StartTime) + ' ' + CAST(DatePart(Year, StartTime) AS nvarchar(50)) AS TimePeriod,
       DATEADD(day, DATEDIFF(Day, 0, StartTime), 0) AS FromDate,
       DateDiff(minute, StartTime, EndTime) AS JobTime,
       tblJobWorkLog.ChargeableTime

FROM
       tblJobWorkLog
       INNER JOIN tblJob ON tblJobWorkLog.JobID = tblJob.JobID
       INNER JOIN tblContact ON tblJob.ContactID = tblContact.ContactID

WHERE
       tblJobWorkLog.StartTime >= @FromDate
       AND tblJobWorkLog.EndTime <= @ToDate
       AND (WorkLogJobTypeID = 'FA5E6979-D228-44B7-A91B-8DDC8DDC709B' OR WorkLogJobTypeID = '3171B295-60E9-4724-95A3-04FA182D7D43' OR WorkLogJobTypeID = '52c2691f-ff0a-4263-a440-8a309f868f93')
       AND SystemUserID = @SystemUserID

)
, cte2 AS (

SELECT
       DATEPART(Year, StartTime) AS YearNumber,
       DATEPART(Month, StartTime) AS MonthNumber,
       DateName(Month, StartTime) + ' ' + CAST(DatePart(Year, StartTime) AS nvarchar(50)) AS TimePeriod,
       DATEADD(day, DATEDIFF(Day, 0, StartTime), 0) AS FromDate,
       DateDiff(minute, StartTime, EndTime) AS JobTime,
       tblJobWorkLog.ChargeableTime

FROM
       tblJobWorkLog
       INNER JOIN tblJob ON tblJobWorkLog.JobID = tblJob.JobID
       INNER JOIN tblContact ON tblJob.ContactID = tblContact.ContactID

WHERE

       tblJobWorkLog.StartTime >= @FromDate
       AND tblJobWorkLog.EndTime <= @ToDate
       AND (WorkLogJobTypeID = 'D0E910B1-B4BD-430C-AD04-EB4E67946806' OR WorkLogJobTypeID = 'B0BBF362-294D-4262-BED8-EDA7EE74745B' OR WorkLogJobTypeID = '1E333ADC-E4F2-4042-8B65-E25F2770D59F'
       OR WorkLogJobTypeID = 'A445B7CE-E9E4-48E6-B5AA-83C83F045315' OR WorkLogJobTypeID = '1D83F510-87FA-446E-9337-3D0376210D57' OR WorkLogJobTypeID = 'B59C1596-E1D0-4118-A805-65208E27AFB5' OR
       WorkLogJobTypeID = 'F44A4B3C-B149-45A8-A9F0-5A57883482FD')
       AND SystemUserID = @SystemUserID
)
SELECT
       a.FromDate,
       (SUM(a.JobTime) / 60.0) SupportHoursWorked,
       SUM(a.ChargeableTime) AS SupportHoursCharged,
       (SUM(b.JobTime) / 60.0) DevelopmentHoursWorked,
       SUM(b.ChargeableTime) AS DevelopmentHoursCharged


FROM
       cte as a

JOIN 
      cte2 as b
ON
     a.SystemUserID=b.SystemUserID

GROUP BY
       a.FromDate
ORDER BY
       a.FromDate

但显然没有测试数据,我只能猜测这会根据你在这里向我们展示的内容起作用。

答案 2 :(得分:0)

在使用现有例程的新程序中组合输出,如果需要更改程序,则维护较少。

CREATE PROCEDURE dbo.FetchHours
@FromDate datetime,
@ToDate datetime,
@SystemUserID uniqueidentifier
AS
    EXEC [dbo].[usp_JobTimeSystem_FetchSupport] @FromDate, @ToDate, @SystemUserID;
    EXEC [dbo].[usp_JobTimeSystem_FetchDevelopment] @FromDate, @ToDate, @SystemUserID;
GO

答案 3 :(得分:0)

如果存储过程返回结果集,则可以将结果保存到临时表中(如注释中提到的@ughai)。

类似的东西(我不确定语法是否正确):

INSERT INTO #temp1 
FROM
EXEC [dbo].[usp_JobTimeSystem_FetchSupport] @FromDate, @ToDate, @SystemUserID;

第二个查询相同:

INSERT INTO #temp2
FROM
EXEC [dbo].[usp_JobTimeSystem_FetchDevelopment] @FromDate, @ToDate, 
@SystemUserID;

然后你可以编写一个新的SELECT查询来合并上面的结果。 最好的方法是在单独的存储过程中关闭所有内容。

在使用它们之前,应先创建两个临时表。

一些伪代码:

CREATE PROCEDURE dbo.FetchHours
@FromDate datetime,
@ToDate datetime,
@SystemUserID uniqueidentifier
AS

    CREATE TABLE #temp1...
    CREATE TABLE #temp2...

    INSERT INTO #temp1 
    FROM
    EXEC [dbo].[usp_JobTimeSystem_FetchSupport] @FromDate, @ToDate, @SystemUserID;

    INSERT INTO #temp2
    FROM
    EXEC [dbo].[usp_JobTimeSystem_FetchDevelopment] @FromDate, @ToDate, 
    @SystemUserID;

    SELECT your columns
    FROM #temp1 JOIN #temp2 ON ...
    ORDER BY ...
GO

如果您的数据大小不是太大(超过数千行),您可以使用变量表而不是临时表。