Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to get all 7 days Name in One table. Expand / Collapse
Author
Message
Posted Thursday, April 4, 2013 9:46 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, January 25, 2016 10:47 AM
Points: 26, Visits: 302
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???
Post #1438874
Posted Thursday, April 4, 2013 9:52 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: 2 days ago @ 1:45 PM
Points: 15,549, Visits: 15,699
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1438880
Posted Thursday, April 4, 2013 9:55 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: 2 days ago @ 3:21 PM
Points: 612, Visits: 1,898
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.
Post #1438881
Posted Thursday, April 4, 2013 10:02 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 6:20 PM
Points: 14,184, Visits: 37,070
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!
Post #1438885
Posted Thursday, April 4, 2013 11:54 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, January 25, 2016 10:47 AM
Points: 26, Visits: 302
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.
Post #1438947
Posted Thursday, April 4, 2013 11:47 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, January 21, 2016 2:19 AM
Points: 2, Visits: 136
Hi try ISnull function in sql Query isnull(columnname,'0') something like this
Post #1439103
Posted Friday, April 5, 2013 7:26 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 8:08 PM
Points: 40,374, Visits: 37,575
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."

Helpful Links:
How to post code problems
How to post performance problems
Post #1439204
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse