• Home
  • SQL Server
    • Articles
    • T-SQL Puzzles
    • Output Puzzles
    • Interview Questions
    • Performance Tuning
    • SQL SERVER On Linux
    • Resources
  • SSRS
    • SSRS Articles
    • Interview Questions
  • SSAS
    • SSAS Articles
    • DAX
  • SQL Puzzles
  • Interview Questions
    • SQL Interview Questions
    • Data Interview Questions
  • Python Interview Puzzles
  • New Features(SQL SERVER)
    • SQL SERVER 2017
    • SQL SERVER 2016
    • SQL SERVER On Linux
  • Social
    • Expert Exchange
      • Top Expert in SQL
      • Yearly Award
      • Certifications
      • Achievement List
      • Top Expert of the Week
    • HackerRank (SQL)
    • StackOverflow
    • About Me
      • Contact Me
      • Blog Rules

Improving my SQL BI Skills

Improving my SQL BI Skills

Tag Archives: The Multiple Sums Puzzle

SQL Puzzle | The Phone Call Puzzle

10 Monday Oct 2016

Posted by Pawan Kumar Khowal in SQL Puzzles, SQL SERVER, SQL SERVER Puzzles, T SQL Puzzles, Tricky SQL Queries

≈ 13 Comments

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, Even Odd Count Puzzle, Excellent t-SQL puzzles, Find nth highest salary, Find Nth maximum value in SQL Server, find records not ending with s, find records not ending with s puzzle, Finding prime numbers with SQL server, First letter upper case for each word in sql server, Free Download SQL SERVER Interview questions, Generate possible combinations puzzle, Get nth highest and lowest salary of an employee, Getting group by sum and total sum in a single query, Group by numbers that are in sequence, group sum total in one query, implementation and administration, Improve SQL Skills, Interesting Interview Questions, 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, Joins, Joins Interview questions, Joins Puzzle, Khowal, last non null puzzle, Learn complex SQL, Learn SQL, Learn T-SQL, Microsoft SQL Server interview questions for DBA, MS SQL Server interview questions, MSBI Interview Questions, Multiple Column Pivoting Puzzle, Multiplication of Digits Puzzle, Multiply digits of a number, multiply digits of a number puzzle, nth maximum & minimum salary in a single sql query, Objective Puzzle, Pawan, Pawan Khowal, Pawan Kumar, Pawan Kumar Khowal, PL/SQL Challenges, Prev Value puzzle, Previous value puzzle, Prime numbers after given number puzzle, Prime numbers using SQL, Program to Print prime numbers, Puzzle SQL, puzzle sql developer, Puzzle SQl Server, Puzzles, PUzzles in SQL SERVER, Queries for SQL Interview, Records not ending with a character Puzzle, Records not ending with s Puzzle, Replace string puzzle, SELECT Puzzle, Single Query multiple conditions puzzle, Smart Replace puzzle, Split & Count Puzzle, Split & count puzzle in SQL, Split and Count puzzle SQL Server, SQL, sql - How to find third or nth maximum salary from salary table?, 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 Puzzle | 2nd Highest Salary in Each Department using OffSet & Fetch Method, SQL Puzzle | Find Different Values Puzzle, SQL Puzzle | Get Weeks (Start Date - End Date) for a Month & Year, SQL Puzzle | How to get Date Part & Time Part only from DateTime in Sql Server [Multiple Methods], SQL Puzzle | Multiplication of Digits Puzzle, SQL Puzzle | Star Pattern Puzzle ( Fun with SQL ), SQL Puzzle | The Fiscal Puzzle - II, SQL Puzzle | The Middle 4 Puzzle, SQL Puzzle | The Missing Gaps Puzzle [Multiple Solutions], SQL Puzzle | The Time Difference 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 - Group sum, sql server - How to find the nth highest salary in SQL?, 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 Indexes, 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 % percentage puzzle, The Biggest Gap Puzzle, The Gap Puzzle, The Gap Puzzle Puzzle, The In Puzzle, The InNotIn Puzzle, The Middle 4 Puzzle, The Middle 4 SQL Puzzle, The Multiple Sums Puzzle, The new rows count puzzle, The Phone Call Puzzle, The Prime Number Puzzle, The Replace Puzzle, The Time Difference Puzzle, The Tree Puzzle, The Update Puzzle, Top 10 Frequently Asked SQL Query Interview Questions, TOP 100 SQL SERVER INTERVIEW QUESTIONS QUERIES, Top 50 SQL Server Interview Question for Testers, TOP Clause, Tough SQL Challenges, Tough SQL Puzzles, Tricky Questions, TSQL, TSQL Challenge, TSQL Challenges, TSQL Interview questions, TSQL Puzzle, TSQL Puzzles, TSQL Queries, Update one table and Insert in another table using single T-SQL statement at same time, Week puzzle, Write a SQL Query to Calculate the longest period ( in days ) that the company has gone without hiring or firing any one, [Solved] sql query for nth highest & lowest salary


SQL Puzzle | The Phone Call Puzzle

Write a T-SQL query that fetches records where the person’s first and last call of the day is to the same person.

Please check out the sample input and the expected output.

Sample Inputs

