Generate Sequence Day and number Using Type Expression

  • Hi i am trying to generate record like this

    Day Week

    11

    12

    13

    14

    15

    16

    17

    21

    22

    23

    24

    25

    26

    27

    31

    32

    33

    34

    35

    36

    37

    41

    42

    43

    44

    45

    46

    47

    For this i write this expression

    ;with WeekTable(i,Week,Day,NoWeeK,NoDay,Total) as (

    select 1, 1, 1, 4, 7, 28

    union all

    select i+1,

    case when Day+1 > NoDay then Week+1 else Week end,

    case when Day+1 > NoDay then Week+1 else Day+1 end,

    NoWeeK,NoDay,Total

    from WeekTable

    where i<total

    )

    SELECT Week,Day FROM WeekTable

    But not able to find desire result ,

    Please help me on this , or if someone have better suggestion , so please suggest me .

    Thanks

  • I done

    ;with WeekTable(i,Week,Day,NoWeeK,NoDay,Total) as (

    select 1, 1, 1, 4, 7, 28

    union all

    select i+1,

    case when Day+1 > NoDay then Week+1 else Week end,

    case when Day+1 > NoDay then 1 else Day+1 end,

    NoWeeK,NoDay,Total

    from WeekTable

    where i<total

    )

    SELECT Week,Day FROM WeekTable

    This will give my result , if some one have better option please post

  • How about something like this?

    SELECT *

    FROM (VALUES (1),(2),(3),(4)) a([Day])

    CROSS JOIN (VALUES (1),(2),(3),(4),(5),(6),(7)) b ([Week])


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 3 posts - 1 through 2 (of 2 total)

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