Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 获取其工资在发布日期之后的员工的详细信息_Sql_Oracle - Fatal编程技术网

Sql 获取其工资在发布日期之后的员工的详细信息

Sql 获取其工资在发布日期之后的员工的详细信息,sql,oracle,Sql,Oracle,我有两张表——员工表和工资表 Employee有3列: EmpId、JoiningDate和LastDate 薪资表有3列: EmpId,SalaryPerMonth,SalaryDate 我需要得到那些工资在最后一天之后的员工的empId和SalaryPerMonth 我试过- select emp.id,s.salary from empG emp, salG s where emp.END_DATE < ( select max(s.sal_date) from salG s wh

我有两张表——员工表和工资表

Employee有3列: EmpId、JoiningDate和LastDate

薪资表有3列: EmpId,SalaryPerMonth,SalaryDate

我需要得到那些工资在最后一天之后的员工的empId和SalaryPerMonth

我试过-

select emp.id,s.salary 
from empG emp,
salG s
where emp.END_DATE < ( select max(s.sal_date) from salG s where emp.id= s.id); 

但这是笛卡尔积。有更好的方法吗?

在外部查询中根本不需要两个表:

select s.id, s.salary 
from salG s
where s.sal_date > (select e.END_DATE from empg e where e.id = s.id); 

这似乎是对您要求的更直接的翻译。

您没有提供示例预期输出,但据我所知,这应该可以满足您的需要。不需要子查询或其他任何查询,您可以在连接中执行逻辑

示例表“雇员”

IF OBJECT_ID('tempdb..#Employee') IS NOT NULL DROP TABLE #Employee
CREATE TABLE #Employee (EmpID int, JoiningDate date, LastDate date)
INSERT INTO #Employee
VALUES
(1,'2016-01-01','2016-01-31')
,(2, '2016-01-10','2016-03-31')
,(3, '2016-01-11','2016-03-31')
示例表“工资”

IF OBJECT_ID('tempdb..#Salary') IS NOT NULL DROP TABLE #Salary
CREATE TABLE #Salary (EmpId int, SalaryPerMonth int, SalaryDate date)
INSERT INTO #Salary
VALUES
 (1, 100, '2016-02-02')
,(2, 200, '2016-01-31')
,(2, 200, '2016-02-28')
,(2, 200, '2016-04-01')
,(3, 300, '2016-01-30')
,(3, 300, '2016-02-23')
,(3, 300, '2016-03-29')
质疑


@史丹:它;It’你好像是个读心术的人。我只是在谷歌上搜索如何格式化。请给我提供链接。非常感谢。给你:谢谢你,先生。不,我不是故意的。在我的下一个问题中,您没有机会这样说:顺便说一句,您添加了表的示例数据,这非常好。但别忘了包括你期望的结果应该是什么样子。那是什么语言?它看起来不像Oracle SQL;在Oracle SQL中,表名可能不包括。是的,这是SQL server,但这些表名只是临时表,所以我不必在测试系统中创建实际的表。
IF OBJECT_ID('tempdb..#Salary') IS NOT NULL DROP TABLE #Salary
CREATE TABLE #Salary (EmpId int, SalaryPerMonth int, SalaryDate date)
INSERT INTO #Salary
VALUES
 (1, 100, '2016-02-02')
,(2, 200, '2016-01-31')
,(2, 200, '2016-02-28')
,(2, 200, '2016-04-01')
,(3, 300, '2016-01-30')
,(3, 300, '2016-02-23')
,(3, 300, '2016-03-29')
SELECT
    e.EmpID
    ,e.JoiningDate
    ,e.LastDate
    ,s.SalaryDate
    ,s.SalaryPerMonth
FROM #Employee e
INNER JOIN #Salary s
    ON e.EmpID = s.EmpId
    AND e.LastDate < s.SalaryDate
EmpID   JoiningDate LastDate    SalaryDate  SalaryPerMonth
1       2016-01-01  2016-01-31  2016-02-02  100
2       2016-01-10  2016-03-31  2016-04-01  200