How to get Weeks in a month

  • Hi,

    I want to display the weeks for a particular month.

    i.e., i will pass @year=2008 and @month=5

    i want output like Week1, week2, week3, week4, week5 like that.

    Example:

    1st query:

    select month, count(*) from

    sample data:

    month Count

    ---------------------

    1 45

    2 64

    3 19

    4 27

    5 98

    I need the 2 nd query:

    from the above query i will pass the month number, and i need output like the below:

    Sample::::

    week Count

    -------------------

    week1 10

    week2 20

    week3 40

    week4 28

    If any one knows the solution for the above query, please help me.

    Thanks in Advance.

    Regards

    Venki

  • I'm not sure I fully understand your problem. Can you post DDL for your table and the sample data you would expect to generate the results you've already posted?

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • You need to define what a "week" is before anyone can help... Do weeks start on a Monday or ??? Are you talking about ISO week #'s or SQL Server Week numbers?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • To add to the list of questions:

    ..are we talking full weeks (fully circumscribed within the month)? weeks that START within a month/weeks that END within a month?

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • Hi,

    In my requirement week starts with monday and ends with sunday.

    I will pass year for ex:2008

    and month =May

    expected result:

    weeks count

    ---------------

    week1 4

    week2 90

    week3 8

    etc..

    corresponding month weeks only

    DatePart function will returns weeks from 1 to 53.

    Can u please help me.

  • Alright, Venki... you've given us just about nothing to go on... no table Create statement, no data, no column names... and your sample query of...

    1st query:

    select month, count(*) from

    sample data:

    month Count

    ---------------------

    1 45

    2 64

    3 19

    4 27

    5 98

    ... really doesn't tell us anything about the table you have. Do you actually have a table with a "Month" column in it? Does it have a "Year" column in it? If so, what is their datatype? Or, do you have a column of the DATETIME datatype?

    Because you've not adequately defined the problem, you're not getting the help you need or want because folks just don't want to guess about your requirements nor do they want to take the time to setup some test data to ensure that their answer will actually work for you.

    So, what you need to do is you need to read and follow the URL in my signature. Make the sample data "self loading" like in the article. If the data is larger than 20 or 30 rows, attach it as a file in the format like in the article.

    This is not a difficult problem, but you haven't given us enough info so solve it correctly.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • Well said.

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

Viewing 7 posts - 1 through 6 (of 6 total)

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