-1

I have 2 table like this:

Table_Work

Name      Date
===============
Andy      1 Jan
Andy      2 Jan
Andy      3 Jan
Ana       1 Jan
Ana       2 Jan
Ana       3 Jan
Ana       4 Jan

and Table_Salary

Name      Salary
=================
Andy       150
Ana        120

I want to use column Name and Salary from table_salary as a parameter. So, I can show a data like this:

Name     Salary_Got
===================
Andy        300
Ana         360

Table above show Salary_Got that calculate by (Salary x Number of Work). But I show only where work from 2 Jan. So, Andy only work for 2 days and Ana for 3 days.

Note: This is only illustrated problem. My problem more complex than this. The point is I cannot use JOIN because I need to calculate other row.

I hope anyone can help me to solve this problem. Thanks in advance.

Tommy Sayugo
  • 375
  • 2
  • 5
  • 16

3 Answers3

0

Try something like that;

select ts.Name, (ts.Salary * ISNULL(tw.workcound,0)) from Table_Salary ts left join
(select Name, count(*) workcount from Table_Work where Date != '2017-01-02' group by Name) 
tw ON ts.Name = tw.Name
lucky
  • 12,734
  • 4
  • 24
  • 46
0

Try this:

select TW.Name, TW.DaysWorked * TS.Salary from (
    select [Name], count(*) [DaysWorked] from Table_Work TW
    where [Date] <> '1 Jan' --if you store date as text...
    -- where [Date] > '2018-01-01' --if you store Date as date
    group by [Name]
) TW join Table_Salary TS on TW.Name = TS.Name
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
0

TRY THIS: You can use outer apply to find out the count of days for each employees then multiply it will salary of the respected employee

SELECT ts.Name, (ts.Salary*ISNULL(t.tot, 1)) Salary_Got 
FROM Table_Salary ts
OUTER APPLY(SELECT COUNT(*) tot 
            FROM Table_Work tw 
            WHERE tw.name = ts.name AND [Date] >= '2018-01-02') t
Shushil Bohara
  • 5,556
  • 2
  • 15
  • 32