Tags
Advanced SQL Interview Questions and Answers, Advanced SQL tutorial pdf, Buy SQL Server Interview Questions Book Online at Low Price, Complex SQL Challenges, complex sql statement(puzzle), Complex TSQL Challenge, 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, find records not ending with s, find records not ending with s puzzle, Free Download SQL SERVER Interview questions, implementation and administration, 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, Multiplication of Digits Puzzle, Multiply digits of a number, multiply digits of a number puzzle, Objective Puzzle, Pawan, Pawan Khowal, Pawan Kumar, Pawan Kumar Khowal, PL/SQL Challenges, Prev Value puzzle, Previous value puzzle, 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, SELECT Puzzle, SQL, SQL 2012, SQL 2014, SQL 2014 Interview Questions, SQL Challenge, SQL Challenges, SQL FAQ, SQL FAQs, SQL Interview Q & A, SQL Interview Questions, SQL Interview Questions - Part 2, SQL Interview Questions for SQL Professionals, SQL Joins, SQL pl/sql puzzles, SQL puzzle, SQL Puzzle | Multiplication of Digits Puzzle, SQL Puzzles, SQL Queries, SQL Queries asked in interviews, SQL Questions, SQL Quiz, SQL Server - Common Interview Questions and Answers, 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 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 Tricks, SQL Trikcy question, sql/database interview for puzzle sql developer, SQLBI Interview Questions, SQLSERVER, Sum of digits puzzle, Sum of digits SQL puzzle, 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 Biggest Gap Puzzle, The Gap Puzzle, The Gap Puzzle Puzzle, The Tree 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, Week puzzle, What You Can (and Can't) Do With Filtered Indexes
SQL Puzzle – Find Numbers that satisfies below condition-
Number – ReverseDIGITS(Number) = SUMOfDIGITS(Number) + MULTIPLICATIONOFDIGITS(Number)
Friends, today I discuss a numbers puzzle, Original puzzle link is https://community.oracle.com/thread/668829. Well it was asked in oracle community. Still we shall look how can we can achieve this in SQL. The puzzle says-
You have to find +ve numbers who satisfy following condition-
Number – ReverseDIGITS(Number) = SUMOfDIGITS(Number) + MULTIPLICATIONOFDIGITS(Number)
X – Y = T + Z
where
X as a positive number,
Y is the reverse of X,
T is the sum of each number which X has
Z is the product of each number which X has
Example the number is 63.
63 – 36 = 9 + 18
Expected Output
Id |
63 |
726 |
8937 |
Solution # – Here we have to first create 2 functions, one is to find sum of digits and another is find multiplication of digits.
-- --Function to find Multiplication of Digits CREATE FUNCTION [dbo].[MultiplyDigits] ( @InputString VARCHAR(1000) ) RETURNS @results TABLE ( MultiplicationOutput BIGINT ) AS BEGIN DECLARE @str AS VARCHAR(1000) = @InputString ;WITH CTE AS ( SELECT 1 start , CASE WHEN SUBSTRING(@str,1,1) LIKE '[0-9]' THEN CAST(SUBSTRING(@str,1,1) AS TINYINT) ELSE 0 END MultiplicationOutput UNION ALL SELECT start + 1 start , MultiplicationOutput * CASE WHEN SUBSTRING(@str,start+1,1) LIKE '[0-9]' THEN CAST(SUBSTRING(@str,start+1,1) AS TINYINT) ELSE 0 END MultiplicationOutput FROM CTE WHERE start 0 AND number <= DATALENGTH(@intValue) ) x RETURN; END --Create table and insert some sample data into it CREATE TABLE testPuzzle ( Id BIGINT PRIMARY KEY ) GO BEGIN TRAN ;WITH CTE AS ( SELECT 1 start UNION ALL SELECT start + 1 FROM CTE WHERE start 10 AND Id - REVERSE(Id) = ( (SELECT MultiplicationOutput FROM dbo.MultiplyDigits(Id)) + (SELECT SummationOutput FROM dbo.SumofDigits(Id)) ) -- |
Solution by Lonnberg, David
-- /* X-Y=T+Z Y is the reverse of X T is the sum of digits X Z is the product of digits in X */ /* create table #n(n bigint not null primary key clustered); ;with gen as ( --used to generate numbers select num from (values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) as t(num) ), nums as ( select (ones.num + (10 * tens.num) + (100 * hundreds.num) + (1000 * thousands.num) + (10000 * tenthousands.num) + (100000 * hundredthousands.num)) as n from gen ones, gen tens, gen hundreds, gen thousands, gen tenthousands, gen hundredthousands ) insert #n select n from nums where n >= 1 select * from #n; drop table #n; */ ;with d as ( select n, cast(reverse(cast(n as varchar(10))) as int) as [rev], (n - cast(reverse(cast(n as varchar(10))) as int)) as [diff] from #n ), s as ( select 1 as pos, n, cast(substring(cast(n as varchar(10)), 1, 1) as int) as digit from d union all select s.pos + 1, n, cast(substring(cast(n as varchar(10)), s.pos + 1, 1)as int) from s where pos < datalength(cast(n as varchar(10))) ), p as ( select pos, n, digit from s where pos = 1 union all select s.pos, s.n, s.digit * p.digit from s join p on s.pos = p.pos + 1 and s.n = p.n ), sap as ( select n, sod = (select sum(digit) from s where s.n = p.n), pod = digit from p where pos = (select max(pos) from p as p2 where p.n = p2.n) ) select sap.n from sap join d on sap.n = d.n where d.diff = (sap.sod + sap.pod) order by sap.n -- |
Add a comment if you have any other solution in mind. We all need to learn. Enjoy !!!
Keep Learning
Pawan Khowal
Http://MSBISkills.com