在同一个表TQSL上分组多个查询

时间:2013-04-08 23:04:38

标签: sql-server tsql group-by union

我有一个相当大的信息表,需要针对几个不同的工作部门进行不同的过滤。我创建了以下查询,返回相同数量的列,以便它们应该能够联合起来。我想确保没有返回重复的行,因此我使用UNION的原因,但我不断收到错误“消息8120,级别16,状态1,行53 列'ACT_DW.dbo.INV-ACT.DNUM'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。“
有人在这看到问题吗?我希望得到的只是一个大表,其中包含按工作组的Deskname字段分组的所有记录,如果可能的话。

    declare @InventoryAsOfEOD date,
        @Start date,
        @End date   


SET @Start = CAST(DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()),0) AS DATE)
SET @End = CAST(DATEADD(DAY, -(DAY(DATEADD(MONTH, 1, GETDATE()))), DATEADD(MONTH, 1, GETDATE())) as DATE)

IF
    DATENAME(DW, GETDATE()) = 'Monday'
BEGIN
    Set @InventoryAsOfEOD = CAST(DATEADD(D, -3, GETDATE())AS DATE) 
END
ELSE
    Set @InventoryAsOfEOD = CAST(DATEADD(D, -1, GETDATE()) AS DATE)

print @inventoryasofeod
print @start
print @end



SELECT 
    DNUM,
    SSN,
    DTRFIRSTNAME,
    DTRLASTNAME,
    ASSIGNDATE,
    [DESK],
    Deskname,
    PYMTAMT1,
    DueDate,
    PnI,
    PIBALATSETUP,
    Ttl_Balance,
    pmgroup,
    PROGTYPE,
    office,
    client,
    batch,
    placetype,
    pymtdt1,
    Ttl_Starts

FROM

(SELECT 
    DNUM,
    SSN,
    DTRFIRSTNAME,
    DTRLASTNAME,
    ASSIGNDATE,
    [DESK],
    Deskname,
    PYMTAMT1,
    PROGSTARTDT as DueDate,
    sum([Prinbal]+[INTBAL]) as PnI,
    PIBALATSETUP,
    BALANCE as Ttl_Balance,
    pmgroup,
    PROGTYPE,
    office,
    client,
    batch,
    placetype,
    pymtdt1,
    COUNT(SSN) as Ttl_Starts

FROM 
    [ACT_DW].[dbo].[INV-ACT] with(INDEX([IX_INV-ACT_InventoryAsOfEOD]),nolock)
WHERE 
    InventoryAsOfEOD = @InventoryAsOfEOD AND
    BC ='GSL' AND
    PROGTYPE not in('PMT PLAN', 'Fallout', 'PIF', 'SIF') AND
    PROGTYPE is not null and
    PYMTDT1 BETWEEN @Start AND @End


UNION 
--Get Oregon Starts

SELECT 
    DNUM,
    SSN,
    DTRFIRSTNAME,
    DTRLASTNAME,
    ASSIGNDATE,
    [DESK],
    Deskname,
    PYMTAMT1,
    PROGSTARTDT as DueDate,
    sum([Prinbal]+[INTBAL]) as PnI,
    PIBALATSETUP,
    BALANCE as Ttl_Balance,
    pmgroup,
    PROGTYPE,
    office,
    client,
    batch,
    placetype,
    pymtdt1,
    COUNT(SSN) as Ttl_Starts

FROM 
    [ACT_DW].[dbo].[INV-ACT] with(INDEX([IX_INV-ACT_InventoryAsOfEOD]),nolock)
WHERE
    InventoryAsOfEOD = @InventoryAsOfEOD and
    CLIENT like 'ORDOR%' and    
    PROGTYPE not in ('Null', 'Fallout') AND
    PYMTDT1 BETWEEN @Start AND @End


UNION 
--Get Perkins Starts

SELECT 
    DNUM,
    SSN,
    DTRFIRSTNAME,
    DTRLASTNAME,
    ASSIGNDATE,
    [DESK],
    Deskname,
    PYMTAMT1,
    PROGSTARTDT as DueDate,
    sum([Prinbal]+[INTBAL]) as PnI,
    PIBALATSETUP,
    BALANCE as Ttl_Balance,
    pmgroup,
    PROGTYPE,
    office,
    client,
    batch,
    placetype,
    pymtdt1,
    COUNT(SSN) as Ttl_Starts
FROM
    [ACT_DW].[dbo].[INV-ACT] with(INDEX([IX_INV-ACT_InventoryAsOfEOD]),nolock)
WHERE
    InventoryAsOfEOD = @InventoryAsOfEOD and
    BC in ('PE1', 'PE2', 'PE3', 'NU1', 'NU2', 'NDS') and
    PROGTYPE not in ('Null', 'Fallout') AND
    PYMTDT1 BETWEEN @Start AND @End

UNION 

--Get starts for Tuition

SELECT 
    DNUM,
    SSN,
    DTRFIRSTNAME,
    DTRLASTNAME,
    ASSIGNDATE,
    [DESK],
    Deskname,
    PYMTAMT1,
    PROGSTARTDT as DueDate,
    sum([Prinbal]+[INTBAL]) as PnI,
    PIBALATSETUP,
    BALANCE as Ttl_Balance,
    pmgroup,
    PROGTYPE,
    office,
    client,
    batch,
    placetype,
    pymtdt1,
    COUNT(SSN) as Ttl_Starts
FROM
    [ACT_DW].[dbo].[INV-ACT] with(INDEX([IX_INV-ACT_InventoryAsOfEOD]),nolock)
WHERE
    InventoryAsOfEOD = @InventoryAsOfEOD and
    BC in ('AR1', 'AR2', 'IN1', 'IN2', 'IN3', 'TU1', 'TU2', 'TU3') and
    PROGTYPE not in ('Null', 'Fallout') AND
    PYMTDT1 BETWEEN @Start AND @End

UNION 

--Get Starts for Chase

SELECT 
    DNUM,
    SSN,
    DTRFIRSTNAME,
    DTRLASTNAME,
    ASSIGNDATE,
    [DESK],
    Deskname,
    PYMTAMT1,
    PROGSTARTDT as DueDate,
    sum([Prinbal]+[INTBAL]) as PnI,
    PIBALATSETUP,
    BALANCE as Ttl_Balance,
    pmgroup,
    PROGTYPE,
    office,
    client,
    batch,
    placetype,
    pymtdt1,
    COUNT(SSN) as Ttl_Starts
FROM
    [ACT_DW].[dbo].[INV-ACT] with(INDEX([IX_INV-ACT_InventoryAsOfEOD]),nolock)
WHERE
    InventoryAsOfEOD = @InventoryAsOfEOD and
    CLIENT like 'AG%' and
    PROGTYPE not in ('Null', 'Fallout') AND
    PYMTDT1 BETWEEN @Start AND @End) 
    as StartsTable

GROUP BY 
    DNUM,
    SSN,
    DTRFIRSTNAME,
    DTRLASTNAME,
    ASSIGNDATE,
    PYMTAMT1,
    DueDate,
    PIBALATSETUP,
    Ttl_Balance,
    PROGTYPE,
    DESK,
    deskname,
    PMGROUP,
    OFFICE,
    CLIENT,
    BATCH,
    PLACETYPE,
    PYMTDT1
ORDER BY 
    PMGROUP,
    DESK

1 个答案:

答案 0 :(得分:0)

您需要为每个包含聚合的子查询包含GROUP BY子句。你可以改为联合行,然后然后聚合,但这将是一个不同的查询。