I'm creating a SSRS report and I want to get the open cases for particular user in specific date range like below.
I have table called User from there I'm getting user info(User1,User2,User3).
I have open cases in the table management under description table.
I have c_date column in class table.
And I have 3 parameters user, startdate and enddate
And I need to use c_date between startdate and enddate.
If User enters startdate as 2019-01-01 and enddate as 2019-31-01, then I want to display the User1 who has open count.
For 0-5days and User1 who has open count for 6-11 days and same thing for user2 also.
Expected output:
User 0-5days 6-11days
---- ------- -------
User1 2 1
User2 1 4
User3 5 0
Explanation: User 1 has 2 open cases between 0-5 days means when I enter date range consider 2019-01-01 and 2019-31-01 so I have 2 open cases between first 0-5 days(2019-01-01 and 2019-05-01) and 1 open cases between next 6-11 days(2019-06-01 and 2019-11-01) etc.
Can I get result like this?
You should probably do this in the dataset query if possible. Use CASE and DATEDIFF to group your data something like
SELECT
[User],
[AnyOtherColumns],
CASE
WHEN DATEDIFF(d, #startdate, c_date) BETWEEN 0 AND 5 THEN '0-5'
WHEN DATEDIFF(d, #startdate, c_date) BETWEEN 6 AND 11 THEN '6-11'
ELSE 'older'
END AS [Age]
FROM myTable
WHERE [User] = #user
AND c_date BETWEEN #startdate AND #enddate
(done from memory so may not be perfect)
In your report you can use [User] on your row group, [Age] as your column group and then simply count any of the columns to give you the actual count of records.
You could do the counting in SQL too but I'm not sure if you need the detail for something else.
Considering you have two columns,
My approach would be
Have 3 parameters, one for user and other for To and from date.
Now selecting these parameters, add them to your dataset query as filter
Note you can apply filter on ssrs dataset as well but I would prefer on query level so that you have data been filtered and loaded only req one.
Then you can apply summing and grouping based on user and play around with Ssrs tablix to get the desired results.
https://www.mssqltips.com/sqlservertip/3453/sql-server-reporting-services-reports-with-optional-query-parameters/
https://reportsyouneed.com/ssrs-tip-put-parameters-in-your-query-not-your-filter/
Related
I have a query that pull out all customers records I will need to call within the next 5 days in the where clause as:
Remind to call =< DATEADD(DD,5,GETDATE() and
Remind to call >= GETDATE()
The way i want it to work is that i want all customers records that i need to call 5 days in advance from GETDATE(). Once it has entered the table, it needs to stay in the table until users enters a date for the 'Confirmstart' field on web application.
What happens is my query displays the data as it should except it removes the customers record after the 5 days where i want the records to remain until the user has entered a date for the 'ConfirmStart' field
For example - Lets say todays date is 03/08/2016 where I want to pull out the customers i need to call within the next 5 days, therefore in my where clause i wrote:
<Remind to call =< DATEADD(DD,5,GETDATE() and
<Remind to call >= GETDATE()
The Dateadd should pull out records up to 08/08/2016. t also say record iD called 'CR1' that has a RemindToCall date 08/08/2013 meet this criteria.
The 'CR1' record appears in the table which is great. However when GETDATE() reaches the 9/08/2016, the 'CR1' record disappears from the table when i want it to still remain in that table until the 'ConfirmedStart' date is entered.
How do i get my customer records to still appear in the table beyond the 5 days advance period even though i need the records to appear 5 days in advance?
Further Information:
To elaborate on my above example - here is the SQL code that for the 'CR1' record but instead of it being named 'CR1' in this live example its CoS ID record called'1232' - everything else remains the same:
SELECT
CoSID,
SurName,
FirstName,
EmailAddress,
CONVERT (varchar,UKAddressDeadline,103) as UKAddressDeadline,
CONVERT (varchar,PlacementStart,103) as PlacementStart,
CONVERT (varchar,PlacementEnd,103) as PlacementEnd,
CONVERT (varchar,RemindToCall,103) as RemindToCall,
ExtReference,
HostOrgName,
UKEndorsement,
PaymentUKBA
FROM
dbo.tbl_CoS
WHERE
RemindToCall between CAST('2016-08-03' AS DATE) and DateAdd(DD,+5, CAST('2016-08-03' AS DATE)) AND
(CoSNotRequired is null and
AppWithdrawn = 0 and
CoSWithdrawn = 0 and
VisaRejected = 0 and
UKAddressDeadline is not null AND
ConfirmedStart is NULL OR
UKEndorsement is NULL)
SQL RESULT of above
I hope this extra information/details help you.
Thanks people :D
assuming that the Confirmstart is null when no value are entered.
Add that to your where clause
WHERE
(
(
[Remind to call] =< DATEADD(DD,5,GETDATE() and
[Remind to call] >= GETDATE()
)
OR
(
Confirmstart IS NULL
)
)
Maybe you need something like this:
([Remind to call] =< DATEADD(DD,5,GETDATE() and
[Remind to call] > CONVERT(varchar(8), GETDATE(), 112)) or
ConfirmStart is Null
In other words, you have to pass in the second line not GetDate(), but 08/08/2016 00:00:00
I have following table in sql server.
ID ,EventID ,EventDate ,Title ,Type
Now I want to make query in below logic.
if user enter fromdate and todate. Here event are weekly or biweekly.
so let say fromdate '03/01/2016' to '03/31/2016 and type biweekly. Means columns are every biweekly.
so I want to generate query like.
3/1/2016 to 3/15/2016 | 3/16/2016 to 3/30/2016-these 2 biweekly are columns
if I pass so let say fromdate '03/01/2016' to '03/31/2016 and type weekly.
then columns should like every week
3/1/2016 to 3/8/2016 | 3/9/2016 to 3/16/2016 |3/17/2016 to 3/24/2016 and so on to till end date(To).
and rows of above output is Title.
How can this possible in sql server?
Get biweekly/weekly value. Let's say the user selects weekly. The value would be 7.
Use a loop to generate the columns. For instance, column1 would be the StartDate_to_EndDate. The StartDate would be the value the user entered. The EndDate would be the StartDate + 7 days(The value from #1 above)
Loop Through to get the second column etc etc (until the EndDate the user specified)
I am looking for some general advice rather than a solution. My problem is that I have a list of dates per person where due to administrative procedures, a person may have multiple records stored for this one instance, yet the date recorded is when the data was entered in as this person is passed through the paper trail. I understand this is quite difficult to explain so I'll give an example:
Person Date Audit
------ ---- -----
1 2000-01-01 A
1 2000-01-01 B
1 2000-01-02 C
1 2003-04-01 A
1 2003-04-03 A
where I want to know how many valid records a person has by removing annoying audits that have recorded the date as the day the data was entered, rather than the date the person first arrives in the dataset. So for the above person I am only interested in:
Person Date Audit
------ ---- -----
1 2000-01-01 A
1 2003-04-01 A
what makes this problem difficult is that I do not have the luxury of an audit column (the audit column here is just to present how to data is collected). I merely have dates. So one way where I could crudely count real events (and remove repeat audit data) is to look at individual weeks within a persons' history and if a record(s) exists for a given week, add 1 to my counter. This way even though there are multiple records split over a few days, I am only counting the succession of dates as one record (which after all I am counting by date).
So does anyone know of any db2 functions that could help me solve this problem?
If you can live with standard weeks it's pretty simple:
select
person, year(dt), week(dt), min(dt), min(audit)
from
blah
group by
person, year(dt), week(dt)
If you need seven-day ranges starting with the first date you'd need to generate your own week numbers, a calendar of sorts, e.g. like so:
with minmax(mindt, maxdt) as ( -- date range of the "calendar"
select min(dt), max(dt)
from blah
),
cal(dt,i) as ( -- fill the range with every date, count days
select mindt, 0
from minmax
union all
select dt+1 day , i+1
from cal
where dt < (select maxdt from minmax) and i < 100000
)
select
person, year(blah.dt), wk, min(blah.dt), min(audit)
from
(select dt, int(i/7)+1 as wk from cal) t -- generate week numbers
inner join
blah
on t.dt = blah.dt
group by person, year(blah.dt), wk
I have a rather interesting problem which I first thought would be straight-forward, but it turned out to be more complicated.
I have data like this:
Date User ID
2012-10-11 a
2012-10-11 b
2012-10-12 c
2012-10-12 d
2012-10-13 e
2012-10-14 b
2012-10-14 e
... ...
Each row has a Date, User ID couple which indicates that that user was active on that day. A user can appear on multiple dates and a date will have multiple users -- just like in the example. I have millions of rows like this which cover a time range of about 90 days.
Here's the question: For each day, I want to get the number of users who have not been active for the past 10 days. For instance, if the user "a" was active on 2012-05-31 and but hasn't been active on any of the days between 06-01 and 06-10, I want to count this user on 6/10. I wouldn't count him again on the following days though unless he becomes active and disappears again.
Can I do this in SQL or would I need to some kind of script to organize the data the way I want. What would be your recommendations? I use Hive.
Thank you so much!
I think you can do this in Hive-compatible SQL. Here is the idea.
For each user/date get the next date for the user.
Discard the original record if the next is less than 10 days after the current one.
Add 10 to the date
Aggregate and count
I am not sure of all the Hive functions for things like date. Here is an example of how to do it:
select date+10, count(*)
from (select t.userid, t.date,
min(case when tnext.date > t.date then tnext.date end) as nextdate
from t left outer join
t tnext
on t.userid = tnext.userid
group by t.userid, t.date
) t
where nextdate is null or nextdate - date >= 10
group by date+10;
Note that the inner subquery would be better written using:
on t.userid = tnext.userid and t2.date > t.date
However, I don't know if Hive supports such a join (it doesn't support non-equijoins and it not clear about whether one or all clauses have to be equal).
For the following table :
Id Timestamp AssignedTo
1 2012-01-01 User1
2 2012-01-02 User2
3 2012-01-10 User3
4 2012-01-15 User1
what would be the general approach in MS SQL Server to calculating how many days/hours/minutes the entity was assigned to a specific user?
As a developer (with basic SQL knowledge), I've always opted for Functions or Stored procedures when doing these kind of queries (these kind = queries including results based on the difference between two rows in a set).
In this example, I'd have a cursor iterating over the items, keeping the previous one in a variable, and then calculating the difference. However, I've been told that this is a serious performance concern, especially on large sets.
EDIT: Sample results
User TotalTime
User1 23d
User2 8d
User3 5d
i.e. the total time that an item was assigned to a specific user. User1's TotalTime is 23 days because it has been assigned to him since 2012-01-15 (and it's 2012-02-06 now), along with the first day it was assigned to him.
In this example I'd use a subquery within the select statement to get the end date for each row, then use this end date to get the time assigned to a specific user. The below is SQL Server syntax but the same principal can be applied whatever the RDBMS.
SELECT AssignedTo,
SUM(DATEDIFF(DAY, TimeStamp, ISNULL(EndDate, GETDATE()))) [Days]
FROM ( SELECT ID,
[TimeStamp],
AssignedTo,
( SELECT MIN(TimeStamp)
FROM [YOURTABLE] b
WHERE b.TimeStamp > a.TimeStamp
) [EndDate]
FROM [YOURTABLE] a
) a
GROUP BY AssignedTo