Home Forums SQL Server 7,2000 T-SQL Combining Two Grouping Select Statements RE: Combining Two Grouping Select Statements

  • quote:


    To be more specific, I am trying to obtain the total of questionnaires in the

    query per "WSI" as opposed to the percentage from the whole.

    This select statement is being called from an ASP page as opposed to in the sql

    stored procedures etc.


    I think it would be better if you used more than one resultset, but:

    
    
    SELECT WSI, QDefectCount, DefectSum, CountAllDefects, SumAllDefects
    FROM
    (
    SELECT
    ISNULL(dbo.Defect_Data.UDL4, 'Unknown') AS WSI
    , COUNT(dbo.Defect_Data.DataID) AS QDefectCount
    , SUM(dbo.Defect_Data.SUMDEFECTS) AS DefectSum
    , (SUM(dbo.Defect_Data.SUMDEFECTS) / @SumDefectsTotal) * 100 AS DefectSumPercent
    FROM dbo.Defect_Data
    WHERE (DATETIME >= '2003050100000000')
    AND (DATETIME < '2003060100000000')
    AND SUMDEFECTS > 0
    GROUP BY dbo.Defect_Data.UDL4
    ORDER BY dbo.Defect_Data.UDL4
    )
    ,
    (
    SELECT COUNT(dbo.Defect_Data.DataID) AS CountAllDefects
    FROM dbo.Defect_Data
    WHERE (DATETIME >= '2003050100000000')
    and (DATETIME < '2003060100000000')
    AND SUMDEFECTS > 0
    )
    ,
    (
    SELECT SUM(dbo.Defect_Data.SUMDEFECTS) AS SumAllDefects
    FROM dbo.Defect_Data
    WHERE (DATETIME >= '2003050100000000')
    and (DATETIME < '2003060100000000')
    AND SUMDEFECTS > 0
    )