ID FromPhoneNumber ToPhoneNumber Starttime EndTime
101 9878650055 9878650002 2016-10-09 12:17:48.483 2016-10-09 12:20:48.483
101 9878650055 9878650000 2016-10-09 13:17:48.483 2016-10-09 13:20:48.483
101 9878650055 9878650008 2016-10-09 14:17:48.483 2016-10-09 14:20:48.483
101 9878650055 9878650000 2016-10-09 15:17:48.483 2016-10-09 15:20:48.483
101 9878650055 9878650002 2016-10-09 20:17:48.483 2016-10-09 20:20:48.483
102 9878650051 9878650099 2016-10-09 12:17:48.483 2016-10-09 12:20:48.483
102 9878650051 9878650099 2016-10-09 12:21:48.483 2016-10-09 12:25:48.483
102 9878650051 9878650099 2016-10-09 12:29:48.483 2016-10-09 12:31:48.483
102 9878650051 9878650077 2016-10-09 20:29:48.483 2016-10-09 20:35:48.483

Expected Output

ID FromPhoneNumber ToPhoneNumber Date
101 9878650055 9878650002 2016-10-09

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 PhoneLog
(
       ID INT,
       FromPhoneNumber Varchar(100),
       ToPhoneNumber Varchar(100),
       Starttime DateTime,
       EndTime DateTime
)
GO
 
INSERT INTO PhoneLog Values 

(101,9878650055,9878650002,'2016-10-09 12:17:48.483','2016-10-09 12:20:48.483'),
(101,9878650055,9878650000,'2016-10-09 13:17:48.483','2016-10-09 13:20:48.483'),
(101,9878650055,9878650008,'2016-10-09 14:17:48.483','2016-10-09 14:20:48.483'),
(101,9878650055,9878650000,'2016-10-09 15:17:48.483','2016-10-09 15:20:48.483'),
(101,9878650055,9878650002,'2016-10-09 20:17:48.483','2016-10-09 20:20:48.483'),
 
(102,9878650051,9878650099,'2016-10-09 12:17:48.483','2016-10-09 12:20:48.483'),
(102,9878650051,9878650099,'2016-10-09 12:21:48.483','2016-10-09 12:25:48.483'),
(102,9878650051,9878650099,'2016-10-09 12:29:48.483','2016-10-09 12:31:48.483'),
(102,9878650051,9878650077,'2016-10-09 20:29:48.483','2016-10-09 20:35:48.483')
 
GO



--

—

SOLUTION – 1


--

SELECT tbl.Id, tbl.FromPhoneNumber, tbl.ToPhoneNumber, tbl.distinctdate Date 
FROM 
(
       SELECT  a.Id, a.FromPhoneNumber, a.ToPhoneNumber, a.Starttime, Format(Starttime, 'MM/dd/yyyy') distinctdate
       FROM    PhoneLog a
                    OUTER APPLY ( SELECT MIN(starttime) CallDateTime
                                          FROM      Phonelog b
                                          WHERE     b.ID = a.ID AND Format(b.Starttime, 'MM/dd/yyyy') = Format(a.Starttime, 'MM/dd/yyyy')
       UNION ALL 
        SELECT MAX(starttime) 
                                          FROM      Phonelog b
                                          WHERE     b.ID = a.ID AND Format(b.Starttime, 'MM/dd/yyyy') = Format(a.Starttime, 'MM/dd/yyyy')
                                        ) C
       WHERE a.Starttime = c.CallDateTime
) tbl
GROUP BY tbl.Id, tbl.FromPhoneNumber, tbl.ToPhoneNumber, tbl.distinctdate
HAVING COUNT(tbl.ToPhoneNumber) = 2


--

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

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email

SQL Puzzle | The Greatest Number of Employees (Solution Time:))

30 Friday Sep 2016

Posted by Pawan Kumar Khowal in SQL Puzzles, SQL SERVER, SQL SERVER Puzzles, T SQL Puzzles, Tricky SQL Queries

