Setting a Default SSRS parameter - sql-server

Default SSRS Settings. I have several parameters, some are interdependent on each other.
For example:
There is season parameter, final, and then i have a user parameter.
The final field is Yes or No.
The user output depends on the year
If my year is 2019, i will only see the users that are active for that year.
select distinct user where year = #year
I need to set defaults:
season needs to be max(year) how do i select max possible value?
final, options are yes or no, on the server we select 'N'
to be default. (see graphic below)
But with the users we want to select 'Select All' as a default based on the year. I don't know how to get the default to be a 'select all' possible options based on the query/year.

You can not achieve this from report manager unless it is defined in the report.
E.g. for season, you need to define the default from a dataset, and create a dataset with the SQL to SELECT MAX(year).

Related

Creating SQL views in VBA

I am trying to create a view on a table called petients in my database. The table has five columns. One of them is the column which I want to keep patient admitted date. It data type is datetime so I want to create a query that filters the data in this table based on current date. For example I want create a view that shows only details of petients who have been recorded on the current day.
Here is my code:
CREATE VIEW [dbo].[recent petients]
AS
SELECT petient_id, name, age, contact
FROM [petients]
WHERE [date] = 'date.Today'
I am getting an error saying that failed to convert date to string. Can you help me to solve it, or where is my code wrong?
Your code looks like SQL Server code. If so, I would recommend:
SELECT petient_id, name, age, contact
FROM [patients]
WHERE [date] = CONVERT(date, GETDATE());
As a note: This version is much better than DATEDIFF() because it allows the use of an index on patient([date]).
If the "date" column has a time component, you can use:
WHERE CONVERT(date, [date]) = CONVERT(date, GETDATE())
Note that this is also index-safe in SQL Server.
I'm assuming you are using Transact-SQL from Microsoft SQL Server, but you should specify the sql dialect you are using.
Since the datetime field type generally includes also a time, it is better to use the DATEDIFF function: https://learn.microsoft.com/it-it/sql/t-sql/functions/datediff-transact-sql?view=sql-server-ver15
In your case, to consider only the record where date=today, the difference in days must be zero:
--SQL QUERY
WHERE DATEDIFF(day, GETDATE(), [date]) = 0
day identifies the element you want to consider the difference. A list of names or abbreviations can be found in the link
GETDATE() returns now datetime
2nd and 3rd arguments are the dates you want to make the difference between

SSRS: Two cascading date parameters do not refresh

I have an SSRS 2014 report with three parameters: #Period (text), #FromDate (date), and #ToDate (date). They work together by first selecting a value from the Period dropdown list (January, February, March etc...). Depending on what you period you choose, the #FromDate and #ToDate parameters change accordingly to reflect your choice. This works well, but the problem arises when you select a new period after having already selected one, as the date parameters do not refresh.
I have been looking at some suggestions and workarounds, but I have yet to find one that deals with two dependent date parameters. Any suggestions?
The Date parameters once generated cannot be changed. It does not have the cascading facility and according to Microsoft, it's by design (it's how they wanted it to behave):
Follow this link please
As already stated, parameters which have no available values/default values from a cascading query won't refresh their default values.
Workaround: Create a one row dataset which calculates your DateFrom and DateTo dates, each in a separate column and depending on your #Period parameter, and assign the dataset to the available and default values of both your parameters. Disadvantage: you won't be able to edit the values when running the report, since the fields are populated by a dataset.
This is working as intended, Microsoft doesn't want cascading date parameters to refresh if you change the parameter they are dependent on. However it's possible to get around it.
It requires two datasets for the two data parameters which return one row with the required data dependent on the #Period parameter e.g.
DECLARE #Dates as TABLE ([Period] INT, [Date] SMALLDATETIME)
INSERT INTO #Dates VALUES
(1,DATEADD(s, 86340, DATEADD(dd,-1,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0))) )
,(2,DATEADD(DAY , 7-DATEPART(WEEKDAY,GETDATE()),DATEADD(MINUTE,- 1,DATEADD(DAY,0,DATEADD(day,DATEDIFF(day,0,GETDATE())+1,0)))) )
,(3,DATEADD(MINUTE,- 1,DATEADD(DAY,0,DATEADD(day,DATEDIFF(day,0,GETDATE()),0))))
SELECT
[Period]
,[Date]
FROM
#Dates
WHERE
[Period] = #Period
Set the available values of the #Period parameter too match and set the default value of the data parameters to their matching datasets.
Now when you change the #Period parameter it forces the date datasets to be be rerun and your date parameters will default to the new result.

