views:

63

answers:

1
+2  Q: 

TSQL Set theory

Here is the question

You are to label the dice sides with numbers. Each dice has 6 sides. You have two dice. You must label so that you can display (not sum or product) the numbers 0 to 31. Complete output:

00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

I tried

(select
       0 as dice1 union
       select 1 union
       select 2 union
       select 3 union
       select 4 union
       select 5 )

    join

(select
       0 as dice2 union
       select 1 union
       select 2 union
       select 3 union
       select 4 union
       select 5 )

I don't know how to process it further.Help is appreciated.

+1  A: 

As Martin mentioned in his comment, the key to solving this problem is to use the upside down 6 for a 9. See: solution: calendar cubes

As for a programmatic T-SQL solution, perhaps:

declare @Dice1 table (
    side int
)

insert into @Dice1
    (side)
    select 0 union all
    select 1 union all
    select 2 union all
    select 3 union all
    select 4 union all
    select 5

declare @Dice2 table (
    side int
)

insert into @Dice2
    (side)
    select 0 union all
    select 1 union all
    select 2 union all
    select 6 union all
    select 7 union all
    select 8 union all
    select 9 /* Upside down 6 */


select CAST(d1.side as CHAR(1)) + CAST(d2.side as CHAR(1)) as MyDate
    from @Dice1 d1
        cross join @Dice2 d2
    where d1.side * 10 + d2.side <= 31
union
select CAST(d2.side as CHAR(1)) + CAST(d1.side as CHAR(1)) as MyDate
    from @Dice1 d1
        cross join @Dice2 d2
    where d2.side * 10 + d1.side <= 31
order by MyDate
Joe Stefanelli
Very elegant solution. It's `0 1 2 3 4 5` and `0 1 2 7 8 6`.
Denis Valeev
Why `MyDate`? :)
Denis Valeev
@Denis: Years and years of drinking Microsoft Kool-Aid: My Documents, My Downloads, ..., MyDate. :-)
Joe Stefanelli
Wow ,I got the answer
amit
@Joe Stefanelli, haha, but those are numbers, not dates!
Denis Valeev