0

我有一个相当大的信息表,需要针对几个不同的工作部门进行不同的过滤。我创建了以下查询,它返回相同数量的列,以便它们应该能够被 UNION 起来。我想确保没有返回重复的行,这就是我使用 UNION 的原因,但是我不断收到错误消息“Msg 8120, Level 16, State 1, Line 53 Column 'ACT_DW.dbo.INV-ACT.DNUM' is invalid在选择列表中,因为它不包含在聚合函数或 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
4

1 回答 1

0

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

于 2013-04-08T23:15:10.317 回答