SQL Query for SSRS report - sql-server

I would like to create an ssrs report as below --
I have the following columns to be displayed-
| Tickets | Tickets |
| scanned on| scanned on|
Attraction | Hour | 09/08/2014| 09/09/2014| 09/10/2014| Day 4| Day 5| Day 6| 09/14/2014
Monday Tuesday Wednesday ...................... Sunday
U Mueseum | 9:00 AM | 10 | 40 |
10:00 aM |
..
..
..
..
..
23:00 AM
I will get the Start Date and End Date from the user. Now my problem is that I want a query for 7 days starting from the Start Date selected by the user to the End Date and for each hour i.e. for 1 day it would be 24 hrs, so 24*7 hours in total. When I display the value for scans in my current sql query it displays just for one day. How can I do it for 7 days, and the value of scans for that date should be displayed in the respective week day ie. Monday or Tuesday and so on. I am not able to get as to for each date and each hour the scan value changes, so I am confused and mixing up all here. The values for each hour on each day should be different and there is only one scan column, so how will the distinct values show up in the table.
I used the pivot table to convert the name of the week day from rows to individual columns.
Then the problem arises for ssrs report. How can this be executed in an ssrs report where the rows are for each hour and the columns displays the dates of the week selected. How can I achieve that in ssrs? I am getting currently for only 24 hrs, but i want the report to run for all 24 hrs for 7 days and should display the value side by side for each hour in each week day column.
Thank you.

Pass in one parameter into your stored procedure called #StartDate.
In SQL create the #EndDate like this
DECLARE #EndDate DATETIME
SET #EndDate = DATEADD("d",7,#StartDate)
Return your date information in an hour and a day column. Then use the grouping feature in ssrs to display the data.
I hope this helps you get a bit further with your issue.
Bobby

You could do this pretty easily with a Date Dimension table and a PIVOT operator. Give that a try.

Related

How to get a weekly view from date field in snowflake

I have a small query mentioned below.
Need to break down a date field "DAY" to monthly and weekly wise in snowflake.
Input:
DAY
-------
2022-06-09
2022-04-04
Output
DAY_MONTH
----------
2022-06-01
2022-04-01 Monthly wise--- Its done
Here I have used
DATE_FROM_PARTS( YEAR(DAY), MONTH(DAY), 1) AS DAY_MONTH
DAY_WEEK
----------
2022-06-06
2022-04-04
They should be first day of working days like (Monday). How to do that for a weekly view?
I think you're looking for the date_trunc function:
set ts = '2022-07-07 11:14:00'::timestamp;
select date_trunc('DAY', $TS);
select date_trunc('WEEK', $TS);
select date_trunc('MONTH', $TS);
This is showing for a timestamp to show how it truncates to the day, but it works the same way for date types. Truncating to the week will start by the week_start parameter that's in effect (it will default to Monday as the start for this function):
https://docs.snowflake.com/en/sql-reference/parameters.html

Consecutive day count with total

Is there a way to show if the days go consecutively? I need to show a total for when the date is 5 consecutive.
The data is shown below. Could I put a date range for April like: date between 2019-04-01 and 2019-05-31 and show the DATEPART(WEEK,date) for the first date of the 5 consecutive.
I'd like to show the total of 5 consecutive days like below. It skips the week that did not have a date for 5 consecutively.
Week Total
21 7.50
23 7.50
Something like this should work for you.
SELECT DATEPART(WEEK,[Date]),SUM([Dollar])
FROM [dbo].[TEST]
GROUP BY DATEPART(WEEK,[Date])
HAVING COUNT(DATEPART(WEEK,[Date])) >= 5
Here is a SQL Fiddle.

How can I get data for a window of 24 hours from given timestamp

Let's say I have a table with date-timestamp column in a table and every time I pass a date and time to it, I want to get the data for last 24 hours from that timestamp.
say, on querying for TIMESTAMP 23/03/2019 18:00:00
it should filter out and give results for the following period:
22/03/2019 18:00:01 to 23/03/2019 18:00:00
You may use an Interval expression to go back 1 day.
where timestamp_column > :v_timestamp - INTERVAL '1' DAY
AND timestamp_column <= :v_timestamp --The date you want to pass.

