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

时间:2019-02-19 11:08:11

标签: sql sql-server

我有一个查询,其中包含2个我联合在一起的数据集。我正在处理每个数据集上的group by,因为我正在使用两种不同的产品类型,它们位于两个不同的表中,但每个表中都有相同的数据列。

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

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

(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)

如何将两个数据集整体分组?

2 个答案:

答案 0 :(得分:1)

如果您将脚本用作子查询并在外部使用GROUP BY,那么它将起作用;

    SELECT FieldNames
    FROM
    (   SELECT FieldNames
        FROM Table1
        GROUP BY FieldNames

        UNION

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

答案 1 :(得分:0)

仅对产品表使用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