November 18, 2010 at 7:19 am
Hello,
Can anyone point me in the right direction? I'm after a 'self contained' select statement that will simply return a list of the last 30 days. It cannot use CTEs nor can it refer to a numbers table or the like.
thanks,
Dom
November 18, 2010 at 7:22 am
Dom Horton (11/18/2010)
Hello,Can anyone point me in the right direction? I'm after a 'self contained' select statement that will simply return a list of the last 30 days. It cannot use CTEs nor can it refer to a numbers table or the like.
thanks,
Dom
Why the restrictions, Dom?
If it can't refer to a numbers table (or the like), can it use a system table as a source of rows?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
November 18, 2010 at 7:43 am
Thanks for the reply Chris,
I'm actually trying to write a 'Sql command' in Crystal Reports. I've a report that lists application usage over time....I'm trying to find usage or lack of over the last 30 days....whereby a join the '30 day list' to the usage (I may even be not going about this effectively!...open to suggestions).
I can't create temp tables, but could possibly refer to an existing table in the db to use as a source of rows.
thanks,
dom
November 18, 2010 at 7:54 am
Hi Dom
I did a lot of Crystal reports from 3 to 2 years ago. Stored procedures were far away the best data source then, but I can't even remember the Crystal version. Can you use a sproc, and integrate this 30-day rowsource into it? If not, it's easy enough to do it using system tables:
SELECT Reportdate = DATEADD(dd, -d.rn, DATEADD(dd, DATEDIFF(dd,0,getdate()), 0))
FROM (
SELECT TOP 30 rn = ROW_NUMBER() OVER (ORDER BY Name) -1 -- starts at rn = 0 i.e. today
FROM master.dbo.syscolumns
) d
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
November 18, 2010 at 8:02 am
That's great Chris...very useful (and can be adapted for various uses)
Unfortunately I'm not allowed to create sprocs! Just writing 'sql commands' is rocket science to those who require the reports!
Once again thanks for the swift and prompt response.
cheers,
dom
November 18, 2010 at 8:05 am
Dom - Can you use a table variable?
DECLARE @I INT
SET @I = 0
DECLARE @T AS TABLE(d DATETIME)
WHILE @I < 30
BEGIN
INSERT INTO @T
SELECT DATEADD (dd , -@I, GETDATE())
SET @I = @I + 1
END
--To test results
SELECT D FROM @T ORDER BY D
November 18, 2010 at 10:01 am
Hi Ron,
Thanks for the reply but Crystal Reports XI doesn't support table variables.
For info/closure I've rejigged the code supplied by Chris to work with my clients SQL Server 2k db.
SELECT DATEADD(dd, -d.rn, DATEADD(dd, DATEDIFF(dd,0,GETDATE()), 0)) AS ReportDate
FROM (
SELECT TOP 30 number rn FROM master.dbo.spt_values
WHERE NAME is null
) d
cheers,
dom
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply