SQL Server计数出现的值为colounns

时间:2014-04-01 07:27:26

标签: asp.net sql count rdlc

感谢您花时间阅读本文。

我有一个ServiceDetails表,其中包含

ID, ServiceID , ClientID... , Status ,IsFollowUp

和服务

ID, Date , CityID, AreaID

现在,当输入服务请求时,其状态为'pending', 'Completed','testing', or 'indeteriminent'

现在最终用户需要一份报告

城市,地区,TotalServices,总计未完成后续完成,第一次跟进完成总数,第二次跟进完成总数......,第五次跟进完成总数

我已完成现在已完成,已完成但没有followup,但如何计算Completed服务followups计数。

CREATE TABLE #TEMP#(
    [ID] int PRIMARY KEY IDENTITY,
    [Area] varchar(250),
    [City] varchar(250),
    [Total] int,
    [WithoutFollowup] int,
    [FirstFollowup] int,
    [SecondFollowup] int,
    [ThirdFollowup] int,
    [FourthFollowup] int,
    [FifthFollowup] int
);

    DECLARE @AreaID AS bigint = 0
    DECLARE @CityID AS bigint = 0
    DECLARE @AreaName AS nvarchar(250) = ''
    DECLARE @CityName AS nvarchar(250) = ''

DECLARE @VCCTDetailsID AS bigint = NULL, @ClientID AS bigint = NULL
        ,@TotalTests as int, @WithoutFollowup as int, @FirstFollowup as int,@SecondFollowup as int, @ThirdFollowup as int, @FourthFollowup as int, @FifthFollowup as int
        ,@Org as varchar(250),@City as varchar(250)

DECLARE cur CURSOR FOR 

    SELECT  Areas.ID, Areas.Name, Cities.ID, Cities.CityName
    FROM    [dbo].[Areas]
        INNER JOIN  [dbo].[AreaCities] ON Areas.ID = AreaCities.AreaID
        INNER JOIN  [dbo].[Cities] ON AreaCities.CityID = Cities.ID
        INNER JOIN  [dbo].[States] ON States.ID = Cities.StateID
        INNER JOIN  [dbo].[Countries] ON Countries.ID = States.CountryID
    WHERE   [Areas].[IsActive] = 1
        AND [Cities].[IsActive] = 1
        AND [Areas].[CountryID] = 168

OPEN cur

FETCH NEXT FROM cur INTO @AreaID, @AreaName, @CityID, @CityName
    WHILE @@FETCH_STATUS = 0
    BEGIN

        SET @Total = (
            SELECT  COUNT(1)
            FROM    [dbo].[ServiceDetails]
                INNER JOIN  [dbo].[Services] ON [ServiceDetails].[ServiceID] =  [Services].[ID]
            Where   [ServiceDetails].[Status] !='Testing'
                AND [ServiceDetails].[Status] !='Pending'
                AND [Services].[AreaID] = @AreaID
                AND [Services].[CityID] = @CityID
            GROUP BY    [Services].[AreaID],[Services].[CityID]                                
        )
        SET @WithoutFollowup = (
            SELECT  COUNT(1)
            FROM    [dbo].[ServiceDetails]
                INNER JOIN  [dbo].[Services] ON [ServiceDetails].[ServiceID] =  [Services].[ID]
            Where   [ServiceDetails].[Status] !='completed'
                AND [ServiceDetails].[IsFollowUp] = 'false'
                AND [Services].[AreaID] = @AreaID
                AND [Services].[CityID] = @CityID
            GROUP BY    [Services].[AreaID],[Services].[CityID]                                
        )
        SET @FirstFollowup = (
            SELECT  COUNT(1)
            FROM    [dbo].[ServiceDetails]
                INNER JOIN  [dbo].[Services] ON [ServiceDetails].[ServiceID] =  [Services].[ID]
            Where   [ServiceDetails].[Status] !='completed'
                AND [ServiceDetails].[IsFollowUp] = 'True'
            GROUP BY    [Services].[AreaID],[Services].[CityID]                              
        )

        INSERT #TEMP# ([Org],[City],[Total],[WithoutFollowup],[FirstFollowup],[SecondFollowup],[ThirdFollowup],[FourthFollowup],[FifthFollowup]) 
        VALUES(@AreaName,@CityName,@Total,@WithoutFollowup,@FirstFollowup,@SecondFollowup,@ThirdFollowup,@FourthFollowup,@FifthFollowup);

        FETCH NEXT FROM cur INTO @AreaID, @AreaName, @CityID, @CityName
    END
CLOSE cur
DEALLOCATE cur

SELECT * FROM #TEMP#    
DROP TABLE #TEMP#

1 个答案:

答案 0 :(得分:0)

我已使用rownumbers和光标内的另一个temporary Table完成此任务

INSERT INTO #Services# 
SELECT  ROW_NUMBER() OVER (ORDER BY [Services].[Date]) as 'RowNo',[ServiceDetails].* , [Services].[Date]
FROM    [ServiceDetails]
    INNER JOIN  [Services] ON [Services].[ID] = [ServiceDetails].[VCCTsServiceID]
    INNER JOIN  [Clients] ON [Clients].[ID] = [ServiceDetails].[ClientID]
WHERE   [Clients].[ID] LIKE @ClientID       
ORDER BY    [Services].[Date]

#Services#ServiceDetails表的精确副本。现在,我们通过rownumberDate为客户提供了所有服务。它是按日期排序的,因此我们会在完成后完全跟进。现在您可以查询您想要的内容。

SET @TotalServices = ( SELECT COUNT(*) FROM #Services# )
SET @FirstSericeDate = (SELECT  [#Services#].[Date] FROM    #Services# WHERE [#Services#].[RowNo] = 1 )
SET @Status = (SELECT [#Services#].[Status] FROM #Services# WHERE [#Services#].[RowNo] = 1 )
SET @Date1 = (SELECT [#Services#].[Date] FROM #Services# WHERE [#Services#].[RowNo] = 2 )
SET @Status1 = (SELECT [#Services#].[Status] FROM #Services# WHERE [#Services#].[RowNo] = 2)

然后将您的变量插入主temptable以进行报告

INSERT #TEMP# ([RegNo], . . . [Status], Date1], [Status1]  . .)  
VALUES(@RegNo,  . .. @Status, @Date1 , @Status1, . . );
TRUNCATE TABLE #Services#

FETCH NEXT FROM cur INTO @RegNo