| Author |
Topic |
|
hueby
Posting Yak Master
127 Posts |
Posted - 2006-04-14 : 11:26:18
|
hi all,I am using this code below to select the MAX requisition number for the equipment_code.select max(tool_requisition_number), equipment_code, job_numberfrom et_req_history_sum_job_mcwhere company_code = 'hwp'group by equipment_code, job_numberorder by equipment_code This works, but it since I have to group by Job_number because I have it in the select statement it returns TWO values for the equipment_code (with different Job_numbers). So... for example:req. # Equip_code job_number10 e1 job18 e2 job4I only want one to return. The req. # 10 because it's higher. If I removed job_number from the select statement and group by then it works perfect. Though I need to display the job_number too.When just leaving out job_number from group by I get the "Column 'et_req_history_sum_job_mc.Job_Number' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause." error. I do not fully understand this. |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2006-04-14 : 11:43:44
|
| [code]select max(tool_requisition_number), equipment_code, max(job_number)from et_req_history_sum_job_mcwhere company_code = 'hwp'group by equipment_codeorder by equipment_code[/code] |
 |
|
|
jubinjose
Starting Member
20 Posts |
Posted - 2006-04-14 : 11:51:34
|
| 10 e1 job18 e2 job4Both should return right?coz job code and equipment code are different btb does table have a primary key? |
 |
|
|
hueby
Posting Yak Master
127 Posts |
Posted - 2006-04-14 : 11:54:03
|
| Hi, that would work, expect my job_numbers do NOT increase with the requisition numbers.So technically my data could look like:req#, equip. code, job #10, e1, dc11118, e2, sc6000So, then the code would pull 10, e1, sc6000 |
 |
|
|
jubinjose
Starting Member
20 Posts |
Posted - 2006-04-14 : 11:54:12
|
| I think the sample data u gave is wrong. Anywaysselect tool_requisition_number, equipment_code, job_numberfrom et_req_history_sum_job_mc Awhere not exists(select 1 from et_req_history_sum_job_mc Bwhere B.tool_requisition_number>A.tool_requisition_numberand A.equipment_code = B.equipmentcode) |
 |
|
|
hueby
Posting Yak Master
127 Posts |
Posted - 2006-04-14 : 11:57:27
|
quote: Originally posted by jubinjose 10 e1 job18 e2 job4Both should return right?coz job code and equipment code are different btb does table have a primary key?
Hi, I actually only want the MAX requisition number to return. 10 e1 job1. I don't care about the lower ones. (This is a history table, that I didn't design, but i'm forced to work with)Now that I look at my data example, the equipment numbers should be the same. Sorry about that... like:10 e1 job18 e1 job4I want the highest requisition number for that equipment code, and then the job_number associated with that row.The job number and requistion number are primary keys. |
 |
|
|
hueby
Posting Yak Master
127 Posts |
Posted - 2006-04-14 : 11:58:22
|
quote: Originally posted by jubinjose I think the sample data u gave is wrong. Anywaysselect tool_requisition_number, equipment_code, job_numberfrom et_req_history_sum_job_mc Awhere not exists(select 1 from et_req_history_sum_job_mc Bwhere B.tool_requisition_number>A.tool_requisition_numberand A.equipment_code = B.equipmentcode)
I did... sorry about that. It should of been something like:10 e1 job18 e1 job4 |
 |
|
|
hueby
Posting Yak Master
127 Posts |
Posted - 2006-04-14 : 12:08:20
|
| [code]select tool_requisition_number, equipment_code, job_numberfrom et_req_history_sum_job_mc Awhere not exists(select 1 from et_req_history_sum_job_mc Bwhere B.tool_requisition_number>A.tool_requisition_numberand A.equipment_code = B.equipmentcode)[/code]That gets the job done. THANK YOU. I learned something new... |
 |
|
|
hueby
Posting Yak Master
127 Posts |
Posted - 2006-04-25 : 15:14:02
|
Thanks for everyone's help...I've changed the code from > to <, this gives me the correct information now needed.select tool_requisition_number, equipment_code, job_numberfrom et_req_history_sum_job_mc Awhere not exists(select 1 from et_req_history_sum_job_mc Bwhere B.tool_requisition_number<A.tool_requisition_numberand A.equipment_code = B.equipment_code) Only thing is, when a job_number has a single "tool_requisition_number" row, then it does NOT display anything. I'm assuming this is because of the "where B.tool_requisition_number<A.tool_requisition_number" compare? |
 |
|
|
hueby
Posting Yak Master
127 Posts |
Posted - 2006-04-25 : 15:20:24
|
| One second thought, let me explain this:I went from > to <, because that acutally gave me the higher value. It must be the way the data is formatted in the cell?? It's a VARCHAR(7) and the number will display first then fill up in spaces until 7 characters are maxed. So: '72 ' and '283 'With > it took '72 ' instead of '283 'Is this maybe the problem to address? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-04-25 : 15:26:26
|
To get the job_number and don't include it in the group by, you can use a derived table:select t.tool_requisition_number, t.equipment_code, e.job_numberfrom ( select max(tool_requisition_number) as tool_requisition_number, equipment_code from et_req_history_sum_job_mc where company_code = 'hwp' group by equipment_code) tINNER JOIN et_req_history_sum_job_mc eON e.tool_requisition_number = t.tool_requisition_number AND e.equipment_code = t.equipment_code Tara Kizeraka tduggan |
 |
|
|
jubinjose
Starting Member
20 Posts |
Posted - 2006-04-25 : 16:06:17
|
quote: Originally posted by hueby One second thought, let me explain this:I went from > to <, because that acutally gave me the higher value. It must be the way the data is formatted in the cell?? It's a VARCHAR(7) and the number will display first then fill up in spaces until 7 characters are maxed. So: '72 ' and '283 'With > it took '72 ' instead of '283 'Is this maybe the problem to address?
If you want to comapre the varchar fields as int, then use cast. So your condition would look likecast(A.jobno as int)>cast(B.jobno as int) |
 |
|
|
|