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)
Related
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/
I am trying to calculate the percentage change between two dates in Access.
The user inputs the start date, an end date and the desired percentage change. If the result matches the criteria it should return the name.
Please note this is the dd/mm/yy format.
This is the table I have:
Name | Payment | DateTime
John | 53.00 | 06/01/18
Mike | 23.16 | 12/07/18
Steve | 31.28 | 21/03/18
John | 58.30 | 22/04/18
For example, if the user inputs a start date of 01/01/18, an end date of 23/04/18, and a desired percentage change of 10 percent then it should return the name John.
Currently, this is the query I have:
SELECT Name
FROM UserPayments
WHERE DateTime >= '01/01/18' AND DateTime <= '23/04/18';
I would like to calculate the total percentage change for two entries. This should be the record date immediately after the first date and the date immediately before the second date.
I'm completely stuck on how to do this in Access (SQL).
I think I need to use a join, but I have not done these before.
There should be a Users table with a primary key of something like UserID, and that UserID should be used in the UserPayments table instead of the Name of the user.
Although it would by nice to use functions like First and Last, MS Access will not allow to use ORDER BY for sorting the details in a group defined by GROUP BY. Therefore, you will have to use subqueries to retrieve the two values for the percentage calculation. If you have a Users table like mentionned above and the DateTime value can be assumed to be unique per User, this could look like this:
SELECT Users.Name,
(SELECT TOP 1 Payment FROM UserPayments
WHERE UserID = Users.UserID AND DateTime Between #01/01/2018# And #04/23/2018#
ORDER BY DateTime DESC)
/
(SELECT TOP 1 Payment FROM UserPayments
WHERE UserID = Users.UserID AND DateTime Between #01/01/2018# And #04/23/2018#
ORDER BY DateTime ASC)
-1 AS PercentChange
FROM Users;
Be aware of rounding errors and format the result as a percentage.
Table format is as follows:
Date ID subID value
-----------------------------
7/1/1996 100 1 .0543
7/1/1996 100 2 .0023
7/1/1996 200 1 -.0410
8/1/1996 100 1 -.0230
8/1/1996 200 1 .0121
I'd like to apply STDEV to the value column where date falls within a specified range, grouping on the ID column.
Desired output would like something like this:
DateRange, ID, std_v
1 100 .0232
2 100 .0323
1 200 .0423
One idea I've had that works but is clunky, involves creating an additional column (which I've called 'partition') to identify a 'group' of values over which STDEV is taken (by using the OVER function and PARTITION BY applied to 'partition' and 'ID' variables).
Creating the partition variable involves a CASE statement prior where a given record is assigned a partition based on its date falling within a given range (ie,
...
, partition = CASE
WHEN date BETWEEN '7/1/1996' AND '10/1/1996' THEN 1
WHEN date BETWEEN '10/1/1996' AND '1/1/1997' THEN 2
...
Ideally, I'd be able to apply STDEV and the OVER function partitioning on the variable ID and variable date ranges (eg, say, trailing 3 months for a given reference date). Once this works for the 3 month period described above, I'd like to be able to make the date range variable, creating an additional '#dateRange' variable at the start of the program to be able to run this for 2, 3, 6, etc month ranges.
I ended up coming upon a solution to my question.
You can join the original table to a second table, consisting of a unique list of the dates in the first table, applying a BETWEEN clause to specify desired range.
Sample query below.
Initial table, with columns (#excessRets):
Date, ID, subID, value
Second table, a unique list of dates in the previous, with columns (#dates):
Date
select d.date, er.id, STDEV(er.value)
from #dates d
inner join #excessRet er
on er.date between DATEADD(m, -36, d.date) and d.date
group by d.date, er.id
order by er.id, d.date
To achieve the desired next step referenced above (making range variable), simply create a variable at the outset and replace "36" with the variable.
I have a table AreaValues with columns ID, Name, Value, startDate, EndDate.
I need to view a report that select the values of every month with month name or number
Example: this is my table AreaValues:
ID Name Value StartDate EndDate
-------------------------------------------
1 Test 200 05-06-2012 07-08-2016
I need report get the value using SQL Server Reporting Services or query or any way:
month value = (200 / count of months from startdate to end date)
ID Name Value year2012 year2013 year2014 year2015 year2016
1 test 200 6,7,8,9,10,11,12 1,2,3,....12 1,2,3,....12 1,2,3,....12 1,2,3,4,5,6,7
and do that for every record in the table
Please, I need help
It Solved
I used a Loop in SQl that get all months between two dates
Then
Create report with Matrix get the months above and ID left
Say I have a table:
ID DATE
1 2/1/12
2 3/1/12
3 1/1/12
4 4/1/12
How would I go about selecting the first date found when decrementing from a given date.
Example: Find the last entry before 4/1/12, by date. Return entry at SQL ID 2.
If this was added:
ID DATE
5 3/2/12
Than the above example would return the entry at SQL ID 5.
How would I represent what I need in SQL?
Select top 1 ID, DATE
from table
where DATE < '4/1/12'
order by DATE DESC
other ideas: (in addition to Gratzy's)
select the MAX date where the date is less than the target date.
use a LAG function.