≈ Leave a comment

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, Even Odd Count Puzzle, Excellent t-SQL puzzles, Find nth highest salary, Find Nth maximum value in SQL Server, find records not ending with s, find records not ending with s puzzle, Finding prime numbers with SQL server, First letter upper case for each word in sql server, Free Download SQL SERVER Interview questions, Generate possible combinations puzzle, Get nth highest and lowest salary of an employee, Getting group by sum and total sum in a single query, Group by numbers that are in sequence, group sum total in one query, implementation and administration, Improve SQL Skills, Interesting Interview Questions, 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, Joins, Joins Interview questions, Joins Puzzle, Khowal, last non null puzzle, Learn complex SQL, Learn SQL, Learn T-SQL, Microsoft SQL Server interview questions for DBA, MS SQL Server interview questions, MSBI Interview Questions, Multiple Column Pivoting Puzzle, Multiplication of Digits Puzzle, Multiply digits of a number, multiply digits of a number puzzle, nth maximum & minimum salary in a single sql query, Objective Puzzle, Pawan, Pawan Khowal, Pawan Kumar, Pawan Kumar Khowal, PL/SQL Challenges, Prev Value puzzle, Previous value puzzle, Prime numbers after given number puzzle, Prime numbers using SQL, Program to Print prime numbers, Puzzle SQL, puzzle sql developer, Puzzle SQl Server, Puzzles, PUzzles in SQL SERVER, Queries for SQL Interview, Records not ending with a character Puzzle, Records not ending with s Puzzle, Replace string puzzle, SELECT Puzzle, Single Query multiple conditions puzzle, Smart Replace puzzle, Split & Count Puzzle, Split & count puzzle in SQL, Split and Count puzzle SQL Server, SQL, sql - How to find third or nth maximum salary from salary table?, 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 Puzzle | 2nd Highest Salary in Each Department using OffSet & Fetch Method, SQL Puzzle | Find Different Values Puzzle, SQL Puzzle | Get Weeks (Start Date - End Date) for a Month & Year, SQL Puzzle | How to get Date Part & Time Part only from DateTime in Sql Server [Multiple Methods], SQL Puzzle | Multiplication of Digits Puzzle, SQL Puzzle | Star Pattern Puzzle ( Fun with SQL ), SQL Puzzle | The Fiscal Puzzle - II, SQL Puzzle | The Middle 4 Puzzle, SQL Puzzle | The Missing Gaps Puzzle [Multiple Solutions], SQL Puzzle | The Time Difference 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 - Group sum, sql server - How to find the nth highest salary in SQL?, 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 Indexes, 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 % percentage puzzle, The Biggest Gap Puzzle, The Gap Puzzle, The Gap Puzzle Puzzle, The In Puzzle, The InNotIn Puzzle, The Middle 4 Puzzle, The Middle 4 SQL Puzzle, The Multiple Sums Puzzle, The new rows count puzzle, The Prime Number Puzzle, The Replace Puzzle, The Time Difference Puzzle, The Tree Puzzle, The Update Puzzle, Top 10 Frequently Asked SQL Query Interview Questions, TOP 100 SQL SERVER INTERVIEW QUESTIONS QUERIES, Top 50 SQL Server Interview Question for Testers, TOP Clause, Tough SQL Challenges, Tough SQL Puzzles, Tricky Questions, TSQL, TSQL Challenge, TSQL Challenges, TSQL Interview questions, TSQL Puzzle, TSQL Puzzles, TSQL Queries, Update one table and Insert in another table using single T-SQL statement at same time, Week puzzle, Write a SQL Query to Calculate the longest period ( in days ) that the company has gone without hiring or firing any one, [Solved] sql query for nth highest & lowest salary


SQL Puzzle | The Greatest number of employees

Write a T-SQL query that returns each employee and for each employee include the greatest number of employees that worked for the company at any time during their tenure and the first date that maximum was reached.

Sample Inputs

EmployeeId DepartmentId FirstName LastName Salary HireDate TerminationDate
1 1000 A Z 300000.000 2001-09-01 00:00:00.000 2010-01-16 00:00:00.000
2 1000 B Y 350000.000 2003-03-15 00:00:00.000 NULL
3 1000 C X 500000.000 2004-01-15 00:00:00.000 2010-12-02 00:00:00.000
4 1000 D W 400000.000 2005-05-15 00:00:00.000 NULL
5 1000 D Q 200000.000 2006-04-15 00:00:00.000 2008-12-02 00:00:00.000
6 1000 E P 300000.000 2007-08-01 00:00:00.000 NULL
7 1000 F N 600000.000 2008-12-01 00:00:00.000 NULL
8 1000 G O 700000.000 2009-12-01 00:00:00.000 NULL
9 1000 H P 1000000.000 2010-01-01 00:00:00.000 NULL
11 1000 J Z 300000.000 2002-09-05 00:00:00.000 2009-12-15 00:00:00.000
10 1000 I Z 300000.000 2001-09-01 00:00:00.000 2009-12-05 00:00:00.000

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 Employeex
(
       EmployeeId BIGINT
       ,DepartmentId INT
       ,FirstName VARCHAR(1)
       ,LastName VARCHAR(1)
       ,Salary NUMERIC(10,3)
       ,HireDate DATETIME 
       ,TerminationDate DATETIME 
)
GO

INSERT INTO Employeex VALUES
(1,1000,'A','Z',300000,'2001-09-01 00:00:00.000','2010-01-16 00:00:00.000'),
(2,1000,'B','Y',350000,'2003-03-15 00:00:00.000',NULL),
(3,1000,'C','X',500000,'2004-01-15 00:00:00.000','2010-12-02 00:00:00.000'),
(4,1000,'D','W',400000,'2005-05-15 00:00:00.000',NULL),
(5,1000,'D','Q',200000,'2006-04-15 00:00:00.000','2008-12-02 00:00:00.000'),
(6,1000,'E','P',300000,'2007-08-01 00:00:00.000',NULL),
(7,1000,'F','N',600000,'2008-12-01 00:00:00.000',NULL),
(8,1000,'G','O',700000,'2009-12-01 00:00:00.000',NULL),
(9,1000,'H','P',1000000,'2010-01-01 00:00:00.000',NULL),
(11,1000,'J','Z',300000,'2002-09-05 00:00:00.000','2009-12-15 00:00:00.000'),
(10,1000,'I','Z',300000,'2001-09-01 00:00:00.000','2009-12-05 00:00:00.000')

--

—

SOLUTION – 1


--

SELECT * FROM Employeex x
CROSS APPLY
(
       SELECT TOP 1 * FROM
       (
             SELECT d StartDate, Next EndD , SUM(Op) OVER (ORDER BY d) #Employees FROM 
             (
                    SELECT DISTINCT d , ISNULL(LEAD(d) OVER (ORDER BY d), GETDATE()) Next  , Op FROM 
                    (
                           SELECT HireDate d , 1 Op  FROM Employeex
                           UNION ALL
                           SELECT ISNULL(TerminationDate,GETDATE()) TerminationDate , -1 FROM Employeex
                    )r     
             ) cr
       )e
       WHERE e.EndD BETWEEN x.HireDate AND ISNULL(x.TerminationDate,GETDATE())
       ORDER BY #Employees DESC
)r


