0

我正在提取 SQL Server 中的一些数据,我想要GROUP BY我的结果,但无法让它工作。

这是我当前的 SQL 语句:

SELECT
   PA01201.CUSTNMBR, 
   PA01201.PACONTNUMBER, 
   PA01201.PAcontid, 
   PA01100.CUSTNMBR AS Expr1, 
   PA01100.PAcontname, 
   PA01100.PAcontid AS Expr2, 
   PA01100.PACONTNUMBER AS Expr3, 
   PA01201.PAprojname, 
   PA01201.PAPROJNUMBER
   PA01201.Cost

FROM        
   PA01201 
INNER JOIN
   PA01100 ON PA01201.CUSTNMBR = PA01100.CUSTNMBR AND 
              PA01201.PACONTNUMBER = PA01100.PACONTNUMBER AND 
              PA01201.PAcontid = PA01100.PAcontid 

它返回以下结果:

CUSTNMBR    PACONTNUMBER    PAcontid    Expr1   Pacontname  Expr2   Expr3   Paprojname  PAPROJNUMBER    Cost
AD001           8051    8051    AD001           Company 1   8051    8051    Project 1   08051P          951
AP001           8063    8063    AP001           Company 2   8063    8063    Project 2   08063P          2651
AR002           8096    8096    AR002           Company 3   8096    8096    Project 3   08096P          807
AR003           9033    9033    AR003           Company 4   9033    9033    Project 4   09033P          458
AS001           8013    8013    AS001           Company 5   8013    8013    Project 5   08013C          3297
AS001           8013    8013    AS001           Company 5   8013    8013    Project 5   08013P          48
AS001           8013    8013    AS001           Company 5   8013    8013    Project 5   08013P          641
AR002           8096    8096    AR002           Company 3   8096    8096    Project 3   08096P          1000

我希望能够将结果分组PAcontid

谁能建议最简单的方法来做到这一点?

当我尝试插入一个GROUP BY子句时,我收到一条错误消息,指出该子句中不包含其他列(CUSTNMBR、PAPROJNUMBER)等????

非常感谢任何帮助。

所有工作都在 SQL Server Management Studio 2008 R2 中完成


抱歉,由于某种原因,我的最后一篇专栏(成本)没有通过。我已经更新了我的问题以显示正确的字段。基本上我想要做的是获得每个 Pacontid 的总成本。

我希望我的结果看起来像........

CUSTNMBR    PACONTNUMBER    PAcontid    Expr1   Pacontname  Expr2   Expr3   Paprojname  PAPROJNUMBER    Cost
    AS001           8013    8013    AS001           Company 5   8013    8013    Project 5   08013P          3986
    AD001           8051    8051    AD001           Company 1   8051    8051    Project 1   08051P          951
    AP001           8063    8063    AP001           Company 2   8063    8063    Project 2   08063P          2651
    AR002           8096    8096    AR002           Company 3   8096    8096    Project 3   08096P          1807
    AR003           9033    9033    AR003           Company 4   9033    9033    Project 4   09033P          458

这可能在我想做的事情中吗?

4

3 回答 3

1

使用时GROUP BY,所有选定的列都必须在GROUP BY子句中,如果不是,则必须在它周围的aggregate函数(例如:SUM、、、、MAXMINAVG

有关更多信息,请查看


在您的情况下,可能的解决方案可能是:

SELECT  MAX(PA01201.CUSTNMBR),
        MAX(PA01201.PACONTNUMBER),
        PA01201.PAcontid,
        MAX(PA01100.CUSTNMBR AS Expr1,
        MAX(PA01100.PAcontname),
        MAX(PA01100.PAcontid) AS Expr2,
        MAX(PA01100.PACONTNUMBER) AS Expr3,
        MAX(PA01201.PAprojname),
        MAX(PA01201.PAPROJNUMBER)
FROM       PA01201
INNER JOIN PA01100 ON PA01201.CUSTNMBR = PA01100.CUSTNMBR
       AND PA01201.PACONTNUMBER = PA01100.PACONTNUMBER
       AND PA01201.PAcontid = PA01100.PAcontid
GROUP BY PA01201.PAcontid
于 2012-06-19T12:05:38.860 回答
1

GROUP BY在子句中添加所有选定的列。但首先从所需的列开始:PAcontid.

SELECT
    PA01201.CUSTNMBR, 
    PA01201.PACONTNUMBER, 
    PA01201.PAcontid, 
    PA01100.CUSTNMBR AS Expr1, 
    PA01100.PAcontname, 
    PA01100.PAcontid AS Expr2, 
    PA01100.PACONTNUMBER AS Expr3, 
    PA01201.PAprojname, PA01201.PAPROJNUMBER
FROM
    PA01201 
INNER JOIN
    PA01100 ON PA01201.CUSTNMBR = PA01100.CUSTNMBR AND 
    PA01201.PACONTNUMBER = PA01100.PACONTNUMBER AND 
    PA01201.PAcontid = PA01100.PAcontid 
GROUP BY 
    PAcontid, CUSTNMBR, PACONTNUMBER, PAcontname, PAprojname, PAPROJNUMBER
于 2012-06-19T12:08:31.290 回答
1

如果您添加了一个

   Order by CUSTNMBR , PAcontid

这将为您提供左侧的客户列表,然后是 PAcontid 组中的数据。

或者

   Order by PAcontid ,CUSTNMBR 

这将按 PAcontid 对其进行排序,然后将客户按顺序排列..

通常你会说

ann apple 10
pete strawberry 3
Lucy strawberry 12

select fruit, sum(count) as total from lunch group by fruit

要得到

apple 10
strawbery 15
于 2012-06-19T12:21:10.673 回答