I am trying to run a query within Business Objects Universe Designer and I need help with the 'Where' clause.
I want to search for all records that have a 4 digit year (the DB column is in YYYY) less than or equal to 3 years from the current year. So if the year is 2014, I would like to search for every record with a year less than or equal to 2011.
Here is my current where clause:
dbo.DB_TABLE.CATEGORY = 'Actual' and dbo.DB_TABLE.YR <= (convert (SMALLDATETIME, {fn CURDATE()})-3)
Under the 'Date' function, Universe Designer only has: convert (SMALLDATETIME, {fn CURDATE()})
Thanks in advance!!!
Since yr is just a number, you only need to extract the year from the current date:
dbo.DB_TABLE.YR <= datepart(year,{fn curdate()})-3
When writing a SQL statement in the SELECT or WHERE boxes in Designer, you are not limited to only using the functions available in the list box. Any SQL that is valid for the database can be used. The list box is only a helper, and lists commonly-used functions and statements.
Related
Run a query that returns the number of trouble tickets in the VTM024… table, but only the ones created in the year 2015. Note: when setting the where criteria, use a function to extract the Year portion from the timestamp field.
Using the Year function in the first answer, write SQL that shows the number of trouble tickets created in each year dynamically, and do this using a “GROUP BY” clause in the SQL. It should return something like:
2012 10
2013 54
2014 111
etc
I was able to answer first question myself:
SELECT COUNT(CREATE_TIME) FROM TRACS_DW1.VTM024TROUBLE_TKT
WHERE EXTRACT (YEAR FROM CREATE_TIME) = 2015
I need help with 2nd question. I cant get groupby and extract functions together.
i have added a picture of my table.
Add EXTRACT (YEAR FROM CREATE_TIME) as year to your select query and group by the year field.
I'm currently struggling with how to produce a Fiscal Year parameter, so its values start with Current year, but also contain records from all previous years, and then have all following values be each following year.
So the Fiscal Year would look roughly like the following
Current (I would want to include all previous Fiscal years in this as well),
F18,
F19,
F20,
etc. etc.
As of right now, I have all fiscal years on the drop down, starting with the earliest fiscal year that exists in the database using the following query to populate the parameter dropdown.
SELECT DISTINCT FiscalConsideration FROM dbo.Currency
Which would look like this
F12,
F14,
F15,
F16,
F17,
F18,
etc. etc.
Then I filter the results based on the selected Fiscal year.
Question 1
Is there a way where I might be able to do this, have all records from current fiscal year and prior on one value and all individual fiscal years after?
Question 2
I have also discovered that I can't have this be multi-select AND accept NULL's as well. Might I be able to convert nulls to a custom string or something and have that be an option as well?
I ended up doing it by incorporating more of the logic I wanted into the computed column in SQL Server Management Studio with another case when for past records, and changing nulls with isnull to 'TBD'
I've tried limiting data on monthly basis in spotfire and it's working fine.
Now I'm trying to do like getting the records from the current date to month start date.
For suppose if the current date is Sept 21, then i should get the records from Sept 21 to Sept-01(dynamically).
I have a property control to input the number of months.
The easiest way to do this is with Month and Year. For example, in your visualization:
Right Click > Properties > Data > Limit Data Using Expressions (Edit)
Then, use this expression:
Month([TheDate]) = Month(DateTimeNow()) and Year([TheDate]) = Year(DateTimeNow())
This will limit the data to only those rows with the current Year/Month combination in your data column. Just replace [TheDate] with whatever your date column name is.
In other places, you can wrap this in an IF statement if you'd like. It's redundant in this case, but sometimes helps with readability.
IF(Month([TheDate]) = Month(DateTimeNow()) and Year([TheDate]) = Year(DateTimeNow()),TRUE,FALSE)
#san - Adding to #scsimon answer. If you would like to precisely limit values between 1st of the current month to current date, you could add the below expression to 'Limit data using expression' section.
[Date]>=date(1&'-'&Month(DateTimeNow())&'-'&year(DateTimeNow())) and [Date]<=DateTimeNow()
I've got a SQL statement (SQL Server Management Studio) that I'm passing data into a where-statement into via a dashboard software. The users can select the year (2013 or now 2014) and also the month (which gets passes as a numeric value - so December = 12). I need to adjust the statement to where I get the last 3 months from the year/month they select. Before, b/c the SQL statement was only dealing with 2013 data, it was just the following:
YEAR(Main.ActivityDate) = '#Request.parmYear~'
AND (Month(Main.ActivityDate) Between ('#Request.parmMonth~'-2) and '#Request.parmMonth~')
Normally, parmYear = 2013 and then whatever month they select, it will grab 2 months prior through the current month.
Now, b/c it's January 2014, I need to to grab January 2014 + December 2013 + November 2013. I'm wondering how to adjust the statement to make this happen dynamically.
Thoughts?
I do not have a running SQL Server instance to test this solution but I would suggest constructing a date and using the built in functions to calculate the previous date as those already take into consideration the multiple years etc.
Declare #requestDate date = DATEFROMPARTS('#Request.parmYear', '#Request.parmMonth', 1);
...
AND Main.ActivityDate between #requestDate AND DATEADD(month, -2, #requestDate )
See this for more details.
I had a similar problem some time ago. My solution was something like this:
WHERE YEAR(Main.ActivityDate)*12 + YEAR(Month(Main.ActivityDate))
BETWEEN '#Request.parmYear~'*12+'#Request.parmMonth~'-2
AND '#Request.parmYear~'*12+'#Request.parmMonth~'
You could improve this solution sending one parameter as the result of '#Request.parmYear~'*12+'#Request.parmMonth~'.
There are two solutions for this.
Modify your current where statement and add a condition to check for this case.
Use DATEADD function. Present in comments and other answer(s).
Modifying your where to add Condition
Note: Minor error may exists since I need to check if January has month value of zero or 1.
Example:
WHERE
(
'#Request.parmMonth~'-2 < 1 AND
YEAR(Main.ActivityDate) = '#Request.parmYear~'-1 AND
Month(Main.ActivityDate) Between
(12+'#Request.parmMonth~'-2) AND 12
)
OR
(
YEAR(Main.ActivityDate) = '#Request.parmYear~'
AND (Month(Main.ActivityDate) Between
('#Request.parmMonth~'-2) and '#Request.parmMonth~'
)
I am wondering if it's possible (without actually parsing the given string) to get the actual range (in terms of days, minutes or seconds) that is specified when you have an SQL statement like
[select 'x'
from dual
where date between to_date('20111113152049')
and to_date('20120113152049')]
I am working on a query where I'm given a string in the form of
"between to_date(A) and to_date(B)"
and would like to get that value in days to compare to a policy we let the user set so they don't enter a date range longer than say a week.
Assuming you're looking for a theoretical answer (that is: don't take this into production) this could work:
Prerequistes:
have three tables: days_seq(day_seq), month_seq(mth_seq) and year_seq(yr_seq)
days has the numbers 1...31, month 1..12, years 2011....?
Use te following query (I used access because I don't have proper RDBMS available here, keep in mind that MS-ACCESS/JET is forgiving in the use of the Dateserial function, that is, it doesn't break when you ask the dateserial for february, 30th, 2012)
SELECT Max(DateSerial(
[year_seq]![yr_seq]
,[month_seq]![mth_seq]
, [days_seq]![day_seq]))
-
Min(DateSerial(
[year_seq]![yr_seq]
,[month_seq]![mth_seq]
,[days_seq]![day_seq])) AS days
FROM days_seq, month_seq, year_seq
WHERE DateSerial(
[year_seq]![yr_seq]
,[month_seq]![mth_seq]
,[days_seq]![day_seq])
BETWEEN #2012-02-1# AND #2012-02-28#
The query basically produces a carthesian product of three tables which generates all possible days in months, months in a year for as many years as you have in the years table.
Bonus:
You could off-course generate a permanent Calendar table as X-Zero suggests.
table calendar([date])
INSERT INTO calendar
SELECT DISTINCT DateSerial(
[year_seq]![yr_seq]
,[month_seq]![mth_seq]
, [days_seq]![day_seq]))
FROM days_seq, month_seq, year_seq
You still have to pick your start year and your end year wisely. According to the Maya's an enddate of december 21st, 2012 will do.