SSRS load report even if cascading parameter is empty

I have cascading parameters like: Year > Company After choosing Year value, Company parameters are refreshed.
So for example If I choose 2016, but at this year no data, Company parameter is empty (no values) and report says that parameter can't be blank (I've checked "Allow blank values """, but It not working with multivalued parameters) See image below:
How can I load report with message like: "Sorry, there is no data"? Or provide any default value for Company parameter if there is no other values? Have you any ideas?
One solution is to make your Year parameter data-driven, from the same table as your companies. Make a new dataset like the one below, and use it as the available values for your Year parameter.
SELECT DISTINCT year
FROM companies
WHERE report_relevant = 'Yes'
ORDER BY 1
This limits the users to only pick years where valid companies exist. As soon as a 2016 company is added to your data, the report will automatically include 2016 as a Year parameter option.
You could use a UNION to add an extra row of data to your parameter, but Count() your valid companies so the extra row is only included when no other companies would be.
SELECT company_id, company_name
FROM companies
WHERE year = #Year
UNION
SELECT '-1', 'No companies in ' + #Year
WHERE (SELECT Count(company_id) FROM companies WHERE YEAR = #Year) = 0

SSRS BIDS Reporting Group By Header

Does anyone know how to make it so that when there are multiple records for a month (See image) there is only one line of data rather than splitting it into 4 boxes.
For example see June, I want this to have one box for RFC days and one box for Project days.
I have grouped by so far:
Row Groups - Service, MonthName
Column Groups - classification
You could do it by using grouping and sum the values at the group level and hide the detail line.
However, the easiest thing to do is do it in SQL using nested queries, like so (you probably also want to order by the month number rather than name, as shown below):
SELECT Service, Month(DateField) AS MonthNumber,
MAX(DatePart(Month, DateField)) AS MonthName,
SUM(ProjectDays) AS ProjectDays, SUM(RFCDays) AS RFCDays
FROM (
SELECT Service, DateField, Days AS ProjectDays, 0 AS RFCDays
FROM Project
UNION ALL
SELECT Service, DateField, 0 AS ProjectDays, Days AS RFCDays
FROM RFC
)
GROUP BY Service, Month(DateField)
ORDER BY Service, Month(DateField)
I found that I had to GROUP BY my MONTH field so I dragged this into the group by row groups, right clicked on it from here and in Group Properties added a group expression.

Return a Table of Payroll Dates from a SQL Stored Procedure

I'm working with SQL Server Reporting Services 2008, which is somewhat new to me, as most of my experience is with LAMP development. In addition, moving most of the logic to SQL as stored procedures is something I'm not very familiar with, but would like to do. Any help or direction would be greatly appreciated.
I need a list of acceptable payroll dates in the form of a table to use as the allowed values for a report parameter. Ideally, the person will be able to select this payroll date from the drop-down provided by the report parameter, which will then be used in the dataset to pull data from a table. I would like the logic to be stored on the SQL server if possible, as this is something that will most likely be used on a few other reports.
The logic to create the list of dates is rather simple. It starts with the oldest payroll date that is need by the system (sometime in 2007) and simply goes every two weeks from there. The procedure or function should return a table that contains all these dates up to and including the nearest upcoming payroll date.
It seems to me that the way to go about this would be a procedure or function that creates a temporary table, adds to it the list of dates, and then returns this table so that the report parameter can read it. Is this an acceptable way to go about it?
Any ideas, examples, or thoughts would be greatly appreciated.
I would use a CTE something like this one:
;WITH PayPeriod AS (
SELECT #DateIn2007 AS p UNION ALL
SELECT DATEADD(dd, 14, p) as P FROM PayPeriod WHERE p < GetDate() )
SELECT p FROM PayPeriod
OPTION ( MAXRECURSION 500 )
The MAXRECURSION and/or where parameter limits the number of dates it will generate.
You can use a parameter to figure out the correct limit to get the correct last date still, of course.
try something like this:
;with AllDates AS
(
SELECT CONVERT(datetime,'1/1/2007') AS DateOf
UNION ALL
SELECT DateOf+14
FROM AllDates
WHERE DateOf<GETDATE()+14
)
SELECT * FROM AllDates
OPTION (MAXRECURSION 500)
you can put this in a view or function.
However, I would suggest that instead of presenting a select box of this many values, why not just have two text box fields: start date and end date and default them to reasonable values, just my 2 cents

Resources