SQL Clone
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-Enthusiastic
SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)

Group: General Forum Members
Points: 116 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
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: General Forum Members
Points: 61367 Visits: 17954
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.

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
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3507 Visits: 2000
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
SSC Guru
SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)

Group: General Forum Members
Points: 70256 Visits: 40923
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-Enthusiastic
SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)

Group: General Forum Members
Points: 116 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
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

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

Group: General Forum Members
Points: 211030 Visits: 41974
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

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