Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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: Friday, February 7, 2014 1:45 PM
Points: 26, Visits: 266
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


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:05 PM
Points: 13,320, Visits: 12,804
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
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Yesterday @ 11:06 AM
Points: 557, Visits: 1,648
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: Today @ 2:14 PM
Points: 12,952, Visits: 32,476
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
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: Friday, February 7, 2014 1:45 PM
Points: 26, Visits: 266
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: Monday, July 22, 2013 11:51 PM
Points: 2, Visits: 133
Hi try ISnull function in sql Query isnull(columnname,'0') something like this
Post #1439103
Posted Friday, April 5, 2013 7:26 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 2:42 PM
Points: 35,768, Visits: 32,431
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

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

Add to briefcase

Permissions Expand / Collapse