0

I have a sql query that runs fine but it returns results from 2 different tables that contain a breakdown of contracts and the projects that belong to them.

An example of this is: Contract number 12004 contains Projects 12004C, 12004D, 12004F

is there a way I can get all 12004 to group together under the 12004 banner contract Number?

My query as it stands to get me the current info is:

SELECT     PA01201.PACONTNUMBER, PA01201.PAPROJNUMBER, PA01201.PAprojname, PA01100.PAcontname
FROM         PA01201 INNER JOIN
             PA01100 ON PA01201.PACONTNUMBER = PA01100.PACONTNUMBER

basically i am trying to get all the figures from PAPROJNUMBERS (C,D,F etc) to form one line of a subtotal under PACONTNUMBER

I have tried a 'Group By' but get a Ambiguous column name 'PACONTNUMBER'???

Any help at all much appreciated.


Thanks for the help. Much appreciated. I will keep trying different things.

In response to using aggregates that's not really what I was trying to do.

Basically in my example of projects 12004C, 12004D, 12004F etc I just want them all to wrap up under 12004. so it would look something like this..............

Contract Figures Description:-

12004    25000  SS Bus Station
12005    xxxxx  xxxxxxxx
12006    xxxxx  xxxxxxxx
12007    xxxxx  xxxxxxxx
12008    xxxxx  xxxxxxxx

instead of how it looks at the moment:-

Contract Figures Description

12004     6000  SS Bus Station
12004C 8000     SS Bus Station
12004D 1000 SS Bus Station
12004F 10000    SS Bus Station
12005   xxxxx   xxxxxx
user1086159
  • 1,045
  • 5
  • 16
  • 24
  • 3
    What database engine? WM_Concat or ListAgg in Oracle, MySQL uses Group_concat, MS:SQL has it's own flavor: do a search in stack and on either WM_Concat or Group_Concat for your DB engine – xQbert Dec 19 '11 at 18:02
  • what about just using order by. order by PA01201.PACONTNUMBE, PA01201PAPROJNUMBER. (based on additional information provided in an answer.) – xQbert Dec 20 '11 at 11:17
  • I tried this but as the other information being pulled through is financial numbers it still gives out 12004, 12004D, 12004F instead of just one line for 12004 that incorporates all the various 12004. – user1086159 Dec 20 '11 at 11:50
  • could you update your contract figures description to show the desired output? I'm not understanding what you're looking for yet. – xQbert Dec 20 '11 at 11:53
  • I have updated the Contract figures example. As you can see we have a total of 25000 for SS Bus Station. But this is made up of differnt projects all with the same contract code sepertaed by a letter. I am trying to get a 1 line summary of each contract so it would read 12004 25000 and then move on to 12005. – user1086159 Dec 20 '11 at 12:09
  • Ok, you need to use group by and aggregrates you want to SUM(col2) and group by col3 and min(col1) – xQbert Dec 20 '11 at 12:14

3 Answers3

0

If you get a warning about an ambiguous name, it's because you mention a column that's in more than one table, and the database doesn't know which table you mean.

That being said, depending on the database engine you have (SQLServer, MySQL, Oracle), some have a "concat" function for aggregation.

MPelletier
  • 16,256
  • 15
  • 86
  • 137
0

If you wanna list a contract with all it's projects you'll have to do it the way you are already doing it. the result will look like this:

contract1 project1
contract1 project2

you can't use aggregates to get

contract1
           project1
           project2

that being said, your Ambiguous error is because you need to write

GROUP BY PA01201.PACONTNUMBER

and not

GROUP BY PACONTNUMBER

it still going to complain about all the other columns not being in an aggregate function(e.g. MAX, COUNT, AVG, MIN ....)

Bassam Mehanni
  • 14,796
  • 2
  • 33
  • 41
0
SELECT     min(PA01201.PACONTNUMBER) as minPAContNumber, 
           sum(cast(PA01201.PAPROJNUMBER as int)) as SUMPaProjNumber, 
               PA01201.PAprojname
FROM         PA01201 
INNER JOIN PA01100 
  ON PA01201.PACONTNUMBER = PA01100.PACONTNUMBER
GROUP BY PA01201.PAprojname

I'm assuming paprojnumber is a numeric field since your example is doing math on it.

you can't add0 PA01100.PACONTNUMBER back in to the select or group by as it will create the seperate rows you're trying to avoid; unless you use wm_Concat or a similar function to aggregrage all the different projects into the same line/column

UPDATE based on expected results and comments:

12004   25000  SS Bus Station
12005   xxxxx   xxxxxx

is not achievable because paprojnumber contains data such as 0612AB which can not be treated as a number thus when we try and roll up the data into one row adding the projnumbers together, the DBengine can't add 0612AB to the other results.

OR are these results OK?

12004  |8000, 6000, 1000, 10000  | SS BUS STATION
12005  |9000AB, 6000, 2000, 4000 | SS BUS STATION
xQbert
  • 34,733
  • 2
  • 41
  • 62
  • I just tried running that piece of code and got this error.... 'Operand data type char is invalid for sum operator'??? – user1086159 Dec 20 '11 at 12:22
  • That means my assumption was wrong. Paprojnumber is not numeric. you need to first cast it to number to do the math. What db engine are you using? this will tell me the syntax I need to update to cast paprojnumber to numeric. assumed mySQL – xQbert Dec 20 '11 at 12:25
  • syntax for sql 2005 is the same for mySQL in this [case](http://blog.sqlauthority.com/2007/07/07/sql-server-convert-text-to-numbers-integer-cast-and-convert/) I've updated my response to include the casting – xQbert Dec 20 '11 at 12:32
  • okay I just ran that peice of code and got the error.....'Conversion failed when converting the varchar value '06012AB ' to data type int??? Am I right in thinking this means '06012AB' is already an int field already? I am truely stumped by this as I have never seen this error before? – user1086159 Dec 20 '11 at 12:39
  • No, it means that your paprojNumber field isn't a NUMBER it has character data in it too. So when the system tried to change 06012AB from char to NUMBER it failed because AB is not a number. This means what your trying to do is not reasonable: for the contnumber that has 06012AB as a project number what would the single row results look like? you can't sum() AB into that row... in other words: eliminate character data from that field's data or you need to change your expected results and explain what you want to happen with character data. – xQbert Dec 20 '11 at 12:43
  • this is going to be an issue as most projects have the main number for example 06012 then subsequent projects of 06012AB, 06012D, etc i basically need to just drop the 'Lettering' or somehow subtract the figures so i can get a total under the main number of 06012? Sorry if this sounds confusing. I am not even sure what i am trying to do is possible. – user1086159 Dec 20 '11 at 12:48
  • In your example I assumed you were wanting to sum up the data in the projNumber field 6000+8000+1000+10000=25000. If this assumption is wrong I'm going down the wrong path. If however my assumption is correct, then what you are trying to do (sum) is not correct. However, SQL 2005 does have a similar function to WM_Concat whereby you could combine the rows into 1. See my response above for example: if that approach is acceptable, then there is a way to achieve it. I'm just not good at SQL server so I may point you to other solutions that explain how to do it. – xQbert Dec 20 '11 at 12:54
  • yes your assumption is coorect. My main goal is to take a total for all linked projects on one line. so in my original example i would get a 1 line total for 12004 of 25000, and then get a 1 line total for 12005 etc – user1086159 Dec 20 '11 at 13:00
  • In that case the alphabetic characters have to be eliminated somehow. bit beyond me; but the rest of the approach seems solid. – xQbert Dec 20 '11 at 13:48
  • yes those last results with the AB after the figure could potentially work. how did you get that to poull through like that? – user1086159 Dec 20 '11 at 14:08
  • http://stackoverflow.com/questions/180032/how-can-i-combine-multiple-rows-into-a-comma-delimited-list-in-sql-server-2005 (XML path is the most common solution) I'll never get the syntax correct w/o SQL Server, so I provided a link showing what to do, it's up to you to make it fit your situation (or someone else needs to help) – xQbert Dec 20 '11 at 14:52