How to forecast count based on a day?

I am new to SQL Server world. I have a table as below:
alert_id | create_date | Status
---------+-------------+---------
1231 | 4/15/2017 | Open
1232 | 4/15/2017 | Open
1234 | 4/15/2017 | Closed
1235 | 4/16/2017 | Open
All of these alerts should be closed in 30 days. I need to get a forecast report which shows how many alerts are open for past 30 days.
I would like to write a select query whose output would be 2 columns. First would be Date and 2nd would be count. The date column should display all the dates for next 30 days and Count column should display the number of records which are due to expire on that day. Something like below would work. Please assist.
date | Count
----------+---------
5/15/2017 | 2
5/16/2017 | 3
5/17/2017 | 0
5/18/2017 | 0
.
.
.
6/14/2017 | 0
This is a job for GROUP BY and date arithmetic. In MySQL:
SELECT DATE(create_date) + INTERVAL 30 DAY expire_date, COUNT(*) num
FROM tbl
WHERE status = 'Open'
GROUP BY DATE(create_date)
DATE(create_date) + INTERVAL 30 DAY gets you the create date values with thirty days added.
GROUP BY(create_date) groups your data by values of your create date, truncated to midnight.
And, COUNT(*) goes with GROUP BY to tell you how many records in each group.
Edit In recent versions of SQL Server (MS)
SELECT DATEADD(day, 30, CAST(create_date AS DATE)) expire_date, COUNT(*) num
FROM tbl
WHERE status = 'Open'
GROUP BY CAST(create_date AS DATE)
Notice, please, that date arithmetic varies between make and model of SQL server software. That's why you get hassled by Stack Overflow users in comments when you use more than one tag like [oracle] [mysql] [sql-server] on your questions.
Cool, huh? You should read up on aggregate queries, sometimes called summary queries.
You're not going to get the missing dates with zeros by them. That's quite a bit harder to do with SQL.

Calculate payments on a month by month bases from date opened SQL 2008

I'm currently trying to replicate an old report that used to produce a rolling sum of collections. However it wasn't a standard month on month. Here is screen shot of the excel based report.
The blue section is based on a simple query and gives the dataset used to start(EXAMPLE):
SELECT COUNT(AccountNo) AS Number, SUM(Balance) AS Value, DATENAME(MM,DateOpened) AS Month, DATEPART(Y,DateOpened) AS Year FROM tblAccounts
GROUP BY DATENAME(MM,DateOpened), DATEPART(Y,DateOpened)
The tables are very basic :
AccountNo | Balance | DateOpened
12345 | 1245.55 | 01/01/2015
I'm struggling to get it to work out the months on a rolling basis, so Month 1 for Apr 2011 will be the first month for those files (payments in April), month 2 would be payments in May for the accounts opened in April (I hope that is clear).
So this means Month 1 for April would be April, and Month 1 for Nov would be Nov. Payments are stored in a table tblPayments
AccountNo | DatePayment | PaymentValue
12345 | 02/02/2015 | 15.99
Please ask if I haven't been clear enough
Assuming you have a column called "DatePayment", you should simply do something like this:
SELECT COUNT(AccountNo) AS Number, SUM(Balance) AS Value,
DATENAME(MM,DateOpened) AS Month, DATEPART(Y,DateOpened) AS Year,
DATEDIFF(MONTH, DateOpened, DatePayment) AS MonthN
FROM [...]
GROUP BY DATENAME(MM,DateOpened), DATEPART(Y,DateOpened),
DATEDIFF(MONTH, DateOpened, DatePayment)
The DATEDIFF simply counts the months between the date the account was opened and the date of the payment. Note that you might want to change the DateOpened to always be the 1st of the month in the DATEDIFF calculation.
In the FROM [...] part of your query, you will need a join between your Payments-table and the table holding your accounts, in order to be able to compare DateOpened with DatePayment. You should join them on the AccountNo-column. This looks something like this:
FROM Accounts INNER JOIN Payments ON Accounts.AccountNo = Payments.AccountNo
After doing this, you will need to make sure that all references to columns that exist in both tables are fully qualified. This means that COUNT(AccountNo) should be changed to COUNT(Accounts.AccountNo), etc.

Resources