How to get all 7 days Name in One table.

  • I am working on one report which showing all days name as column.but Now I am getting some bugs.

    If for particular day data is not available on table then that days name are missing on report and which is not good.

    If table doesn't have that day data even we want to show that day column as null.

    can any one help me out with this???

  • You need to use a calendar table or a tally table as the main table for your query. You need to have a row for the date when there are no details. Do a quick search on this site for calendar tables.

    If you need specific coding help please take a few minutes and read the article at the first link in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Are you using SSRS or Crystal Reports? I had this issue once with Crystal. First I used data grouping to summarize records for each day of the week, but if there were no records for a particular day, that day was missing - but the client wanted to see all 7 days with zero total if the day had no records.

    What I ended up doing was a separate formula for each day of the week that counts records for that day, then put all seven formulas in one report section. It was more work, but it did show all seven days and zeros for days with no records.

    I'm not very familiar with SSRS, so if that's what you're using I hope this is helpful.

  • you have to select form a CalendarTable, which would contain all possible dates, and left join it to your data that has the rest of the data.

    you can generate a Calendar on the fly, or create a permanent one , because it's very useful for reports like this.

    one example:

    SET DATEFORMAT MDY

    CREATE TABLE #myBalances (

    [TranDate] DATETIME,

    [TransactionAmount] money,

    [Balance] money)

    INSERT INTO #myBalances VALUES ('12/1/2008',100,100)

    INSERT INTO #myBalances VALUES ('12/3/2008',-60,40)

    INSERT INTO #myBalances VALUES ('12/4/2008',10,50)

    DECLARE @StartDate DATETIME, @EndDate DATETIME

    SET @StartDate = '11/15/2008'

    SET @EndDate = '12/6/2008'

    SELECT TallyCalendar.TheDate AS TranDate, ISNULL(b.TransactionAmount, 0) AS TransactionAmount, ISNULL(x.Balance ,0)

    FROM (

    SELECT dateadd( dd,-3650 + RW ,DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)) As TheDate

    FROM (

    SELECT TOP 7300

    row_number() OVER (ORDER BY sc1.id) AS RW

    FROM Master.dbo.SysColumns sc1

    CROSS JOIN Master.dbo.SysColumns sc2

    ) X

    )TallyCalendar

    LEFT JOIN #myBalances b ON b.TranDate = DATEADD(DD, 0, TallyCalendar.TheDate)

    LEFT JOIN #myBalances x ON x.TranDate = (SELECT MAX(TranDate) AS TranDate FROM #myBalances WHERE TranDate <= TallyCalendar.TheDate)

    WHERE TallyCalendar.TheDate BETWEEN @StartDate AND @EndDate

    DROP TABLE #myBalances

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thanks to everyone for giving me better suggestion and yes I am using SSRS.

    I will try all your possible suggestion and If it is not working out or have problem then get back again.

  • Hi try ISnull function in sql Query isnull(columnname,'0') something like this

  • bvenkannababu (4/4/2013)


    Hi try ISnull function in sql Query isnull(columnname,'0') something like this

    That won't work for missing days unless you have a reference (like a Calendar table or Tally Table driven result set) to be able to join to to establish what the missing days actually are.

    --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)

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

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