--

SOLUTION – 2


--


select * from Employeex e1 cross apply
(
       Select top 1 dateadd(month, e.number ,'20000101') dt, cnt1 cnt1 from (select top 10000 row_number() over (order by (select null)) - 1 number 
       from Master..spt_values a cross join Master..spt_values b) e
       cross apply 
       (
             select count(*) cnt1 from Employeex e1 where dateadd(month, e.number ,'20000101') between e1.HireDate and isnull(e1.TerminationDate, getdate())  
       ) A 
       where dateadd(month, e.number ,'20000101') <= getdate() and dateadd(month, e.number ,'20000101') between e1.HireDate and isnull(e1.TerminationDate,getdate())
       order by cnt1 desc, dateadd(month, e.number ,'20000101')
) B


--

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

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email

SQL Puzzle | The Longest Period – II [ SQL Query to Calculate the longest period ( in days ) that the company has gone without hiring or firing any one ] (Solution Time :))

29 Thursday Sep 2016

Posted by Pawan Kumar Khowal in SQL Puzzles, SQL SERVER, SQL SERVER Puzzles, T SQL Puzzles, Tricky SQL Queries

≈ 6 Comments

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, Even Odd Count Puzzle, Excellent t-SQL puzzles, Find nth highest salary, Find Nth maximum value in SQL Server, find records not ending with s, find records not ending with s puzzle, Finding prime numbers with SQL server, First letter upper case for each word in sql server, Free Download SQL SERVER Interview questions, Generate possible combinations puzzle, Get nth highest and lowest salary of an employee, Getting group by sum and total sum in a single query, Group by numbers that are in sequence, group sum total in one query, implementation and administration, Improve SQL Skills, Interesting Interview Questions, 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, Joins, Joins Interview questions, Joins Puzzle, Khowal, last non null puzzle, Learn complex SQL, Learn SQL, Learn T-SQL, Microsoft SQL Server interview questions for DBA, MS SQL Server interview questions, MSBI Interview Questions, Multiple Column Pivoting Puzzle, Multiplication of Digits Puzzle, Multiply digits of a number, multiply digits of a number puzzle, nth maximum & minimum salary in a single sql query, Objective Puzzle, Pawan, Pawan Khowal, Pawan Kumar, Pawan Kumar Khowal, PL/SQL Challenges, Prev Value puzzle, Previous value puzzle, Prime numbers after given number puzzle, Prime numbers using SQL, Program to Print prime numbers, Puzzle SQL, puzzle sql developer, Puzzle SQl Server, Puzzles, PUzzles in SQL SERVER, Queries for SQL Interview, Records not ending with a character Puzzle, Records not ending with s Puzzle, Replace string puzzle, SELECT Puzzle, Single Query multiple conditions puzzle, Smart Replace puzzle, Split & Count Puzzle, Split & count puzzle in SQL, Split and Count puzzle SQL Server, SQL, sql - How to find third or nth maximum salary from salary table?, 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 Puzzle | 2nd Highest Salary in Each Department using OffSet & Fetch Method, SQL Puzzle | Find Different Values Puzzle, SQL Puzzle | Get Weeks (Start Date - End Date) for a Month & Year, SQL Puzzle | How to get Date Part & Time Part only from DateTime in Sql Server [Multiple Methods], SQL Puzzle | Multiplication of Digits Puzzle, SQL Puzzle | Star Pattern Puzzle ( Fun with SQL ), SQL Puzzle | The Fiscal Puzzle - II, SQL Puzzle | The Middle 4 Puzzle, SQL Puzzle | The Missing Gaps Puzzle [Multiple Solutions], SQL Puzzle | The Time Difference 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 - Group sum, sql server - How to find the nth highest salary in SQL?, 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 Indexes, 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 % percentage puzzle, The Biggest Gap Puzzle, The Gap Puzzle, The Gap Puzzle Puzzle, The In Puzzle, The InNotIn Puzzle, The Middle 4 Puzzle, The Middle 4 SQL Puzzle, The Multiple Sums Puzzle, The new rows count puzzle, The Prime Number Puzzle, The Replace Puzzle, The Time Difference Puzzle, The Tree Puzzle, The Update Puzzle, Top 10 Frequently Asked SQL Query Interview Questions, TOP 100 SQL SERVER INTERVIEW QUESTIONS QUERIES, Top 50 SQL Server Interview Question for Testers, TOP Clause, Tough SQL Challenges, Tough SQL Puzzles, Tricky Questions, TSQL, TSQL Challenge, TSQL Challenges, TSQL Interview questions, TSQL Puzzle, TSQL Puzzles, TSQL Queries, Update one table and Insert in another table using single T-SQL statement at same time, Week puzzle, Write a SQL Query to Calculate the longest period ( in days ) that the company has gone without hiring or firing any one, [Solved] sql query for nth highest & lowest salary


SQL Puzzle | The Longest Period – II

This puzzle is simple. Write a SQL Query to Calculate the longest period ( in days ) that the company has gone without hiring or firing any one.

Sample Inputs

