我有三张桌子:
Emp
EmpId EmpName
51894 Alex
51899 Peter
51901 Sam
EmpPunch
PunchId EmpId BusinessDate PunchIn PunchOut
15896 51894 12/18/2017 7:30:00 AM 5:30:00 PM
15897 51894 12/19/2017 7:30:00 AM 5:30:00 PM
15898 51899 12/18/2017 7:30:00 AM 5:30:00 PM
15899 51901 12/18/2017 7:30:00 AM 5:30:00 PM
EmpMeals
MealID PunchID MealStart MealEnd
40725 15896 12/18/2017 9:00 12/18/2017 10:00
40726 15896 12/18/2017 11:30 12/18/2017 12:30
40727 15897 12/19/2017 9:00 12/19/2017 10:00
40728 15897 12/19/2017 11:30 12/19/2017 12:30
40729 15898 12/18/2017 9:00 12/18/2017 10:00
我需要编写一个查询来生成以下格式的报告:
EmpId EmpName BusinessDate PunchIn Meal 1 Start Meal 1 End Meal 1 Duration Meal 2 Start Meal 2 End Meal 2 Duration PunchOut
51894 Alex 12/18/2017 7:30:00 AM 9:00:00 AM 10:00:00 AM 1.00 11:30:00 AM 12:30:00 PM 1.00 5:30:00 PM
51894 Alex 12/19/2017 7:30:00 AM 9:00:00 AM 10:00:00 AM 1.00 11:30:00 AM 12:30:00 PM 1.00 5:30:00 PM
51899 Peter 12/18/2017 7:30:00 AM 9:00:00 AM 10:00:00 AM 1.00 5:30:00 PM
51901 Sam 12/18/2017 7:30:00 AM 5:30:00 PM
注意事项:
a. Employee can have any number of meals. But we are considering first two.
b. If employee has availed only one meal, then for 2nd meal start and end time, NULL will be displayed.
c. If employee has not availed any meals then his record will not appear in EmpMeals table.
现在,我写的查询是在多行中获取记录。我不是很精通SQL,所以寻求帮助。
请指导。
答案 0 :(得分:0)
这是一种使用临时表的方法。
be_parental()
答案 1 :(得分:0)
以下是我为此问题撰写的代码:
SELECT
E.EmpId EmpId,
E.EmpName,
EP.PunchId PunchId,
EP.BusinessDate BusinessDate,
FORMAT(EP.StartTime, 'hh:mm tt') InTime,
FORMAT(EP.EndTime, 'hh:mm tt') OutTime,
MAX(CASE WHEN EPMBTemp.DefNum = 1 THEN FORMAT(EPMB.StartTime, 'hh:mm tt') END) MealOut1,
MAX(CASE WHEN EPMBTemp.DefNum = 1 THEN FORMAT(EPMB.EndTime, 'hh:mm tt') END) MealIn1,
MAX(CASE WHEN EPMBTemp.DefNum = 1 THEN DATEDIFF(MINUTE,EPMB.StartTime,EPMB.EndTime)/60.0 END) Meal1Length,
MAX(CASE WHEN EPMBTemp.DefNum = 2 THEN FORMAT(EPMB.StartTime,'hh:mm tt') END) MealOut2,
MAX(CASE WHEN EPMBTemp.DefNum = 2 THEN FORMAT(EPMB.EndTime,'hh:mm tt') END) MealIn2,
MAX(CASE WHEN EPMBTemp.DefNum = 2 THEN DATEDIFF(MINUTE,EPMB.StartTime,EPMB.EndTime)/60.0 END) Meal2Length
FROM EmpPunch EP
LEFT JOIN
(
SELECT ROW_NUMBER () OVER (PARTITION BY PunchId ORDER BY PunchId ) AS DefNum, PunchId, MealPunchId
FROM
(
SELECT DISTINCT EPMB.PunchId, EPMB.MealPunchId
FROM EmpMealPunch EPMB
)Temp
)EPMBTemp ON EPMBTemp.PunchId = EP.PunchId
LEFT JOIN EmpMealPunch EPMB ON EPMB.MealPunchId = EPMBTemp.MealPunchId
JOIN Emp E ON EP.EmpId = E.EmpId
group by E.EmpId, E.EmpName, EP.BusinessDate, EP.PunchId, EP.StartTime, EP.EndTime