Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to get all 7 days Name in One table.


How to get all 7 days Name in One table.

Author
Message
zoom19
zoom19
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
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???
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16624 Visits: 17024
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)
dan-572483
dan-572483
SSChasing Mays
SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)

Group: General Forum Members
Points: 645 Visits: 1958
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.
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14967 Visits: 38985
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!

zoom19
zoom19
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
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.
bvenkannababu
bvenkannababu
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 136
Hi try ISnull function in sql Query isnull(columnname,'0') something like this
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45173 Visits: 39925
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search