I am pulling through some data in SQL Server and I would like to GROUP BY my results but cannot get it to work.
This is my current SQL statement:
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
It returns these results:
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
I would like to be able to group the result by PAcontid.
Can anyone advise the easiest way to do this?
When I try and insert a GROUP BY clause I get an error stating that the other columns (CUSTNMBR, PAPROJNUMBER) etc are not included in the clause????
Any help much appreciated.
All work is being done in SQL Server Management Studio 2008 R2
Sorry for some reason my last column (Cost) didn;t come through. I have updated my question to show the proper fields. Basically what I am trying to do is get a total cost for each Pacontid.
I would like my results to look something like ……..
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
Is this possible in what I am trying to do?
When using
GROUP BY, all the selected columns must be in theGROUP BYclause or, if not, anaggregatefunction (ex:SUM,MAX,MIN,AVG) around it!For more info, check THIS.
In you case, a possible solution might be: