Log in  ::  Register  ::  Not logged in

## Puzzle : Generating two unique numbers from an array of numbers (repetation not allowed)

 Author Message Shaun-884394 SSChasing Mays Group: General Forum Members Points: 608 Visits: 1121 Hi All,(This is just for fun, so you may choose to ignore this topic.)Today I read a puzzle somewhere which is mentioned below.I thought why not solve it by some SQL query.Puzzle : Use the numbers 1,2,3,4,5,6,7,8,9 only once to fill in the blanks and complete the equation.(4 _ _ _ 6) - (_ _ _ _) = 33333As you see for the equation we have to generate 2 numbers, first a 3 digit number and second a 4 digit number and none of the digits in both these numbers can be repeated.So here is the query i tried and it worked fine.But i was wondering whether the negation joins i have used is the only way to achieve this?Is there any smarter/efficient way to crack this puzzle?`with a(num) as(select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9),b(num1,num2) as( select convert(bigint, '4' + convert(varchar,a.num) + convert(varchar,b.num) + convert(varchar,c.num) + '6') ,convert(bigint, convert(varchar,d.num) + convert(varchar,e.num) + convert(varchar,f.num) + convert(varchar,g.num)) from a join a as b on a.num <> b.num join a as c on a.num <> b.num and a.num <> c.num and b.num <> c.num join a as d on a.num <> b.num and a.num <> c.num and a.num <> d.num and b.num <> c.num and b.num <> d.num and c.num <> d.num join a as e on a.num <> b.num and a.num <> c.num and a.num <> d.num and a.num <> e.num and b.num <> c.num and b.num <> d.num and b.num <> e.num and c.num <> d.num and c.num <> e.num and d.num <> e.num join a as f on a.num <> b.num and a.num <> c.num and a.num <> d.num and a.num <> e.num and a.num <> f.num and b.num <> c.num and b.num <> d.num and b.num <> e.num and b.num <> f.num and c.num <> d.num and c.num <> e.num and c.num <> f.num and d.num <> e.num and d.num <> f.num and e.num <> f.num join a as g on a.num <> b.num and a.num <> c.num and a.num <> d.num and a.num <> e.num and a.num <> f.num and a.num <> g.num and b.num <> c.num and b.num <> d.num and b.num <> e.num and b.num <> f.num and b.num <> g.num and c.num <> d.num and c.num <> e.num and c.num <> f.num and c.num <> g.num and d.num <> e.num and d.num <> f.num and d.num <> g.num and e.num <> f.num and e.num <> g.num and f.num <> g.num)select * from b where num1-num2 = 33333order by 1,2` Eugene Elutin SSC-Dedicated Group: General Forum Members Points: 32294 Visits: 5478 You don't really need last join as the last digit of the second number can only be '3' `with a(num) as(select 1 union select 2 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9),b(num1,num2) as( select convert(bigint, '4' + convert(varchar,a.num) + convert(varchar,b.num) + convert(varchar,c.num) + '6') ,convert(bigint, convert(varchar,d.num) + convert(varchar,e.num) + convert(varchar,f.num) + '3') from a join a as b on a.num <> b.num join a as c on a.num <> b.num and a.num <> c.num and b.num <> c.num join a as d on a.num <> b.num and a.num <> c.num and a.num <> d.num and b.num <> c.num and b.num <> d.num and c.num <> d.num join a as e on a.num <> b.num and a.num <> c.num and a.num <> d.num and a.num <> e.num and b.num <> c.num and b.num <> d.num and b.num <> e.num and c.num <> d.num and c.num <> e.num and d.num <> e.num join a as f on a.num <> b.num and a.num <> c.num and a.num <> d.num and a.num <> e.num and a.num <> f.num and b.num <> c.num and b.num <> d.num and b.num <> e.num and b.num <> f.num and c.num <> d.num and c.num <> e.num and c.num <> f.num and d.num <> e.num and d.num <> f.num and e.num <> f.num)select distinct *, num1-num2 from b where num1-num2 = 33333order by 1,2`Also, I thought that you shouldn't be able to use 4 and 6 as they were already used by the first number. In this case you will get only single possible result:`with a(num) as(select 1 union select 2 union select 5 union select 7 union select 8 union select 9),b(num1,num2) as( select convert(bigint, '4' + convert(varchar,a.num) + convert(varchar,b.num) + convert(varchar,c.num) + '6') ,convert(bigint, convert(varchar,d.num) + convert(varchar,e.num) + convert(varchar,f.num) + '3') from a join a as b on a.num <> b.num join a as c on a.num <> b.num and a.num <> c.num and b.num <> c.num join a as d on a.num <> b.num and a.num <> c.num and a.num <> d.num and b.num <> c.num and b.num <> d.num and c.num <> d.num join a as e on a.num <> b.num and a.num <> c.num and a.num <> d.num and a.num <> e.num and b.num <> c.num and b.num <> d.num and b.num <> e.num and c.num <> d.num and c.num <> e.num and d.num <> e.num join a as f on a.num <> b.num and a.num <> c.num and a.num <> d.num and a.num <> e.num and a.num <> f.num and b.num <> c.num and b.num <> d.num and b.num <> e.num and b.num <> f.num and c.num <> d.num and c.num <> e.num and c.num <> f.num and d.num <> e.num and d.num <> f.num and e.num <> f.num)select distinct *, num1-num2 from b where num1-num2 = 33333order by 1,2` _____________________________________________"The only true wisdom is in knowing you know nothing""O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D(So many miracle inventions provided by MS to us...)How to post your question to get the best and quick help Eugene Elutin SSC-Dedicated Group: General Forum Members Points: 32294 Visits: 5478 Also, you don't need to check if the same digits from different joins are not equal (eg. if checked a.num<>b.num in one "join" you don't need to check it again in other "joins"). Therefore all JOINs can be rewritten using "incremented" NOT IN:`with a(num) as(select 1 union select 2 union select 5 union select 7 union select 8 union select 9),b(num1,num2) as( select convert(bigint, '4' + convert(varchar,a.num) + convert(varchar,b.num) + convert(varchar,c.num) + '6') ,convert(bigint, convert(varchar,d.num) + convert(varchar,e.num) + convert(varchar,f.num) + '3') from a join a as b on a.num <> b.num join a as c on c.num NOT IN (a.num, b.num) join a as d on d.num NOT IN (a.num, b.num, c.num) join a as e on e.num NOT IN (a.num, b.num, c.num, d.num) join a as f on f.num NOT IN (a.num, b.num, c.num, d.num, e.num))select distinct *, num1-num2 from b where num1-num2 = 33333order by 1,2` _____________________________________________"The only true wisdom is in knowing you know nothing""O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D(So many miracle inventions provided by MS to us...)How to post your question to get the best and quick help GSquared SSC Guru Group: General Forum Members Points: 140683 Visits: 9731 Here's what I came up with. Does the same thing the same way, just another way to write it:`WITH Numbers AS (SELECT * FROM ( VALUES ( '1'), ( '2'), ( '3'), ( '4'), ( '5'), ( '6'), ( '7'), ( '8'), ( '9') ) AS Nums (Number)) SELECT N1.Number + N2.Number + N3.Number + N4.Number + N5.Number, N6.Number + N7.Number + N8.Number + N9.Number FROM Numbers AS N1 CROSS APPLY (SELECT Number FROM Numbers AS N2 WHERE N2.Number != N1.Number) AS N2 CROSS APPLY (SELECT Number FROM Numbers AS N3 WHERE N3.Number NOT IN (N1.Number, N2.Number)) AS N3 CROSS APPLY (SELECT Number FROM Numbers AS N4 WHERE N4.Number NOT IN (N1.Number, N2.Number, N3.Number)) AS N4 CROSS APPLY (SELECT Number FROM Numbers AS N5 WHERE N5.Number NOT IN (N1.Number, N2.Number, N3.Number, N4.Number)) AS N5 CROSS APPLY (SELECT Number FROM Numbers AS N6 WHERE N6.Number NOT IN (N1.Number, N2.Number, N3.Number, N4.Number, N5.Number)) AS N6 CROSS APPLY (SELECT Number FROM Numbers AS N7 WHERE N7.Number NOT IN (N1.Number, N2.Number, N3.Number, N4.Number, N5.Number, N6.Number)) AS N7 CROSS APPLY (SELECT Number FROM Numbers AS N8 WHERE N8.Number NOT IN (N1.Number, N2.Number, N3.Number, N4.Number, N5.Number, N6.Number, N7.Number)) AS N8 CROSS APPLY (SELECT Number FROM Numbers AS N9 WHERE N9.Number NOT IN (N1.Number, N2.Number, N3.Number, N4.Number, N5.Number, N6.Number, N7.Number, N8.Number)) AS N9 WHERE N1.Number = '4' AND N5.Number = '6' AND CAST(N1.Number + N2.Number + N3.Number + N4.Number + N5.Number AS INT) - CAST(N6.Number + N7.Number + N8.Number + N9.Number AS INT) = 33333` - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETCProperty of The Thread"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon Eugene Elutin SSC-Dedicated Group: General Forum Members Points: 32294 Visits: 5478 As said before, you don't need to guess the last digit of the second number as it can be only 3!Saves one cross-apply... _____________________________________________"The only true wisdom is in knowing you know nothing""O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D(So many miracle inventions provided by MS to us...)How to post your question to get the best and quick help Luis Cazares SSC Guru Group: General Forum Members Points: 96522 Visits: 21217 Eugene Elutin (9/12/2012)You don't really need last join as the last digit of the second number can only be '3' Wouldn't that be cheating?Because that way I can get the best performance`SELECT 41286 num1, 7953 num2`I had my solution that was very similar to yours (again) but using the different(<> operators instead of the NOT IN.However, a better performance would be using the best datatype for the digits (char) since the beginning avoiding additional conversions. Luis C.General Disclaimer:Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?How to post data/code on a forum to get the best help: Option 1 / Option 2 Shaun-884394 SSChasing Mays Group: General Forum Members Points: 608 Visits: 1121 Thank you Eugene and GSquared.Eugene, the NOT IN looks like a much better/easier/readable join.GSquared i love your signature ROFL,LOL,ETC .... :-DI don't think it can get any better. Eugene Elutin SSC-Dedicated Group: General Forum Members Points: 32294 Visits: 5478 Luis Cazares (9/12/2012)Eugene Elutin (9/12/2012)You don't really need last join as the last digit of the second number can only be '3' Wouldn't that be cheating?....Cheating? No, it's a common sense. There is no other number you can deduct from 6 to get 3. There is no need to guess that.Regarding performance... I don't think it is relevant to this kind of task at all. I guess finding the more "elegant" looking solution is what OP is after. Saying that, removing number of casting will make it look more elegant:`;with a (num)as( select * from (values ('1'),('2'),('5'),('7'),('8'),('9')) a(num) ),b as( select '4' + a.num + b.num + c.num + '6' as num1 ,d.num + e.num + f.num + '3' as num2 from a join a as b on a.num <> b.num join a as c on c.num NOT IN (a.num, b.num) join a as d on d.num NOT IN (a.num, b.num, c.num) join a as e on e.num NOT IN (a.num, b.num, c.num, d.num) join a as f on f.num NOT IN (a.num, b.num, c.num, d.num, e.num))select * from b where cast(num1 as int) - cast(num2 as int)= 33333` _____________________________________________"The only true wisdom is in knowing you know nothing""O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D(So many miracle inventions provided by MS to us...)How to post your question to get the best and quick help GSquared SSC Guru Group: General Forum Members Points: 140683 Visits: 9731 Luis Cazares (9/12/2012)Eugene Elutin (9/12/2012)You don't really need last join as the last digit of the second number can only be '3' Wouldn't that be cheating?Because that way I can get the best performance`SELECT 41286 num1, 7953 num2`I had my solution that was very similar to yours (again) but using the different(<> operators instead of the NOT IN.However, a better performance would be using the best datatype for the digits (char) since the beginning avoiding additional conversions.(emphasis added)That's why I did mine that way. Makes the concatenation, etc., more efficient, at the cost of the final Where clause being slightly more costly.I also kept the rules for what fixed numbers go in what columns in the Where clause, instead of in the various From objects, because that way it can be modified for related queries more easily. If, for example, the puzzle were modified to "second number must start with '79'" instead of "first number must start with '4' and end with '6'", then a simple modification of the Where clause would make my version work.For related puzzles, you could assign a parameter to each column. If not null, then fixed value for that column. Final value could also be a parameter.And, yeah, I really do think extensibility like that with every piece of code I write. - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETCProperty of The Thread"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon GSquared SSC Guru Group: General Forum Members Points: 140683 Visits: 9731 Shaun-884394 (9/12/2012)...GSquared i love your signature ROFL,LOL,ETC .... :-DI don't think it can get any better.Thanks! :-)The really funny part is, there was a noticable increase in people assuming my posts were credible, when I added all that to my sig. - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETCProperty of The Thread"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

