Author |
Topic |
forwheeler
Starting Member
44 Posts |
Posted - 2007-05-21 : 11:58:57
|
Here is what I get from my query:last, first, desc, code, amountmouse mickey SALARY 4000 5000.00mouse mickey GROUP INSURANCE - LIFE 4201 42.16mouse mickey GROUP INSURANCE - HLTH & ACCID 4202 2000.25mouse mickey WORKER'S COMPENSATION 4203 104.19Every code that starts with 42 I want to total. The other codes I just want to display. Here is what I want:mouse mickey 4000 5000.00mouse mickey 4200 2146.60 Here is my query:SELECT d.last_name,d.first_name, b.object_desc, code,SUM(a.amount)FROM payroll a, bud_object b, employee_detail c, employee_master d, transaction_master eWHERE a.pca = @pcaAND e.Effective_date = '2007'AND a.expense_so= b.object_codeAND c.ssn=d.ssnAND a.ssn=c.ssnAND a.pcn=c.pcnAND a.payroll_no=c.payroll_noAND a.transaction_id=e.transaction_idAND a.pay_date=c.pay_dateAND substring(code,1,2)='42'GROUP BY d.last_name,d.first_name,b.object_desc,code |
|
Kristen
Test
22859 Posts |
Posted - 2007-05-21 : 12:01:19
|
SUM(CASE WHEN code LIKE '42%' THEN amount ELSE 0 END) AS MyTotal |
 |
|
forwheeler
Starting Member
44 Posts |
Posted - 2007-05-21 : 14:16:25
|
Thanks for your response Kristen.I get the same result. The only difference is that the amount is 0 if the code is not a '42' code.Any other ideas? |
 |
|
Kristen
Test
22859 Posts |
Posted - 2007-05-21 : 14:48:51
|
"I get the same result"Is it the WRONG result?You had AND substring(code,1,2)='42'in the original query, which is presumably ONLY including the relevant values for CODE beginning with "42". If I've understood you correctly you will need to remove that once you put my suggested SUM() in place.Kristen |
 |
|
forwheeler
Starting Member
44 Posts |
Posted - 2007-05-21 : 15:05:45
|
I get:last, first, desc, code, amountmouse mickey SALARY 4000 0mouse mickey GROUP INSURANCE - LIFE 4201 42.16mouse mickey GROUP INSURANCE - HLTH & ACCID 4202 2000.25mouse mickey WORKER'S COMPENSATION 4203 104.19Which is what I had before. I want the sum of all the 42 codes rolled up to one row as in my example.mouse mickey 4000 5000.00mouse mickey 4200 2146.60 |
 |
|
Kristen
Test
22859 Posts |
Posted - 2007-05-21 : 15:11:52
|
Sounds like you want the answer to a different question to me!Is this what you want?1) Take the first two digits of the code; Append "00" i.e. 4000-> "40"+"00", 4001-> "40"+"00", 4201-> "42"+"00" ...Show the total where CODE starts with those 2 digits.??Kristen |
 |
|
forwheeler
Starting Member
44 Posts |
Posted - 2007-05-21 : 18:36:44
|
No, I do need the code to display 4200 but the real issue is that I want the all the codes that have the first two digits of 42 to have their amounts summed all together on one row.mouse mickey 4200 2146.60Thanks |
 |
|
Kristen
Test
22859 Posts |
Posted - 2007-05-22 : 02:05:12
|
"I do need the code to display 4200"Sorry, I must be missing something. There isn't a data value with Code = '4200' in your data.There are, however, three rows with codes '4201', '4202' and '4203' and their amounts total 2146.60.That suggests to me that you want to aggregate on the first two digits and then append "00" to them. (Or if Code is numeric you could Round it to the nearest Hundred).[Which was the process I thought I described!]Kristen |
 |
|
forwheeler
Starting Member
44 Posts |
Posted - 2007-05-22 : 08:49:25
|
I can handle the renaming of the code. I need help with the summing of the amount field such as select SUM(amount)from tables where substring(code,1,2)='42' I want this to diplay in one row-the sum for all amounts for 42 codes.Thanks |
 |
|
Kristen
Test
22859 Posts |
Posted - 2007-05-22 : 12:08:47
|
select SUM(amount)from tables where substring(code,1,2)='42' is going to give you 2146.60, isn't it? - if that isn't what you want can you give me an example of what you want the output to look like.(Sorry, I still don't get it!)Kristen |
 |
|
|