如何在SQL Server 2008中对多个数据集进行分组

如何在SQL Server 2008中对多个数据集进行分组,sql,sql-server,Sql,Sql Server,我有一个查询,它有两个数据集,我将它们联合在一起。我对每个数据集进行了分组,因为我正在处理两个不同的产品类型,它们位于两个不同的表中,但每个表中都有相同的数据列 我想将数据集作为一个整体进行分组,但我尝试的任何东西似乎都不起作用 下面是我到目前为止的代码 (SELECT Distinct max(Incep) as Startdate, q1.Client, max(q1.Ref) as Reference, Max(Title) as Title,

我有一个查询,它有两个数据集,我将它们联合在一起。我对每个数据集进行了分组,因为我正在处理两个不同的产品类型,它们位于两个不同的表中,但每个表中都有相同的数据列

我想将数据集作为一个整体进行分组,但我尝试的任何东西似乎都不起作用

下面是我到目前为止的代码

(SELECT Distinct 
    max(Incep) as Startdate, 
    q1.Client, 
    max(q1.Ref) as Reference, 
    Max(Title) as Title,
    Max(Forename)as Firstname, 
    Max(Surname) as Lastname, 
    max(Addr1) as Line1, 
    Max(Addr2)as line2, 
    max(Addr3) as line3, 
    MAX(addr4) as Line4,
    Max(a1.Postcode) as Postcode, 
    max(Abode) as Abodetype,  
    Max(Phone) as Phone,
    Max(fax) as Faxnumber, 
    max(Field1) as Marketing,
    Max(a1.Age) as Ageofclient, 
    Max(Test)Passeddate, 
    max(Resdate) as residencydate, 
    Max(Occup) as Occupation, 
    max(EOccup) as industry, 
    Max(RESTR) as DriverRestrictions, 
    Max(Cover) As Covertype,  
    MAx(BONUS) As NCBYears, 
    Max([USE]) as Classofuse, 
    max(REG) as VehicleReg, 
    max(ABI) as VehicleABI, 
    Max(BOUGHT) as PurchasedDate, 
    max(Seats) as VehicleSeats, 
    MAX(a2.YEAR) as Yearofmake, 
    Max(garage) as Overnightlocation 
FROM Quotes q1
    left join agents QZ on QZ.CDLCode =q1.Op collate SQL_Latin1_General_CP1_CI_AS
    left join MIS_O_C a1 on a1.Client = q1.client
    left join MIS_O_PPC a2 on a2.Client = q1.client and a2.Ref = Q1.Ref
WHERE EDate > GETDATE()-180
    and reportgroup not in ('Renewals', 'Underwriting', 'Exclude')
    and Occup not in ('Delivery Courier','Courier')
    and Field1 = 'CROSS'
    and SEATS > '8'
    and [USE] not in ('3')
    and Source in ('DIREC', '1A984', '1A997','1A982', '1A998') 
    and Ptype = 'PC'
GROUP BY q1.Client

UNION ALL

SELECT Distinct 
    max(Incep) as Startdate, 
    q1.Client, 
    max(q1.Ref) as Reference, 
    Max(Title) as Title,
    Max(Forename)as Firstname, 
    Max(Surname) as Lastname, 
    max(Addr1) as Line1, 
    Max(Addr2)as line2, 
    max(Addr3) as line3, 
    MAX(addr4) as Line4,
    Max(a1.Postcode) as Postcode, 
    max(Abode) as Abodetype,
    Max(Phone) as Phone,
    Max(fax) as Faxnumber, 
    max(Field1) as Marketing,  
    Max(a1.Age) as Ageofclient, 
    Max(Test)Passeddate, 
    max(Resdate) as residencydate, 
    Max(Occup) as Occupation, 
    max(EOccup) as industry, 
    Max(RESTR) as DriverRestrictions, 
    Max(Cover) As Covertype,  
    MAx(BONUS) As NCBYears, 
    Max([USE]) as Classofuse, 
    max(REG) as VehicleReg, 
    max(ABI) as VehicleABI, 
    Max(BOUGHT) as PurchasedDate, 
    max(Seats) as VehicleSeats, 
    MAX(a2.YEAR) as Yearofmake, 
    Max(garage) as Overnightlocation 
FROM Quotes q1
    left join agents QZ on QZ.CDLCode =q1.Op collate SQL_Latin1_General_CP1_CI_AS
    left join MIS_O_C a1 on a1.Client = q1.client
    left join MIS_O_PGV a2 on a2.Client = q1.client 
        and a2.Ref = Q1.Ref
WHERE EDate > GETDATE()-180
    and reportgroup not in ('Renewals', 'Underwriting', 'Exclude')
    and Occup not in ('Delivery Courier','Courier')
    and Field1 = 'CROSS'
    and SEATS > '8'
    and [USE] not in ('3')
    and Source in ('DIREC', '1A984', '1A997','1A982', '1A998') 
    and Ptype = 'GV'
GROUP BY q1.Client)

如何将这两个数据集作为一个整体进行分组?

如果将脚本用作子查询并按外部分组,那么它将起作用

    SELECT FieldNames
    FROM
    (   SELECT FieldNames
        FROM Table1
        GROUP BY FieldNames

        UNION

        SELECT FieldNames
        FROM Table2
        GROUP BY FieldNames
    ) a
    GROUP BY FieldNames

仅对产品表使用UNION ALL,而不是对所有查询使用UNION ALL

Select Distinct max(Incep) as Startdate, q1.Client, max(q1.Ref) as Reference, Max(Title) as Title,Max(Forename)as Firstname, Max(Surname) as Lastname, max(Addr1) as Line1, Max(Addr2)as line2, max(Addr3) as line3, MAX(addr4) as Line4,Max(a1.Postcode) as Postcode, max(Abode) as Abodetype,  Max(Phone) as Phone,  Max(fax) as Faxnumber, max(Field1) as Marketing,  
    Max(a1.Age) as Ageofclient, Max(Test)Passeddate, max(Resdate) as residencydate, Max(Occup) as Occupation, max(EOccup) as industry, Max(RESTR) as DriverRestrictions, Max(Cover) As Covertype,  MAx(BONUS) As NCBYears, Max([USE]) as Classofuse, max(REG) as VehicleReg, max(ABI) as VehicleABI, Max(BOUGHT) as PurchasedDate, max(Seats) as VehicleSeats, MAX(a2.YEAR) as Yearofmake, Max(garage) as Overnightlocation from Quotes q1
    left join agents QZ on QZ.CDLCode =q1.Op collate SQL_Latin1_General_CP1_CI_AS
    left join MIS_O_C a1 on a1.Client = q1.client
    left join (
        SELECT * FROM MIS_O_PPC WHERE Ptype = 'PC'
        WHERE
        UNION ALL
        SELECT * FROM MIS_O_PGV WHERE Ptype = 'GV'
    ) a2 on a2.Client = q1.client and a2.Ref = Q1.Ref
    where EDate > GETDATE()-180
    and reportgroup not in ('Renewals', 'Underwriting', 'Exclude')
    and Occup not in ('Delivery Courier','Courier')
    and Field1 = 'CROSS'
    and SEATS > '8'
    and [USE] not in ('3')
    and Source in ('DIREC', '1A984', '1A997','1A982', '1A998') 
    Group by q1.Client

您确定无法避免使用
联合所有
?我所看到的两个查询之间的唯一区别是Ptype过滤器,您可以在('PC','GV')中写入'and Ptype