Avatar of Mark Wilson
Mark Wilson

asked on 

Date variables (last months data)

I am using CR2008

I am connecting to an excel spread sheet

I am using a SQL command as below

select [Assessee Name:], count([Unique Ref]) as 'Total','No of Assessments' as 'Type'
from [Temp Database$A1:AB10000]
where [Skill Category] = 'Total' and month([Date & Time]) = 8 and year([Date & Time]) = 2013
group by [Assessee Name:]

The problem is I want to look at the last months worth of data. At the moment I am using
month([Date & Time]) = 8 and year([Date & Time]) = 2013. This is not good as I would have to go in on the first day of every month and change the month number.

Is there anyway I could add date variables or dates to look back at the last month without me having to change anything?

Thanks
Crystal ReportsSQL

Avatar of undefined
Last Comment
PortletPaul
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

>go in on the first day of every month and change the month number.
Please elaborate on what this means.
Avatar of PortletPaul
PortletPaul
Flag of Australia image

This (by: James0628) comes from a CR related question I participated in  ID: 39296140
To use parameters in a CR Command:

 Create the parameters in the Command edit window.  It has to be there, as opposed to the main CR editor window.  Once the parameters are created, they can be edited from the main window.

 Then, basically, insert the parameter name where you need it in the SQL code.  The simplest thing is to position the cursor where you want the parameter and then double-click the parameter name in the parameter list on the right.  When CR runs the query, it will simply replace the parameter name with its value.  If the parameter value could be a string, put the parameter name in single quotes.  For a date or datetime, I don't think you'll need them.
So you will then insert those params where you have 8 and 2013 below
SELECT
        [Assessee Name:]
      , COUNT([Unique Ref]) AS 'Total'
      , 'No of Assessments' AS 'Type'
FROM [Temp Database$A1:AB10000]
WHERE [Skill Category] = 'Total'
        AND MONTH([Date & Time]) = 8 -- param here
        AND YEAR([Date & Time]) = 2013 -- and another param here
GROUP BY [Assessee Name:]

Open in new window

Avatar of PortletPaul
PortletPaul
Flag of Australia image

mmmmm, OR you are asking for this to be "automatic" based on the server's date

maybe, it's this you want:
SELECT
        [Assessee Name:]
      , COUNT([Unique Ref]) AS 'Total'
      , 'No of Assessments' AS 'Type'
FROM [Temp Database$A1:AB10000]
WHERE [Skill Category] = 'Total'
        AND MONTH([Date & Time]) = MONTH(getdate()) -- "auto month"
        AND YEAR([Date & Time]) = YEAR(getdate()) -- "auto year"
GROUP BY [Assessee Name:]

Open in new window

SOLUTION
Avatar of Mark Wilson
Mark Wilson

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Mike McCracken
Mike McCracken

getdate is an MS SQL function.  Since you are connecting to an Excel spreadsheet you need to use Excel functions

WHERE [Skill Category] = 'Total'
        AND MONTH([Date & Time]) = MONTH(Now()) -- "auto month"
        AND YEAR([Date & Time]) = YEAR(Now()) -- "auto year"

That will get you the current month data.

If you need last month's data it is more complex because you need to handle the year crossing

mlmcc
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Removed Topic: MS SQL Server

PortletPaul
Topic Advisor
SQL
SQL

SQL (Structured Query Language) is designed to be used in conjunction with relational database products as of a means of working with sets of data. SQL consists of data definition, data manipulation, and procedural elements. Its scope includes data insert, query, update and delete, schema creation and modification, and data access control.

61K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo