In Report Builder, I have a report with many parameters, three of the parameters are #DateFrom, #DateTo, #Check
The query from the Main dataset gives this result with 3 columns: Name, Check, ExpiryDate.
Name Check ExpiryDate
A Y 05/01/2016
B N 06/10/2017
C Y 08/15/2017
D N 08/20/2017
E Y 09/05/2017
F N 10/20/2018
I need to group the report into 3 groups based on the parameter values,
For example, the user runs the report would enter: #DateFrom = 08/1/2017 #DateTo: 08/31/2017
=> The report should look like below:
Group Current Range ExpiryDate [(ExpiryDate >= #DateFrom and ExpiryDate <=#DateTo) and Check = #Check ]
C Y 08/15/2017
D N 08/20/2017
Group Before Range ExpiryDate and Check = 'Y' [(ExpiryDate <= #DateFrom) and Check = 'Y']
A Y 05/01/2016
Group After Range ExpiryDate and Check = 'Y' [(ExpiryDate >= #DateTo) and Check = 'Y']
E Y 09/05/2017
In the tablix using the Main dataset (only one tablix in the report), there is one RowGroup named 'Details'
I read lots of arcticles and I tried many ways, but nothing worked as I could not find the right way I guess. Will you be able to help?
The easiest way to create another group is to modify the SQL and add another column with your grouping logic.
SELECT
....,
CASE
WHEN ExpiryDate BETWEEN #DateFrom AND #DateTo AND Check = #Check
THEN 'Current'
WHEN ExpiryDate <= #DateFrom AND Check = 'Y'
THEN 'Before'
WHEN ExpiryDate >= #DateTo AND Check = 'Y'
THEN 'After'
END as MyGroup,
...
FROM ...
so that you can create a parent group inside your tablix with the new custom grouping field.
OR if the SQL is very complex or you are using a shared dataset/stored procedure, then add a group to the SSRS tablix and specify a custom expression with similar logic using the SWITCH construct.
Edit, adding parameters to the SQL CASE
CASE
WHEN expirydate BETWEEN #DateFrom AND #DateTo AND [check] IN #check
THEN 'Current'
WHEN expirydate <= #FromDate AND [check] = 'Y'
THEN 'Before'
WHEN expirydate >= #FromDate and [check] = 'Y'
THEN 'After'
END as MyGroup
Note that check is only surrounded by square brackets because it's a reserved SQL keyword.
Related
I have this query, which returns me 7 records
Now if I add this line 'AND date_format (ds.date_emission,'% Y-% m-% d ') BETWEEN' 'AND' '', nothing returns me, I want that when the date range is empty I need that I also returned the 7 records
Please refer answer below.
I have added two variable replace inStartDate variable with a range start date and inEndDate with range end date.
SELECT ds.id
FROM documentos_salida ds
WHERE ds.terceros_id = 329
AND ds.estado =0
AND ds.tipo_documento ='Factura'
AND ds.saldo_pendiente_factura >0
AND (ds.fecha_vencimiento > now() or
ds.fetcha_vencimiento = NOW())
AND ds.numero_documento LIKE '%%'
AND ( CASE WHEN (inStartDate = '' AND inEndDate = '')
THEN TRUE
ELSE (date_format(ds.fetcha_emision, '%Y-%m-%d') BETWEEN inStartDate AND inStartDate)
END)
ORDER BY ds.fecha_emision DESC;
In SSRS Expression, how to count the numbers of rows that present in today or yesterday in dataset with other equivalent condition,
For example
=COUNT(IIF(
DateDiff(DateInterval.Day,Cdate("01/01/1900"),Fields!Opendate.Value) =
DateDiff(DateInterval.Day,Cdate("01/01/1900"), Now()), Fields!Opendate.Value, Nothing))
Using this expression I can check get the total count for today, it's working.
I need to add to check today date with other condition like:
If (today date and Fields!reason.Value = "Other")
It's not working when I add reason value to check :
=COUNT(IIF(
DateDiff(DateInterval.Day, Cdate("01/01/1900"), Fields!Opendate.Value) =
DateDiff(DateInterval.Day, Cdate("01/01/1900"), Now()) -1, Fields!Opendate.Value, Nothing ) **And Fields!reason.Value = "Other"**)
Please guide me
Just add it in your IIF() statement:
=COUNT(IIF(
DateDiff(DateInterval.Day, Cdate("01/01/1900"), Fields!Opendate.Value) =
DateDiff(DateInterval.Day, Cdate("01/01/1900")
And Fields!reason.Value = "Other",
Now()) -1,
Fields!Opendate.Value,
Nothing
)
)
My task is to get the records between 2fromdate and todate(given as a input parameters).
i am not able to use between operator for 2 input parameters...
My query as follows...
DELIMITER $$
CREATE DEFINER=`testrunner`#`%` PROCEDURE `usp_GetAllTranasactions`(pFromDate nvarchar(30),pToDate nvarchar(30),pstatus int)
BEGIN
select
ST.UserID,U.Username,
ST.SubscriptionID,
ST.DateOfSubscription,
SM.SubType,
SM.Details,
ST.Amount,
ST.EndDate,
ST.Status
from tr_t_subscriptiontransactions ST
Join tr_m_users U on U.UserID=ST.UserID
join tr_m_subscription SM on SM.SubscriptionID=ST.SubscriptionID
where **ST.DateOfSubscription between (pFromDate and pToDate) and ST.EndDate
between(pFromDate and pToDate) and ST.Status=pstatus;**
END if;
END
here i don't know how to use between parameters..plz help me..i want to retrive record between fromdate and todate..hope u understand..
Let us assume you want all transactions for the month of June 2014
In your user interface the parameter values are:
from_date = 2014-06-01
to_date = 2014-06-30
But you will evaluate against a transaction date & time. How do you ensure that absolutely every transactions on June 30 - right up to midnight - is included in the results?
Here is how: use 2014-07-01 instead of 2014-06-30, and here is what the query would look like - which does NOT use between!
SELECT
ST.UserID
, U.Username
, ST.SubscriptionID
, ST.DateOfSubscription
, SM.SubType
, SM.Details
, ST.Amount
, ST.EndDate
, ST.Status
FROM tr_t_subscriptiontransactions ST
JOIN tr_m_users U
ON U.UserID = ST.UserID
JOIN tr_m_subscription SM
ON SM.SubscriptionID = ST.SubscriptionID
WHERE (ST.DateOfSubscription >= pFromDate AND ST.DateOfSubscription < pToDate + 1)
AND (ST.EndDate >= pFromDate AND ST.EndDate < pToDate + 1)
AND ST.Status = pstatus
;
AVOID between for date ranges because it INCLUDES both the lower and upper boundary values.
... equivalent to the expression (min <= expr AND expr <= max)
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between
What this can lead to is an "anti-pattern" which look like this:
where dt_field between '2014-06-01 00:00:00' and '2014-06-30 23:59:59'
but there are time units smaller than one second, so that approach is imperfect. Don't attempt to overcome the deficiencies of between by adjusting the upper value this way. The simple and more accurate approach is to use >= and < adjusting the upper value by one whole time unit (usually the next day).
Hi i have the following SQL question:
SELECT station_id, filling_station_status,date_created ,
case when filling_station_status="FREE" then 0
else 1 end as status
FROM efahrung.electric_station_time_status
where station_id=11
In my table have a column filling_station_status.
It can be "FREE" or "IN_USE".
I want to group elements so, that if the filling_station_status is changed (from "FREE" to "IN_USE") it will create a date range in my case, date_created.
In the next change again from ("IN_USE" to "FREE") it creates a new date range.
Thanks for a suggestions.
If you just need SQL query to generate date range in output, then try this:
Select s.station_id,
Coalesce(e.filling_station_status, s.filling_station_status) fillingStationStatus,
case e.filling_station_status
when "FREE" then 0 else 1 end status,
s.date_created startDate,
e.date_created endDate
From efahrung.electric_station_time_status s
Left Join efahrung.electric_station_time_status e
On e.station_id = s.station_id
and s.filling_station_status = 'IN_USE'
and e.filling_station_status = 'FREE'
and e.date_created =
(Select Min(date_created)
From efahrung.electric_station_time_status
Where station_id = s.station_id
and date_created > s.date_created)
I am using MySQL to make a report showing the number of hours billed for a particular date range and project. The complexity is that the date range is variable for each project (different start month and start day). This information is coming from a value in another database/table.
I have the following UDF in MySQL:
DELIMITER //
CREATE FUNCTION TimeLeft(startday INT, today INT) RETURNS INT
DETERMINISTIC
BEGIN
DECLARE s INT;
IF startday < today THEN SET s = 0;
ELSE SET s = 1;
END IF;
RETURN s;
END //
DELIMITER;
I use that function in the following query, which is supposed to take the value returned in the TimeLeft function to determine the values for the start month (month(curdate())-#xx) and start day (#yy) for each project to calculate the hours:
AND time_records.record_date >= concat('2012/', month(curdate())-#xx , '/' , #yy)
Here's how I am setting the values for #xx and #yy:
SET #xx = 0; #this is the value that we will use to manipulate the month for the date range
SET #yy = 0;
#yy:= SELECT start_day_of_month FROM dashboard.client; #this doesn't seem to work
SELECT #xx:= TimeLeft(#yy,dayofmonth(curdate()));
I am getting some issues:
#yy is not getting the value - possibly my syntax is wrong?
The variables are set at the top of the code, so they are not getting changed for each project as they should be (there should be a different #xx and #yy for each project since each one has a different start and end date).
Here's the full query:
#below is where I assign the variables
SET #xx = 0; #this is the value that we will use to manipulate the month for the date range
SET #yy = 0;
#yy:= SELECT start_day_of_month FROM dashboard.client; #this doesn't seem to work
SELECT #xx:= TimeLeft(#yy,dayofmonth(curdate()));
# below is the MySQL query that is meant to use the variables assigned above
SELECT X.expr1 AS 'Project Name', #monthly_hours - SUM(X.expr2) AS 'Hours Billed
FROM
(SELECT
projects.name AS expr1
, sum(time_records.value) AS expr2
FROM project_objects
INNER JOIN projects
ON projects.id = project_objects.project_id
INNER JOIN time_records
ON time_records.parent_id = project_objects.id
WHERE time_records.parent_type = 'Task'
AND time_records.record_date >= concat('2012/', month(curdate())-#xx , '/' , #yy)
AND time_records.record_date <= curdate()
GROUP BY projects.name
UNION
SELECT
projects.name AS expr1
, sum(time_records.value) as expr2
FROM projects
INNER JOIN time_records
ON projects.id = time_records.parent_id
WHERE time_records.parent_type = 'Project'
AND time_records.record_date >= concat('2012/', month(curdate())-#xx , '/' , #yy)
AND time_records.record_date <= curdate()
GROUP BY projects.name) X
GROUP BY X.expr1
I think there is some issue of where I am assigning the variables #xx and #yy. These should be done for each individual Project, so putting them up on the top is probably not the best idea. I'm also not sure if I am assigning the #yy value correctly. It's supposed to query the value of the field of a table that is in another database but it keeps throwing a syntax error on the #yy assignment to that field.
Assign value to #yy inside select:
SELECT #yy:= start_day_of_month FROM dashboard.client;