好的,我有一张包含以下信息的表
Company, Employee, Work Date, Job Number
ABC 1234 06/01/15 5555
ABC 1234 06/02/15 5555
ABC 1234 06/03/15 5555
ABC 1234 06/04/15 5555
ABC 1234 06/05/15 5555
ABC 1234 06/06/15 5555
ABC 1234 06/11/15 6666
ABC 1234 06/12/15 6666
ABC 1234 06/13/15 6666
ABC 1234 06/14/15 6666
ABC 1234 06/15/15 6666
ABC 1234 06/16/15 6666
我想要的记录是
ABC 1234 06/11/15 6666
我的第一步是使用此查询获取最近的工作日期
SELECT TCH.Company_Code, TCH.Employee_Code, CAST(TCH.Work_Date AS DATE) AS Work_Date, DENSE_RANK() OVER (PARTITION BY TCH.Employee_Code ORDER BY CAST(TCH.Work_Date AS DATE) DESC) AS DateRank
FROM PR_TIME_CARD_HISTORY_MC TCH WITH (NOLOCK)
WHERE TCH.Company_Code <> 'XYZ'
AND LTRIM(RTRIM(TCH.Employee_Code)) = '22164'
第二步是将它连接回原始表以获得作业,但我似乎无法获得作业的记录集来获得最小日期。
SELECT TCH.Company_Code, TCH.Employee_Code, TCH.Job_Number, CAST(TCH.Work_Date AS DATE) AS Work_Date, DENSE_RANK() OVER (PARTITION BY TCH.Job_Number ORDER BY CAST(TCH.Work_Date AS DATE) ASC) AS JobRank
FROM PR_TIME_CARD_HISTORY_MC TCH WITH (NOLOCK)
WHERE TCH.Company_Code <> 'XYZ'
AND TCH.Pay_Type = 'R'
AND LTRIM(RTRIM(TCH.Employee_Code)) = '22164'
希望这有所帮助,肯定可以使用一些指导。
答案 0 :(得分:2)
我认为以下查询应该完成这项工作:
SELECT company, employee, work_date,job_number FROM
(
SELECT
TCH.Company_Code as company,
TCH.Employee_Code as employee,
TCH.Job_Number as job_number,
CAST(TCH.Work_Date AS DATE) AS Work_Date,
ROW_NUMBER() OVER (PARTITION BY TCH.Employee_Code,TCH.Company_Code ORDER BY TCH.Job_Number DESC,CAST(TCH.Work_Date AS DATE) ASC) AS DateRank
FROM PR_TIME_CARD_HISTORY_MC TCH WITH (NOLOCK)
) q
WHERE q.DateRank=1
答案 1 :(得分:0)
编辑:
要获得最高Job_Number
的最低日期,您需要同时应用ORDER BY
:
SELECT *
FROM
(
SELECT TCH.Company_Code, TCH.Employee_Code, TCH.Job_Number,
CAST(TCH.Work_Date AS DATE) AS Work_Date,
ROW_NUMBER() -- latest row gets 1
OVER (PARTITION BY TCH.Employee_Code
ORDER BY TCH.Job_Number DESC, CAST(TCH.Work_Date AS DATE)) AS DateRank
FROM PR_TIME_CARD_HISTORY_MC TCH WITH (NOLOCK)
WHERE TCH.Company_Code <> 'XYZ'
AND LTRIM(RTRIM(TCH.Employee_Code)) = '1234'
) AS dt
WHERE DateRank = 1
但如果工作数量没有增加,你需要一个不同的逻辑:
SELECT *
FROM
(
SELECT TCH.Company_Code, TCH.Employee_Code, TCH.Job_Number,
CAST(TCH.Work_Date AS DATE) AS Work_Date,
ROW_NUMBER() -- latest row gets 1
OVER (PARTITION BY TCH.Employee_Code
ORDER BY CAST(TCH.Work_Date AS DATE)) AS DateRank,
-- The start date of each job
MIN(CAST(TCH.Work_Date AS DATE))
OVER (PARTITION BY TCH.Employee_Code, TCH.Job_Number) AS FirstWorkDate
FROM PR_TIME_CARD_HISTORY_MC TCH WITH (NOLOCK)
WHERE TCH.Company_Code <> 'XYZ'
AND LTRIM(RTRIM(TCH.Employee_Code)) = '1234'
) AS dt
WHERE DateRank = 1
答案 2 :(得分:0)
使用where子句中的嵌套查询尝试此操作:
MS SQL Server 2008架构设置:
messages = ["\n".join(m.decode() for m in mssg[1]) for mssg in messages]
查询1 :
CREATE TABLE TimeCard
(
Company VARCHAR(10),
Employee INT,
WorkDate Date,
JobNumber Int
)
INSERT INTO TimeCard
VALUES
('ABC', 1234, '06/01/15', 5555),
('ABC', 1234, '06/02/15', 5555),
('ABC', 1234, '06/03/15', 5555),
('ABC', 1234, '06/04/15', 5555),
('ABC', 1234, '06/05/15', 5555),
('ABC', 1234, '06/06/15', 5555),
('ABC', 1234, '06/11/15', 6666),
('ABC', 1234, '06/12/15', 6666),
('ABC', 1234, '06/13/15', 6666),
('ABC', 1234, '06/14/15', 6666),
('ABC', 1234, '06/15/15', 6666),
('ABC', 1234, '06/16/15', 6666)
<强> Results 强>:
SELECT TC.Company, TC.Employee, MIN(TC.WorkDate) AS WorkDate, TC.JobNumber
FROM TimeCard TC
WHERE TC.Company <> 'XYZ' AND
TC.Employee = 1234 AND
TC.JobNumber =
(
-- Get Max JobNumber for the Max Workdate
SELECT MAX(JobNumber)
FROM TimeCard TC3
WHERE TC3.WorkDate =
-- Get Max WorkDate of Employee
(
SELECT MAX(WorkDate)
FROM TimeCard TC2
WHERE TC2.Employee = TC.Employee
)
GROUP BY TC3.Employee
)
GROUP BY TC.Company, TC.Employee, TC.JobNumber