EmployeeId DepartmentId FirstName LastName Salary HireDate TerminationDate
1 1000 A Z 300000.000 2001-09-01 00:00:00.000 2010-01-16 00:00:00.000
2 1000 B Y 350000.000 2003-03-15 00:00:00.000 NULL
3 1000 C X 500000.000 2010-01-15 00:00:00.000 2015-12-02 00:00:00.000
4 1000 D W 400000.000 2012-05-15 00:00:00.000 NULL
5 1000 D Q 200000.000 2014-04-15 00:00:00.000 2015-12-02 00:00:00.000
6 1000 E P 300000.000 2015-08-01 00:00:00.000 NULL
7 1000 F N 600000.000 2015-12-01 00:00:00.000 NULL
8 1000 G O 700000.000 2015-12-01 00:00:00.000 NULL
9 1000 H P 1000000.000 2016-01-01 00:00:00.000 NULL

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 Employeex
(
	 EmployeeId BIGINT
	,DepartmentId INT
	,FirstName VARCHAR(1)
	,LastName VARCHAR(1)
	,Salary NUMERIC(10,3)
	,HireDate DATETIME 
	,TerminationDate DATETIME 
)
GO

INSERT INTO Employeex VALUES
(1,1000,'A','Z',300000,'2001-09-01 00:00:00.000','2010-01-16 00:00:00.000'),
(2,1000,'B','Y',350000,'2003-03-15 00:00:00.000',NULL),
(3,1000,'C','X',500000,'2010-01-15 00:00:00.000','2015-12-02 00:00:00.000'),
(4,1000,'D','W',400000,'2012-05-15 00:00:00.000',NULL),
(5,1000,'D','Q',200000,'2014-04-15 00:00:00.000','2015-12-02 00:00:00.000'),
(6,1000,'E','P',300000,'2015-08-01 00:00:00.000',NULL),
(7,1000,'F','N',600000,'2015-12-01 00:00:00.000',NULL),
(8,1000,'G','O',700000,'2015-12-01 00:00:00.000',NULL),
(9,1000,'H','P',1000000,'2016-01-01 00:00:00.000',NULL)
GO

--

—

SOLUTION – 1


--

SELECT MAX(dr) [LongestPeriod] FROM 
(
       SELECT DATEDIFF(d, a, ISNULL(LEAD(a) OVER (ORDER BY a),GETDATE())) dr FROM 
       (
             SELECT HireDate a FROM Employeex UNION ALL SELECT TerminationDate FROM Employeex WHERE TerminationDate IS NOT NULL
       )t
)r


--

SOLUTION – 2


--

SELECT MAX(gap) [LongestPeriod] FROM
(
       SELECT DATEDIFF(D, EmpDate, ISNULL(LEAD(EmpDate) OVER (ORDER BY EmpDate),GETDATE())) as gap FROM (
              SELECT EmpDate FROM Employeex
                      UNPIVOT(EmpDate FOR DateVal IN (HireDate, TerminationDate)) AS U
       )TblEmp 
)TempEmp

--

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

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email

SQL Puzzle | The Date Puzzle – II (Solution Time :))

28 Wednesday Sep 2016

Posted by Pawan Kumar Khowal in SQL Puzzles, SQL SERVER, SQL SERVER Puzzles, T SQL Puzzles, Tricky SQL Queries

≈ Leave a comment

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, Even Odd Count Puzzle, Excellent t-SQL puzzles, Find nth highest salary, Find Nth maximum value in SQL Server, find records not ending with s, find records not ending with s puzzle, Finding prime numbers with SQL server, First letter upper case for each word in sql server, Free Download SQL SERVER Interview questions, Generate possible combinations puzzle, Get nth highest and lowest salary of an employee, Getting group by sum and total sum in a single query, Group by numbers that are in sequence, group sum total in one query, implementation and administration, Improve SQL Skills, Interesting Interview Questions, 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, Joins, Joins Interview questions, Joins Puzzle, Khowal, last non null puzzle, Learn complex SQL, Learn SQL, Learn T-SQL, Microsoft SQL Server interview questions for DBA, MS SQL Server interview questions, MSBI Interview Questions, Multiple Column Pivoting Puzzle, Multiplication of Digits Puzzle, Multiply digits of a number, multiply digits of a number puzzle, nth maximum & minimum salary in a single sql query, Objective Puzzle, Pawan, Pawan Khowal, Pawan Kumar, Pawan Kumar Khowal, PL/SQL Challenges, Prev Value puzzle, Previous value puzzle, Prime numbers after given number puzzle, Prime numbers using SQL, Program to Print prime numbers, Puzzle SQL, puzzle sql developer, Puzzle SQl Server, Puzzles, PUzzles in SQL SERVER, Queries for SQL Interview, Records not ending with a character Puzzle, Records not ending with s Puzzle, Replace string puzzle, SELECT Puzzle, Single Query multiple conditions puzzle, Smart Replace puzzle, Split & Count Puzzle, Split & count puzzle in SQL, Split and Count puzzle SQL Server, SQL, sql - How to find third or nth maximum salary from salary table?, 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 Puzzle | 2nd Highest Salary in Each Department using OffSet & Fetch Method, SQL Puzzle | Find Different Values Puzzle, SQL Puzzle | Get Weeks (Start Date - End Date) for a Month & Year, SQL Puzzle | How to get Date Part & Time Part only from DateTime in Sql Server [Multiple Methods], SQL Puzzle | Multiplication of Digits Puzzle, SQL Puzzle | Star Pattern Puzzle ( Fun with SQL ), SQL Puzzle | The Fiscal Puzzle - II, SQL Puzzle | The Middle 4 Puzzle, SQL Puzzle | The Missing Gaps Puzzle [Multiple Solutions], SQL Puzzle | The Time Difference 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 - Group sum, sql server - How to find the nth highest salary in SQL?, 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 Indexes, 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 % percentage puzzle, The Biggest Gap Puzzle, The Gap Puzzle, The Gap Puzzle Puzzle, The In Puzzle, The InNotIn Puzzle, The Middle 4 Puzzle, The Middle 4 SQL Puzzle, The Multiple Sums Puzzle, The new rows count puzzle, The Prime Number Puzzle, The Replace Puzzle, The Time Difference Puzzle, The Tree Puzzle, The Update Puzzle, Top 10 Frequently Asked SQL Query Interview Questions, TOP 100 SQL SERVER INTERVIEW QUESTIONS QUERIES, Top 50 SQL Server Interview Question for Testers, TOP Clause, Tough SQL Challenges, Tough SQL Puzzles, Tricky Questions, TSQL, TSQL Challenge, TSQL Challenges, TSQL Interview questions, TSQL Puzzle, TSQL Puzzles, TSQL Queries, Update one table and Insert in another table using single T-SQL statement at same time, Week puzzle, [Solved] sql query for nth highest & lowest salary


SQL Puzzle | The Date Puzzle – II

This puzzle is simple. Here you need to write a query to get minimum and maximum value records.

For details please check out the sample input and the expected output below.

Sample Inputs

Date value time
2016-09-20 17:48:23.447 63 17:48:00.0000000
2016-09-20 17:38:08.880 64 17:38:00.0000000
2016-09-20 17:32:03.247 65 17:32:00.0000000
2016-09-20 17:16:23.273 68 17:16:00.0000000
2016-09-20 17:06:27.223 69 17:06:00.0000000
2016-09-20 16:58:27.693 70 16:58:00.0000000
2016-09-20 16:46:27.030 71 16:46:00.0000000
2016-09-20 16:38:19.490 72 16:38:00.0000000
2016-09-20 16:27:07.743 74 16:27:00.0000000
2016-09-20 16:22:19.243 75 16:22:00.0000000
2016-09-20 16:08:24.197 77 16:08:00.0000000
2016-09-20 15:57:02.757 79 15:57:00.0000000
2016-09-20 15:47:23.227 80 15:47:00.0000000
2016-09-20 15:38:23.937 77 15:38:00.0000000
2016-09-20 15:26:22.017 73 15:26:00.0000000
2016-09-20 15:17:23.277 73 15:17:00.0000000
2016-09-20 15:06:36.990 74 15:06:00.0000000
2016-09-20 14:56:42.550 76 14:56:00.0000000
2016-09-20 14:47:07.823 77 14:47:00.0000000
2016-09-20 14:37:23.907 79 14:37:00.0000000
2016-09-20 14:27:38.387 81 14:27:00.0000000
2016-09-20 14:17:23.307 83 14:17:00.0000000
2016-09-20 14:07:23.687 85 14:07:00.0000000
2016-09-20 13:57:08.230 87 13:57:00.0000000
2016-09-20 13:47:23.307 88 13:47:00.0000000
2016-09-20 13:37:23.463 89 13:37:00.0000000
2016-09-20 13:27:44.067 90 13:27:00.0000000
2016-09-20 13:16:41.130 91 13:16:00.0000000
2016-09-20 13:06:22.003 92 13:06:00.0000000
2016-09-20 13:01:33.437 93 13:01:00.0000000
2016-09-20 12:47:23.217 95 12:47:00.0000000
2016-09-20 12:37:04.723 96 12:37:00.0000000

Expected Output

MinDate MaxDate Value Time
9/20/2016 12:37 9/20/2016 17:48 63 5:48:00 PM
9/20/2016 12:37 9/20/2016 17:48 96 12:37:00 PM

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 testDate
(
       [Date] DATETIME
       ,[value] INT
       ,[time] TIME
)
GO
 
INSERT INTO testDate VALUES
('2016-09-20 17:48:23.447',      63      ,'17:48'),
('2016-09-20 17:38:08.880',      64      ,'17:38'),
('2016-09-20 17:32:03.247',      65      ,'17:32'),
('2016-09-20 17:16:23.273',      68      ,'17:16'),
('2016-09-20 17:06:27.223',      69      ,'17:06'),
('2016-09-20 16:58:27.693',      70      ,'16:58'),
('2016-09-20 16:46:27.030',      71      ,'16:46'),
('2016-09-20 16:38:19.490',      72      ,'16:38'),
('2016-09-20 16:27:07.743',      74      ,'16:27'),
('2016-09-20 16:22:19.243',      75      ,'16:22'),
('2016-09-20 16:08:24.197',      77      ,'16:08'),
('2016-09-20 15:57:02.757',      79      ,'15:57'),
('2016-09-20 15:47:23.227',      80      ,'15:47'),
('2016-09-20 15:38:23.937',      77      ,'15:38'),
('2016-09-20 15:26:22.017',      73      ,'15:26'),
('2016-09-20 15:17:23.277',      73      ,'15:17'),
('2016-09-20 15:06:36.990',      74      ,'15:06'),
('2016-09-20 14:56:42.550',      76      ,'14:56'),
('2016-09-20 14:47:07.823',      77      ,'14:47'),
('2016-09-20 14:37:23.907',      79      ,'14:37'),
('2016-09-20 14:27:38.387',      81      ,'14:27'),
('2016-09-20 14:17:23.307',      83      ,'14:17'),
('2016-09-20 14:07:23.687',      85      ,'14:07'),
('2016-09-20 13:57:08.230',      87      ,'13:57'),
('2016-09-20 13:47:23.307',      88      ,'13:47'),
('2016-09-20 13:37:23.463',      89      ,'13:37'),
('2016-09-20 13:27:44.067',      90      ,'13:27'),
('2016-09-20 13:16:41.130',      91      ,'13:16'),
('2016-09-20 13:06:22.003',      92      ,'13:06'),
('2016-09-20 13:01:33.437',      93      ,'13:01'),
('2016-09-20 12:47:23.217',      95      ,'12:47'),
('2016-09-20 12:37:04.723',      96      ,'12:37')
GO


--

—

SOLUTION – 1


--

SELECT * FROM
(
       SELECT DISTINCT CAST(p.[Date] as DATE) [Date] FROM testDate p
)p
CROSS APPLY
(
       SELECT MIN([Date]) MinDate , MAX([Date]) MaxDate FROM testDate t
       WHERE CAST(t.[DATE] AS DATE) = ((p.[DATE]))
)r
CROSS APPLY
(
       SELECT [value],[Time] FROM testDate t
       WHERE (t.[Date] = r.MinDate OR  t.[Date] = r.MaxDate)
)s
 
--

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

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email

SQL Puzzle | 2 Digit Puzzle [No Reverse Function] (Solution Time :))

27 Tuesday Sep 2016

Posted by Pawan Kumar Khowal in SQL Puzzles, SQL SERVER, SQL SERVER Puzzles, T SQL Puzzles, Tricky SQL Queries

≈ 5 Comments

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, Even Odd Count Puzzle, Excellent t-SQL puzzles, Find nth highest salary, Find Nth maximum value in SQL Server, find records not ending with s, find records not ending with s puzzle, Finding prime numbers with SQL server, First letter upper case for each word in sql server, Free Download SQL SERVER Interview questions, Generate possible combinations puzzle, Get nth highest and lowest salary of an employee, Getting group by sum and total sum in a single query, Group by numbers that are in sequence, group sum total in one query, implementation and administration, Improve SQL Skills, Interesting Interview Questions, 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, Joins, Joins Interview questions, Joins Puzzle, Khowal, last non null puzzle, Learn complex SQL, Learn SQL, Learn T-SQL, Microsoft SQL Server interview questions for DBA, MS SQL Server interview questions, MSBI Interview Questions, Multiple Column Pivoting Puzzle, Multiplication of Digits Puzzle, Multiply digits of a number, multiply digits of a number puzzle, nth maximum & minimum salary in a single sql query, Objective Puzzle, Pawan, Pawan Khowal, Pawan Kumar, Pawan Kumar Khowal, PL/SQL Challenges, Prev Value puzzle, Previous value puzzle, Prime numbers after given number puzzle, Prime numbers using SQL, Program to Print prime numbers, Puzzle SQL, puzzle sql developer, Puzzle SQl Server, Puzzles, PUzzles in SQL SERVER, Queries for SQL Interview, Records not ending with a character Puzzle, Records not ending with s Puzzle, Replace string puzzle, SELECT Puzzle, Single Query multiple conditions puzzle, Smart Replace puzzle, Split & Count Puzzle, Split & count puzzle in SQL, Split and Count puzzle SQL Server, SQL, sql - How to find third or nth maximum salary from salary table?, 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 Puzzle | 2nd Highest Salary in Each Department using OffSet & Fetch Method, SQL Puzzle | Find Different Values Puzzle, SQL Puzzle | Get Weeks (Start Date - End Date) for a Month & Year, SQL Puzzle | How to get Date Part & Time Part only from DateTime in Sql Server [Multiple Methods], SQL Puzzle | Multiplication of Digits Puzzle, SQL Puzzle | Star Pattern Puzzle ( Fun with SQL ), SQL Puzzle | The Fiscal Puzzle - II, SQL Puzzle | The Middle 4 Puzzle, SQL Puzzle | The Missing Gaps Puzzle [Multiple Solutions], SQL Puzzle | The Time Difference 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 - Group sum, sql server - How to find the nth highest salary in SQL?, 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 Indexes, 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 % percentage puzzle, The Biggest Gap Puzzle, The Gap Puzzle, The Gap Puzzle Puzzle, The In Puzzle, The InNotIn Puzzle, The Middle 4 Puzzle, The Middle 4 SQL Puzzle, The Multiple Sums Puzzle, The new rows count puzzle, The Prime Number Puzzle, The Replace Puzzle, The Time Difference Puzzle, The Tree Puzzle, The Update Puzzle, Top 10 Frequently Asked SQL Query Interview Questions, TOP 100 SQL SERVER INTERVIEW QUESTIONS QUERIES, Top 50 SQL Server Interview Question for Testers, TOP Clause, Tough SQL Challenges, Tough SQL Puzzles, Tricky Questions, TSQL, TSQL Challenge, TSQL Challenges, TSQL Interview questions, TSQL Puzzle, TSQL Puzzles, TSQL Queries, Update one table and Insert in another table using single T-SQL statement at same time, Week puzzle, [Solved] sql query for nth highest & lowest salary


SQL Puzzle | 2 Digit Puzzle

Here you need to write a query to get distinct digits from the table. Now here half data has been reversely inserted. e.g. For 21 we have inserted 12. So out of 12 and 21 we need 21 as a single row.

For details please check out the sample input and the expected output below.

Sample Inputs

Id
21
12
34
43
29
92

Expected Output

Id
21
43
92

Rules/Restrictions

  • The challenge is to do it with T-SQL statements only.
  • Reverse function is not allowed.
  • 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 [2Digits]
(
	Id INT
)
GO

INSERT INTO [2Digits] VALUES
(21),
(12),
(34),
(43),
(29),
(92)



--

—

SOLUTION – 1


--

SELECT 
	DISTINCT CASE WHEN Id % 10 < (id / 10) THEN (id / 10)*10 + (Id % 10) ELSE (Id % 10 )*10+(id / 10) END Id 
FROM 
[2Digits]

--

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

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email
← Older posts
Newer posts →

Blog Stats

  • 1,086,917 hits

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 1,131 other subscribers

Pawan Khowal

502 SQL Puzzles with answers

Achievement - 500 PuzzlesJuly 18, 2018
The big day is here. Finally presented 500+ puzzles for SQL community.

200 SQL Server Puzzle with Answers

The Big DayAugust 19, 2016
The big day is here. Completed 200 SQL Puzzles today

Archives

March 2023
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  
« Oct    

Top Articles

  • pawankkmr.wordpress.com/2…
  • pawankkmr.wordpress.com/2…
  • pawankkmr.wordpress.com/2…
  • pawankkmr.wordpress.com/2…
  • pawankkmr.wordpress.com/2…

Archives

  • October 2020 (29)
  • September 2018 (2)
  • August 2018 (6)
  • July 2018 (25)
  • June 2018 (22)
  • May 2018 (24)
  • April 2018 (33)
  • March 2018 (35)
  • February 2018 (53)
  • January 2018 (48)
  • December 2017 (32)
  • November 2017 (2)
  • October 2017 (20)
  • August 2017 (8)
  • June 2017 (2)
  • March 2017 (1)
  • February 2017 (18)
  • January 2017 (2)
  • December 2016 (5)
  • November 2016 (23)
  • October 2016 (2)
  • September 2016 (14)
  • August 2016 (6)
  • July 2016 (22)
  • June 2016 (27)
  • May 2016 (15)
  • April 2016 (7)
  • March 2016 (5)
  • February 2016 (7)
  • December 2015 (4)
  • October 2015 (23)
  • September 2015 (31)
  • August 2015 (14)
  • July 2015 (16)
  • June 2015 (29)
  • May 2015 (25)
  • April 2015 (44)
  • March 2015 (47)
  • November 2012 (1)
  • July 2012 (8)
  • September 2010 (26)
  • August 2010 (125)
  • July 2010 (2)

Article Categories

  • Analysis Services (6)
    • DAX (6)
  • Data (2)
    • Data warehousing (2)
  • Integration Services (2)
  • Magazines (3)
  • Python (29)
  • Reporting Services (4)
  • SQL SERVER (820)
    • Download SQL Interview Q's (212)
    • SQL Concepts (323)
    • SQL Performance Tuning (155)
    • SQL Puzzles (331)
    • SQL SERVER 2017 Linux (6)
    • SQL Server Interview Questions (308)
    • SQL SERVER Puzzles (332)
    • T SQL Puzzles (547)
    • Tricky SQL Queries (439)
  • UI (30)
    • ASP.NET (5)
    • C# (13)
    • CSS (9)
    • OOPS (3)
  • Uncategorized (5)

Recent Posts

  • Python | The Print and Divide Puzzle October 30, 2020
  • Python | Count consecutive 1’s from a list of 0’s and 1’s October 30, 2020
  • Python | How to convert a number into a list of its digits October 26, 2020
  • Python | Validate an IP Address-IPV6(Internet Protocol version 6) October 26, 2020
  • Python | Print the first non-recurring element in a list October 26, 2020
  • Python | Print the most recurring element in a list October 26, 2020
  • Python | Find the cumulative sum of elements in a list October 26, 2020
  • Python | Check a character is present in a string or not October 26, 2020
  • Python | Check whether a string is palindrome or not October 26, 2020
  • Python | Find the missing number in the array of Ints October 26, 2020
  • Python | How would you delete duplicates in a list October 26, 2020
  • Python | Check whether an array is Monotonic or not October 26, 2020
  • Python | Check whether a number is prime or not October 26, 2020
  • Python | Print list of prime numbers up to a number October 26, 2020
  • Python | Print elements from odd positions in a list October 26, 2020
  • Python | Print positions of a string present in another string October 26, 2020
  • Python | How to sort an array in ascending order October 26, 2020
  • Python | How to reverse an array October 26, 2020
  • Python | Find un-common words from two strings October 26, 2020
  • Python | How to convert a string to a list October 26, 2020
  • Python | Find unique words from a string October 26, 2020
  • Python | Calculate average word length from a string October 26, 2020
  • Python | Find common words from two strings October 26, 2020
  • Python | Find the number of times a substring present in a string October 26, 2020
  • Python | Find maximum value from a list October 26, 2020
  • Python | How to find GCF of two numbers October 26, 2020
  • Python | How to find LCM of two numbers October 26, 2020
  • Python | How to convert a list to a string October 26, 2020
  • Python | Replace NONE by its previous NON None value October 26, 2020
  • Microsoft SQL Server 2019 | Features added to SQL Server on Linux September 26, 2018

Create a website or blog at WordPress.com

  • Follow Following
    • Improving my SQL BI Skills
    • Join 231 other followers
    • Already have a WordPress.com account? Log in now.
    • Improving my SQL BI Skills
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar