I have created a SSRS report to show holidays that have been booked at a company, on each line it shows date from and date to, is there a way of getting the data to add a line for each day rather than a single line with from and to? as I want to populate a calendar using these fields.
This is regularly solved using a "calendar" table, i.e. a table that has a list of all dates. Then your query looks something like this:
select c.date
from data_table d
inner join
calendar_table c
on c.date between d.start_date and d.end_date
There's an example of how to create a very powerful calendar table here.
Related
I am trying to create a Attendance result set in SQL Server for using it in a SSRS report. The Employee Attendance table is as below:
EmpId
ADate
In
Out
1
2023-01-01
8:00
15:00
I need to calculate the Total working days for all months in a year and display the number of working days per employee. Report format should be as follows:
Saturday and Sunday being weekend, I can able to get the no of working days monthly.
Another table tbl_Holiday has entries for holidays Fromdate and ToDate. I need to consider that also when calculating working days. Several number of results i got from the internet for calculating this. But when creating a view using this data , it has to calculate workdays for each employee row
SELECT
EmpName, EmpId,
(SELECT COUNT(*) FROM tbl_EmpAttendance
WHERE EmpRecId = A.RecId
GROUP BY MONTH(Adate), YEAR(Adate)) AS WorkedDays,
dbo.fn_GetWorkDays(DATEFROMPARTS(YEAR(ADate), MONTH(ADate), 1), EOMONTH(ADate)) AS workingDays
FROM
tbl_Employee A
LEFT JOIN
tbl_EmpAttendance B ON A.RecId = B.EmpRecId
fn_GetWorkDays - calculates the working days for month.
I need to get number of holidays from tbl_holiday too, I understand that this query is becoming more complex than I thought. There must be simpler way to achieve this, can anyone please help?
I tried to get the result in one single view, for using that as SSRS report dataset
I was wondering if anyone can help me with this issue I have been having for at least a good 10 hours now after playing with it. I am going to use the Movies database from wiseowl to illustrate this instead.
I have the following SQL:
SELECT FilmReleaseDate
,FilmName
,directorname
,FilmRunTimeMinutes
,directorgender
,CountryName
,datename(M, FilmReleaseDate) AS [month]
,month(FilmReleaseDate) AS [month_no]
,year(FilmReleaseDate) AS [Year]
FROM tblFilm
INNER JOIN tblDirector ON directorID = FilmDirectorID
INNER JOIN tblCountry ON FilmCountryID = CountryID
WHERE FilmReleaseDate >= '2006-01-01'
now say I want to put this into a stacked chart with the movies from each country, the second axis will show the running minutes of the films. But the problem here is that I want the average for all countries, not for each one. Is there any way to do this so I have 1 line rather then 3 lines.
here is how the out put looks like
as you can see from output there are 3 lines for the 3 countries in the result, is there a way to get an average for all of them in one line rather then each one so the output will have only one line showing the average.
If anyone can help I will really appreciate it as I have spent countless hours on this. Any other info you need let me know.
Try this script. Then use the new field D.AVGFilmRunTimeMinutes on your chart. I added aliases on each table on the script. Just correct the aliases if they're pointing to a wrong table.
SELECT A.FilmReleaseDate
,A.FilmName
,B.directorname
,D.AVGFilmRunTimeMinutes
,B.directorgender
,C.CountryName
,datename(M, A.FilmReleaseDate) AS [month]
,month(A.FilmReleaseDate) AS [month_no]
,year(A.FilmReleaseDate) AS [Year]
FROM tblFilm A
INNER JOIN tblDirector B ON directorID = A.FilmDirectorID
INNER JOIN tblCountry C ON A.FilmCountryID = B.CountryID
LEFT JOIN (SELECT AVG(FilmRunTimeMinutes) AS AVGFilmRunTimeMinutes,FilmDirectorID FROM tblFilm GROUP BY FilmDirectorID) D
ON D.FilmDirectorID = A.FilmDirectorID
WHERE A.FilmReleaseDate >= '2006-01-01'
The reason your chart is producing 3 lines instead of one, is due to the grouping in you chart for each month (determined by how many countries).
I don't know the structure of your table, so I would recommend adding another field to your dataset:
YEAR(A.FilmReleaseDate) * 100 + MONTH(A.FilmReleaseDate) AS [YearMonth]
This will add a field with the format YYYYMM
Add this to your chart on your axis instead year and month_no.
In the Chart Data window, click the drop down next to the newly created Category Group for Year Month. Select Chart Group Properties, and copy the name. (It should be called something like - Chart1_CategoryGroup)
The final step is to modify the expression for your Film Run Time in the chart.
Click on the drop down next to FilmRunTimeMinutes, and click Series Properties.
In the value field use the following:
=Avg(Fields!FilmRunTimeMinutes.Value,"Chart1_CategoryGroup1")
Replacing Chart1_CategoryGroup1 with what your Category Group is called.
I have new table each month that is basically the same only it contains data for that month. For example
Table_201510 -- for October
Table_201511 -- for November and so on...
I want to create a view that will give me the possibility to get data for current month in uniform way. For example:
select * from vwTable_CurrentMonth
Is there a way of doing this without sp_executesql , like maybe creating an alias or something?
Have a view vwTable_CurrentMonth that you modify each time you create a new table to select from that new table.
I have a calendar table that has a list of all days frtom 01-JAN-1990 to 31-DEC-2050
That results in 22279 rows in my table.
A lot of queries we do, I join to the calendar as I need a list of dates based on certain data. For example:
SELECT ...
FROM Person A
INNER JOIN Calendar C
ON C.DateValue BETWEEN A.StartDate and A.EndDate
This is an example... but I'm looking for a list of the dates for the person, and a date column to come back.
What I'd like to know, is: Is the DateValue column a good candicate for an Index? And would there be ebefit of it being Clustered?
(SQL Server 2008 R2)
No, the Date type columns is not good candidate. Columns which you want to choose should be more simple. like int or BigInt types.
What is the best way in SSIS and which transformation(s) should I use to change (update) dates from Sundays to the previous Friday with the exception of the 1st day of the month. The exception would then do an Update to change the date to Monday.
I would also then need an update to change dates that are on certain Holidays like Thanksgiving and Christmas to the previous day, unless of course that day is a Sunday.
I know how to find these dates using SQL, but I'm somewhat of a newbie in SSIS and not sure how this works using transformations or what the best method would be.
For reference, using SQL I use this to identify the dates within a month that are Sundays. I then do an update to change those to either Fridays, or like I said, if that causes it go outside of the current month to Mondays:
SELECT * FROM DriverRoutes
where [Date] BETWEEN dateadd(month,datediff(month,0,'1/1/2014')-1,0)
AND dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0, '1/1/2014'), 0))
and DATEPART(w,[Date]) = '1'
update DriverRoutes
set DATEPART(w,[Date]) = '6'
where DATEPART(w,[Date]) = '1'
etc.... And then I also change dates manually when they are on Holidays, but don't want to do that each month anymore.
Thanks in advance!
Create a calendar table that holds a list of dates and their business meanings. The dates that are recognized locally as holidays, and the holidays that are recognized by a business are in constant flux. Instead of embedded formulas, you end up with easy-to-read code.
UPDATE t1
SET [DeliveryDate] = t2.[NextBusinessDay]
FROM DriverRoutes t1
INNER JOIN Calendar t2 ON (t1.[Date] = t2.[Date])
WHERE t2.[IsHoliday] = 1