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

  • 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) - (_ _ _ _) = 33333

    As 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 = 33333

    order by 1,2

  • 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 = 33333

    order 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 = 33333

    order 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[/url]

  • 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 = 33333

    order 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[/url]

  • 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, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • 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[/url]

  • 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
  • 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 .... πŸ˜€

    I don't think it can get any better.

  • 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[/url]

  • 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, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Shaun-884394 (9/12/2012)


    ...

    GSquared i love your signature ROFL,LOL,ETC .... πŸ˜€

    I 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, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • GSquared (9/12/2012)


    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

    Table constructor with NOT IN yields a very "clean" plan.

    ;WITH a (num) AS (SELECT * FROM (VALUES ('1'),('2'),('5'),('7'),('8'),('9')) a (num)),

    b (num1,num2) AS (

    SELECT

    '4' + a.num + b.num + c.num + '6',

    d.num + e.num + f.num + '3'

    FROM a

    CROSS join a as b

    CROSS join a as c

    CROSS join a as d

    CROSS join a as e

    CROSS join a as f

    WHERE 1 = 1

    AND b.num NOT IN (a.num)

    AND c.num NOT IN (a.num, b.num)

    AND d.num NOT IN (a.num, b.num, c.num)

    AND e.num NOT IN (a.num, b.num, c.num, d.num)

    AND f.num NOT IN (a.num, b.num, c.num, d.num, e.num)

    )

    SELECT num1, num2

    FROM b

    WHERE CAST(num1 AS INT) - CAST(num2 AS INT) = 33333

    β€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • I've tried all three methods, INNER JOINs, CROSS APPLY and CROSS JOIN with WHERE: all produce very similar query plans with use of Nested Loops (Inner Join).

    Cannot see much difference at all.

    _____________________________________________
    "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[/url]

  • Eugene Elutin (9/12/2012)


    I've tried all three methods, INNER JOINs, CROSS APPLY and CROSS JOIN with WHERE: all produce very similar query plans with use of Nested Loops (Inner Join).

    Cannot see much difference at all.

    Try this one.

    β€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Try performance by time (miliseconds or nanoseconds)

    DECLARE @Ddatetime2

    SET @D = SYSDATETIME()

    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 = 33333

    order by 1,2

    SELECT DATEDIFF(ms, @D, SYSDATETIME())

    SET @D = SYSDATETIME()

    WITH Numbers(n) AS(

    SELECT '1' UNION ALL

    SELECT '2' UNION ALL

    SELECT '3' UNION ALL

    SELECT '5' UNION ALL

    SELECT '7' UNION ALL

    SELECT '8' UNION ALL

    SELECT '9' ),

    Numbers2 AS(

    SELECT CAST( '4' + n1.n + n2.n + n3.n + '6' AS int) num1,

    CAST( n4.n + n5.n + n6.n + n7.n AS int) num2

    FROM Numbers n1

    JOINNumbers n2 ON n1.n <> n2.n

    JOINNumbers n3 ON n3.n NOT IN(n1.n, n2.n)

    JOINNumbers n4 ON n4.n NOT IN(n1.n, n2.n, n3.n)

    JOINNumbers n5 ON n5.n NOT IN(n1.n, n2.n, n3.n, n4.n)

    JOINNumbers n6 ON n6.n NOT IN(n1.n, n2.n, n3.n, n4.n, n5.n)

    JOINNumbers n7 ON n7.n NOT IN(n1.n, n2.n, n3.n, n4.n, n5.n, n6.n)

    )

    SELECT num1, num2

    FROM Numbers2

    WHERE num1 - num2 = 33333;

    SELECT DATEDIFF(ms, @D, SYSDATETIME())

    SET @D = SYSDATETIME()

    WITH Numbers(n) AS(

    SELECT '1' UNION ALL

    SELECT '2' UNION ALL

    SELECT '3' UNION ALL

    SELECT '5' UNION ALL

    SELECT '7' UNION ALL

    SELECT '8' UNION ALL

    SELECT '9' ),

    Numbers2 AS(

    SELECT CAST( '4' + n1.n + n2.n + n3.n + '6' AS int) num1,

    CAST( n4.n + n5.n + n6.n + n7.n AS int) num2

    FROM Numbers n1

    JOINNumbers n2 ON n1.n <> n2.n

    JOINNumbers n3 ON n1.n <> n3.n AND n2.n <> n3.n

    JOINNumbers n4 ON n1.n <> n4.n AND n2.n <> n4.n AND n3.n <> n4.n

    JOINNumbers n5 ON n1.n <> n5.n AND n2.n <> n5.n AND n3.n <> n5.n AND n4.n <> n5.n

    JOINNumbers n6 ON n1.n <> n6.n AND n2.n <> n6.n AND n3.n <> n6.n AND n4.n <> n6.n AND n5.n <> n6.n

    JOINNumbers n7 ON n1.n <> n7.n AND n2.n <> n7.n AND n3.n <> n7.n AND n4.n <> n7.n AND n5.n <> n7.n AND n6.n <> n7.n

    )

    SELECT num1, num2

    FROM Numbers2

    WHERE num1 - num2 = 33333;

    SELECT DATEDIFF(ms, @D, SYSDATETIME())

    SET @D = SYSDATETIME()

    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

    SELECT DATEDIFF(ms, @D, SYSDATETIME())

    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
  • Luis Cazares (9/12/2012)


    Try performance by time (miliseconds or nanoseconds)

    ...

    And the winner is the one you didn't include in your test:

    DECLARE @Ddatetime2

    SET @D = SYSDATETIME()

    ;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

    SELECT DATEDIFF(ms, @D, SYSDATETIME())

    _____________________________________________
    "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[/url]

Viewing 15 posts - 1 through 15 (of 40 total)

You must be logged in to reply to this topic. Login to reply