## Permissions

 You can't post new topics. You can't post topic replies. You can't post new polls. You can't post replies to polls. You can't edit your own topics. You can't delete your own topics. You can't edit other topics. You can't delete other topics. You can't edit your own posts. You can't edit other posts. You can't delete your own posts. You can't delete other posts. You can't post events. You can't edit your own events. You can't edit other events. You can't delete your own events. You can't delete other events. You can't send private messages. You can't send emails. You can read topics. You can't vote in polls. You can't upload attachments. You can download attachments. You can't post HTML code. You can't edit HTML code. You can't post IFCode. You can't post JavaScript. You can post emoticons. You can't post or upload images.

##### Select a forum
 SQL Server 2017      SQL Server 2017 - Administration      SQL Server 2017 - Development SQL Server 2016      SQL Server 2016 - Administration      SQL Server 2016 - Development and T-SQL SQL Server 2014      Administration - SQL Server 2014      Development - SQL Server 2014 SQL Server 2012      SQL 2012 - General      SQL Server 2012 - T-SQL SQL Server vNext      SQL Server 15 - Administration      SQL Server 15 - Development SQL Server 2008      SQL Server 2008 - General      T-SQL (SS2K8)      June 2007 CTP      Working with Oracle      July CTP      SQL Server Newbies      Security (SS2K8)      SQL Server 2008 High Availability      SQL Server 2008 Administration      Data Corruption (SS2K8 / SS2K8 R2)      SQL Server 2008 Performance Tuning Cloud Computing      SQL Azure - Development      SQL Azure - Administration      Amazon AWS and other cloud vendors      General Cloud Computing Questions      CosmosDB      Azure Data Lake      Azure Machine Learning Reporting Services      Reporting Services      Reporting Services 2005 Administration      Reporting Services 2005 Development      Reporting Services 2008/R2 Administration      Reporting Services 2008 Development      SSRS 2012      SSRS 2014      SSRS 2016 Programming      Connecting      General      SMO/RMO/DMO      XML      Service Broker      Powershell      Testing      TFS/Data Dude/DBPro      SSDT      Continuous Integration, Deployment, and Delivery      R Services and R Language Data Warehousing      Integration Services      Strategies and Ideas      Analysis Services      Data Transformation Services (DTS)      Performance Point      Data Mining      PowerPivot      R language      Machine Learning Database Design      Disaster Recovery      Design Ideas and Questions      Relational Theory      Hardware      Virtualization      Security and Auditing SQLServerCentral.com      Anything that is NOT about SQL!      Contests!      Editorials      SQLServerCentral.com Announcements      SQLServerCentral.com Website Issues      Suggestions      Tag Issues with Content      Podcast Feedback      SQLServerCentral.com Test Forum      Articles Requested SQL Server 2005      Administering      Backups      Business Intelligence      CLR Integration and Programming.      Data Corruption      Development      Working with Oracle      SQL Server 2005 Compact Edition      SQL Server 2005 General Discussion      SQL Server 2005 Security      SQL Server 2005 Strategies      SS2K5 Replication      SQL Server Express      SQL Server 2005 Performance Tuning      SQL Server 2005 Integration Services      T-SQL (SS2K5)      SQL Server Newbies SQL Server 7,2000      Administration      Backups      Data Corruption      General      Globalization      In The Enterprise      Working with Oracle      Security      Strategies      SQL Server Newbies      Service Packs      SQL Server CE      Performance Tuning      Replication      Sarbanes-Oxley      T-SQL      SQL Server Agent SQL Server and other platforms      MySQL      Oracle      PostgreSQL      DB2      SQL Server and Sharepoint Older Versions of SQL (v6.5, v6.0, v4.2)      Older Versions of SQL (v6.5, v6.0, v4.2) Career      Certification      Employers and Employees      Events      Job Postings      Resumes and Job Hunters      Presentations and Speaking      Retired Members Testing Center      SQL Server Security Skills      Question of the Day (QOD) Microsoft Access      Microsoft Access Products and Books      Third Party Products         SQLCentric         Extreme Technologies.         Innovartis         Embarcadero         SQL Sentry         Sonasoft         Golden Gate Software         Idera         Lumigent         Red Gate Software         ApexSQL         Quest Software         CA      Discussions about Books         Discuss Programming Books          Discuss XML Books          Discuss T-SQL Books          Discuss Data Warehousing Books          Discuss DTS Books          Discuss SQL Server 2000 Books         Discuss SQL Server 7.0 Books Notification Services      Administration Article Discussions Future Versions      SQL 12

## Search

 Copyright © 2002-2018 Redgate. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.