| Author |
Topic |
|
jazzyb
Starting Member
20 Posts |
Posted - 2011-04-20 : 08:27:29
|
| Dear Friends,I am new to sql,I have 2 tables named as payroll and employees.My payroll table has many columns as per my requirement. But I am giving here only few columns of my table.Few columns of payroll table has data in following format:empid, month, year, basicpay, allowance1, billtype1, January, 2011, 5000, 1000, new2, January, 2011, 4000, 900, new3, January, 2011, 3000, 800, new1, February, 2011, 5000, 1000, new2, February, 2011, 4000, 900, new3, February, 2011, 3000, 800, new1, March, 2011, 5500, 1200, new2, March, 2011, 4500, 920, new3, March, 2011, 3000, 800, new..... I need a select statement which results the output in following format:empid, firstname, BASICPAY, allowance1, monthcounter(This monthcounter multiplication factor)1, Sam, 5000, 1000, 21, Sam, 5500, 1200, 12, Robert, 4000, 900, 22, Robert, 4500, 920, 13, Smartman, 3000, 800, 3I used the following query(This is part of my query), It is resulting all rows for the given conditions, but I want distinct rows and monthcounter as the multiplication factor for each distinct row.SELECT payroll.empid,SUM(payroll.allowance1) AS allowance1,payroll.month AS month,SUM(CASE WHEN payroll.Year=2011 AND billtype='new' AND payroll.allowance1 > 0 THEN payroll.[basicpay] ELSE 0 END) AS BASICPAY,employees.firstnameFROM payroll INNER JOIN employees ON payroll.empid = employees.empidWHERE (payroll.year=2011) GROUP BY month, payroll.empid, employees.firstnameORDER BY payroll.empid,payroll.monthSo I need your kind help,Thanks |
|
|
lionofdezert
Aged Yak Warrior
885 Posts |
|
|
jazzyb
Starting Member
20 Posts |
Posted - 2011-04-22 : 10:16:16
|
| Dear Friend,Thanks for your suggestion, I used Over (Partition by) and it helped me to get the desierd results.You are great person......bye |
 |
|
|
|
|
|