Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Group

Author  Topic 

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2007-02-13 : 17:02:14
Hello,

I have a table, DocumentsViews, with 2 columns:
DocumentId and DocumentViewDate

I need to retrieve the number of views per timeframe so that in my .NET code I can create a dataset as follows:

Period NumberOfViews
... ...

I think I am able to retrieve the number of views but of course I also need to deliver the periods so I can create a chart and specify the dates/times.

Can someone please help me out?

SELECT CASE LOWER(@TimeFrame)
WHEN 'day' THEN DAY(DocumentViewDate)
WHEN 'month' THEN MONTH(DocumentViewDate)
WHEN 'year' THEN YEAR(DocumentViewDate)
ELSE DAY(DocumentViewDate)
END,
NumberOfRecords = COUNT(*)
FROM dbo.by27_DocumentsViews
WHERE DocumentId = @DocumentId
GROUP BY CASE LOWER(@TimeFrame)
WHEN 'day' THEN DAY(DocumentViewDate)
WHEN 'month' THEN MONTH(DocumentViewDate)
WHEN 'year' THEN YEAR(DocumentViewDate)
ELSE DAY(DocumentViewDate)
END

@TimeFrame is a parameter which indicates if the grouping and counting should be by day, month or year.

Thanks,
Miguel

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-02-13 : 18:14:26
"I think I am able to retrieve the number of views but of course I also need to deliver the periods so I can create a chart and specify the dates/times."
Please explain more on this statement


KH

Go to Top of Page

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2007-02-13 : 19:56:40
Hi,

To create a chart I will need the number of views but also the time for each view.
For example, if I set the timeframe to day I will need the following info:

t Views
20/01/2007 100
21/01/2007 200
22/01/2007 300

But if I have timeframe set to month I will need:

t Views
Jan 2007 1000
Fev 2007 2000

I suppose my code delivers just something like:
Views
1000
2000

Thanks,
Miguel
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-02-13 : 20:56:31
what you want should be like this


SELECT CASE LOWER(@TimeFrame)
WHEN 'day' THEN dateadd(day, datediff(day, 0, DocumentViewDate), 0)
WHEN 'month' THEN dateadd(month, datediff(month, 0, DocumentViewDate), 0)
WHEN 'year' THEN YEAR(DocumentViewDate)
ELSE dateadd(day, datediff(day, 0, DocumentViewDate), 0)
END,
NumberOfRecords = COUNT(*)
FROM dbo.by27_DocumentsViews
WHERE DocumentId = @DocumentId
GROUP BY CASE LOWER(@TimeFrame)
WHEN 'day' THEN dateadd(day, datediff(day, 0, DocumentViewDate), 0)
WHEN 'month' THEN dateadd(month, datediff(month, 0, DocumentViewDate), 0)
WHEN 'year' THEN YEAR(DocumentViewDate)
ELSE dateadd(day, datediff(day, 0, DocumentViewDate), 0)
END



KH

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-02-13 : 20:58:27
quote:
For example, if I set the timeframe to day I will need the following info:

t Views
20/01/2007 100
21/01/2007 200
22/01/2007 300

But if I have timeframe set to month I will need:

t Views
Jan 2007 1000
Fev 2007 2000

I suppose my code delivers just something like:
Views
1000
2000



No. Actually your current code will gives
by day
20 100
21 200
22 300

by month
02 600

by year
2007 600


KH

Go to Top of Page

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2007-02-13 : 21:24:14
Thanks for your help.
Go to Top of Page
   

- Advertisement -