Tags
Advanced SQL Interview Questions and Answers, Advanced SQL tutorial pdf, any recommended websites for sql puzzles, Best SQL Puzzles, Buy SQL Server Interview Questions Book Online at Low Price, Calculating nth Max salary, Check Prime Number puzzle, Circular prime number puzzle, Circular Prime Number Puzzlre, COmples tSQL puzzles, Complex SQL Challenges, complex sql statement(puzzle), Complex tsql, Complex TSQL Challenge, convert string to proper case in sql server, Divide rows into two columns, Download SQL Questions, Download SQL Server Interview Question in PDF, Download SQL SERVER Interview questions, Download SQL Server Interview questions and answers, download sql server interview questions and answers pdf, download sql server interview questions by Pawan Khowal, download sql server interview questions by Pawan Kumar, download sql server interview questions by Pawan Kumar Khowal, Download T-SQL Interview Questions, Free Download SQL SERVER Interview questions, Improve SQL Skills, Interview Puzzles in SQL Server, Interview Qs.SQL SERVER Questions, Interview questions and Answers for MS SQL Server designing, Interview Questions and Answers For SQL, Interview questions on Joins, Interview Questions on SQL, Interview SQL Puzzles, InterviewQuestions, InterviewQuestions for SQL, Learn complex SQL, Learn SQL, Learn T-SQL, Microsoft SQL Server interview questions for DBA, MS SQL Server interview questions, MSBI Interview Questions, Objective Puzzle, Pawan, Pawan Khowal, Pawan Kumar, Pawan Kumar Khowal, PL/SQL Challenges, Puzzle SQL, puzzle sql developer, Puzzle SQl Server, Puzzles, PUzzles in SQL SERVER, Queries for SQL Interview, SQL, SQL - The Pattern Puzzles, SQL 2012, SQL 2014, SQL 2014 Interview Questions, SQL Challenge, SQL Challenges, SQL Extreme Difficult Puzzle, SQL FAQ, SQL FAQs, sql group by only rows which are in sequence, SQL Interview Q & A, SQL Interview Questions, SQL Interview Questions - Part 2, SQL Interview Questions for SQL Professionals, SQL Joins, SQL Pattern Puzzles, SQL pl/sql puzzles, SQL prime number puzzle, SQL puzzle, SQL Puzzles, sql puzzles & answers, sql puzzles and answers free download, sql puzzles and answers pdf, sql puzzles for interview, sql puzzles oracle, SQL Queries, SQL Queries asked in interviews, SQL QUERY PUZZLES, SQL Query to Find Nth Highest Salary of Employee, SQL Questions, SQL Quiz, SQL Replace Puzzle, SQL Server - Common Interview Questions and Answers, SQL SERVER - Find Nth Highest Salary of Employee, SQL Server - General Interview Questions and Answers, sql server 2008 interview questions, SQL Server 2008 Interview Questions and Answers, SQL Server Database, SQL Server database developer interview questions and answers, sql server dba interview questions, SQL Server developer Interview questions and answers, SQL Server developer Interview questions with answers, SQL Server Developer T-SQL Interview Questions, SQL server filter index with LIKE and RIGHT function?, SQL Server Interview Puzzle, SQL SERVER Interview questions, SQL SERVER Interview questions & Answers, SQL Server Interview Questions - Part 1, SQL Server Interview questions and answers, SQL Server Interview Questions and Answers - Free PDF, SQL SERVER Interview questions and answers for experienced, sql server interview questions and answers for net developers, SQL Server Interview Questions And Answers.pdf, sql server interview questions by Pawan Khowal sql interview questions, SQL SERVER Interview questions pdf, SQL Server Interview Questions | MSBISKILLS.Com, SQL Server Interview Questions | MSBISKILLS.com Top 50 SQL Server Questions & Answers, SQL Server Interview Questions/Answers Part-1, SQL Server Puzzle, SQL SERVER Puzzles, SQL server Questions and Answers, SQL SERVER Tips, SQL Skills, SQL Sudoku, SQL Tips & Tricks, SQL Tips and Tricks, SQL Top clause, SQL Tough Puzzle, SQL Tricks, SQL Trikcy question, sql/database interview for puzzle sql developer, SQLBI Interview Questions, SQLSERVER, Star Pattern Puzzle, Sum of digits puzzle, Sum of digits SQL puzzle, SUM of grouped COUNT in SQL Query, T SQL Puzzles, T-SQL Challenge, T-SQL Interview Questions | SQL Server Interviews and Jobs, T-SQL Puzzle, T-SQL Query | [ Replace 6 Consecutive Digits with x from a string Puzzle ], T-SQL Query | [ The Complex Week Puzzle ], T-SQL Server Interview Questions, T-SQL Tricky Puzzles, The Status Puzzle, Top 10 Frequently Asked SQL Query Interview Questions, TOP 100 SQL SERVER INTERVIEW QUESTIONS QUERIES, Top 50 SQL Server Interview Question for Testers, Tough SQL Challenges, Tough SQL Puzzles, Tricky Questions, TSQL, TSQL Challenge, TSQL Challenges, TSQL Interview questions, TSQL Puzzle, TSQL Puzzles, TSQL Queries
SQL Puzzle | Working Hours Puzzle
Write a T-SQL that gives us hours worked from 2016-09-01 07:00:00 to 2016-09-02 06:59:99.
by EmployeeType
For more please check out the sample input and the expected output.
Sample Inputs
EmployeeName | EmployeeType | StartDateTime | EndDateTime |
---|---|---|---|
Pawan K | N | 2016-09-01 06:00:00.000 | 2016-09-01 14:00:00.000 |
Ramesh K | N | 2016-09-01 13:00:00.000 | 2016-09-02 09:00:00.000 |
Sharlee D | D | 2016-09-01 09:00:00.000 | 2016-09-01 13:00:00.000 |
Mizan D | D | 2016-09-01 20:00:00.000 | 2016-09-02 08:00:00.000 |
Expected Output
EmployeeType | WorkDate | Hours |
---|---|---|
N | 2016-08-31 | 1 |
N | 2016-09-01 | 25 |
N | 2016-09-02 | 2 |
D | 2016-09-01 | 15 |
D | 2016-09-02 | 1 |
Rules/Restrictions
- The challenge is to do it with T-SQL statements only.
- Add your solution(s) in the comments section or send you solution(s) to pawankkmr@gmail.com
Script | use below script to create table and insert some sample data
-- CREATE TABLE Employees1 ( EmployeeName VARCHAR(100) ,EmployeeType VARCHAR(100) ,StartDateTime DATETIME ,EndDateTime DATETIME ) GO INSERT INTO Employees1 VALUES ('Pawan K' , 'N' ,'2016-09-01 06:00:00' , '2016-09-01 14:00:00' ), ('Ramesh K' , 'N' , '2016-09-01 13:00:00' , '2016-09-02 09:00:00' ), ('Sharlee D' , 'D' , '2016-09-01 09:00:00' ,'2016-09-01 13:00:00' ), ('Mizan D' , 'D' , '2016-09-01 20:00:00' , '2016-09-02 08:00:00' ) GO SELECT * FROM Employees1 GO --
—
SOLUTION – 1
-- DECLARE @StartDate AS DATETIME = '2016-01-01' ;WITH SingleDigits(Number) AS ( SELECT Number FROM (VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (0)) AS X(Number) ) ,Series AS ( SELECT (d1.Number+1) + (10*d2.Number) + (100*d3.Number) + (1000*d4.Number) Number from SingleDigits as d1, SingleDigits as d2, SingleDigits as d3, SingleDigits as d4 ) ,CTE0 AS ( SELECT *, DATEADD(d,Number,@StartDate) st FROM Series ) ,CTE1 AS ( SELECT * , CASE WHEN StartDateTime > st then CAST(st AS DATE) else CAST(DATEADD(d,-1,CAST(StartDateTime AS DATE)) AS DATE) END starts , CASE WHEN EndDateTime > et then CAST(et AS DATE) else CAST(DATEADD(d,-1,CAST(EndDateTime AS DATE)) AS DATE) END Ends FROM ( SELECT EmployeeType,StartDateTime , EndDateTime , DATEADD(day, DATEDIFF(day, 0, CAST(StartDateTime AS DATE)), '07:00:00') st , DATEADD(day, DATEDIFF(day, 0, CAST(EndDateTime AS DATE)), '07:00:00') et FROM Employees1 )k ) SELECT EmployeeType, CAST(b.st AS DATE) WorkDate ,SUM(CASE WHEN starts = b.st THEN CASE WHEN a.st > StartDateTime THEN DATEDIFF(hh,StartDateTime,a.st) ELSE CASE WHEN starts <> Ends THEN DATEDIFF(hh,StartDateTime,CASE WHEN a.et < EndDateTime THEN a.et ELSE EndDateTime END) ELSE DATEDIFF(hh,StartDateTime,EndDateTime) END END ELSE CASE WHEN a.et < EndDateTime THEN DATEDIFF(hh,a.et,EndDateTime) ELSE DATEDIFF(hh,a.StartDateTime,a.et)END END) Hours FROM CTE1 a INNER JOIN CTE0 b ON b.st BETWEEN a.starts AND a.Ends GROUP BY EmployeeType,Number,CAST(b.st AS DATE) ORDER BY EmployeeType DESC,Number -- |
Add a comment if you have a solution in mind. I would love to learn it. We all need to learn.
Enjoy !!! Keep Learning
Pawan Khowal
Http://MSBISkills.com