A Complex Select Statement [message #116147] |
Mon, 18 April 2005 03:02 |
weekend79
Messages: 198 Registered: April 2005 Location: Islamabad
|
Senior Member |
|
|
Summery Of Problem
[
Master table have date column.
Child table don’t have date column.
Need a Select statement which return sum of a Child column for a specific month of year.
]
Details Of Problem (If you have time for detail Answer)
[
Master Table
Sale_Sheet_No (PK)
Date
SalesMan_Empno(FK)
Child Table
Sno (PK)
Sale_Sheet_No (FK)
Item_No (FK form Item_Details)
Qty_Sold
A third table Item_Details have column of commission.
I want to calculate commission of salesman for every month and want to add it in table of Monthly_Salary which as follows.
Monthly_Salary
Sno (PK)
Empno (FK from Employee)
Salary
Commission
loan
TotalSalary
Wishes
Jawad
|
|
|
Re: A Complex Select Statement [message #116270 is a reply to message #116147] |
Tue, 19 April 2005 00:30 |
|
djmartin
Messages: 10181 Registered: March 2005 Location: Surges Bay TAS Australia
|
Senior Member Account Moderator |
|
|
Sno from Child is NOT Sno from Monthly_Salary. You need a month (date) on Monthly_Salary or some other way of associating a month/year with a particular entry.
Insert the normal monthly record with Salary and Loan[Repayment] (as a positive number)
update Monthly_Salary ms
set Commission = (select sum (nvl (itm.Commission, 0) * ch.Qty_Sold)
from Item_Details itm, Child ch, Master mst
where 1 = 1
and itm.Item_No = ch.Item_No
and ch.Sale_Sheet_No = ms.Sale_Sheet_No
and ms.Empno = mst.SalesMan_Empno
and to_char (mst.Date, 'YYYYMM') = '200503'),
TotalSalary = Salary
+ (select sum (nvl (itm.Commission, 0) * ch.Qty_Sold)
from Item_Details itm, Child ch, Master mst
where 1 = 1
and itm.Item_No = ch.Item_No
and ch.Sale_Sheet_No = ms.Sale_Sheet_No
and ms.Empno = mst.SalesMan_Empno
and to_char (mst.Date, 'YYYYMM') = '200503')
- loan
where to_char (ms.Date, 'YYYYMM') = '200503'; -- you have to be able to select which Monthly_Salary entries are to be updated.
Give it a go and tell us the result.
|
|
|
|
|
|
|
|
|