向具有不同参数的表添加列

时间:2014-07-13 23:04:13

标签: sql sql-server

我正在尝试将2个表输入到一个输出中,我的问题是该参数是从2个不同的列中提取的。我需要能够显示已完成的内容和一个月内订购的工作。编写脚本后我没有收到任何错误,但是3小时后我就停止了运行。

如果我将@Ordered上的联接从iAuditID更改为EffectiveDate,则需要几秒钟才能运行,但它不会提取正确的信息。我只需要在@data表中正确排序有序审核的数量并且正确无误。可能在工作完成前几个月订购了工作,因此我需要能够看到每个月有多少工作要做,以及正在完成的工作。

我正在使用SQL 2008。

DECLARE 
@StartDate datetime,
@EndDate datetime,
@iClientID int,
@iServiceLevelID int

SET @StartDate = '1-1-13'
SET @EndDate = '12-30-13'

SET DATEFIRST 7

DECLARE @TB1 TABLE (EffectiveDate datetime, 
                    iAuditID int,
                    iClientID int, 
                    iServiceLevelID int)
INSERT INTO @TB1

SELECT dateadd(month, datediff(month, 0, a.dtClosedDate),0) AS EffectiveDate, 
       a.iAuditID, a.iClientID, a.iServiceLevelID               
FROM vwtblAudit a    
WHERE (a.dtClosedDate >= @StartDate) 
AND (a.dtClosedDate <= @EndDate) 
AND (a.iClientID = @iClientID OR @iClientID is null) 
AND (a.iServiceLevelID = @iServiceLevelID or @iServiceLevelID is null)
GROUP BY dateadd(month, datediff(month, 0, a.dtClosedDate),0),
    a.iClientID, a.iAuditID, a.iServiceLevelID

DECLARE @Ordered TABLE (EffectiveDate datetime, 
                        iAuditID int, 
                        iClientID int, 
                        iServiceLevelID int, 
                        dtOpenDate int)

INSERT INTO @Ordered    
SELECT dateadd(month, datediff(month, 0, a.dtOpenDate),0) AS EffectiveDate, 
    a.iAuditID, a.iClientID, a.iServiceLevelID,
    Count(a.dtOpenDate) as Ordered
FROM tblAudit a
WHERE (a.dtOpenDate >= @StartDate) 
AND (a.dtOpenDate <= @EndDate) 
AND (a.iClientID = @iClientID OR @iClientID is null) 
AND (a.iServiceLevelID = @iServiceLevelID or @iServiceLevelID is null)
GROUP BY dateadd(month, datediff(month, 0, a.dtOpenDate),0), 
    a.iClientID, a.iServiceLevelID, a.iAuditID
order by iClientID, EffectiveDate   

DECLARE @DATA table(iclientID int, 
                    sClientCode varchar(8), 
                    sClientName varchar(50), 
                    iServiceLevelID int,
                    sServiceLevelName varchar(50), 
                    EffectiveDate datetime, 
                    iAuditCount int, 
                    Completed int)

SELECT tblClient.iclientID, tblClient.sClientCode, tblClient.sClientName, 
       tblServiceLevel.iServiceLevelID, 
       TB1.EffectiveDate, COUNT(*) AS iAuditCount, 
       COUNT(Ordered.dtOpenDate) as Ordered    
FROM    @TB1 TB1 INNER JOIN
    tblClient ON TB1.iClientID = tblClient.iClientID INNER JOIN
    tblServiceLevel ON TB1.iServiceLevelID = tblServiceLevel.iServiceLevelID
    join @Ordered Ordered on TB1.EffectiveDate=Ordered.EffectiveDate
GROUP BY tblClient.iClientID, TB1.iServiceLevelID, tblclient.sClientCode, 
         tblClient.sClientName, tblServiceLevel.iServiceLevelID, 
         tblServiceLevel.sServiceLevelName, TB1.EffectiveDate 
return

SET DATEFIRST 7

@data中的每一个都应该按照它的方式工作,但是我被要求添加正在进行的工作,所以我添加了@ordered表来引入正确的信息。除了有序列之外,数据仍然是正确的。见下面的示例:

clientID  ClientCode ClientName iServiceLevelID EffectiveDate iAuditCount Ordered
 0001         1001      Sample         1            Jan 2014       104        19(this number should be 134)

0 个答案:

没有答案