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
Related
I have been searching for answers on this for a couple of days but not found any useful results.
A bit of the backstory:
I have about ~20k items that i'm trying to do a lifetime sales history report on.Some items have history from 1/1/2005. Sales are only noted on dates they occur.
Trying to graph first lifetime of sales history by days, first 6mo of sales, and last 30 days of sales. I don't have permission to add a temp table, so i am working with importing an excel file. (and for some reason you can't right-outer-join it to invoice date. im guessing because it's external?)
My problem is that there HAS to be a simple way to tell crystal to include/print missing dates without a lookup table. I've already got the start and end dates passed to parameters.
Isn't there a way to dynamically generate missing dates between {?PM-Start} to {?PM-End}? Using a lookup table to check if all days from 1/1/2005-currentdate match is 90,000,000,000 extra bloops to check.
That would take hours to run. It should be able to grab minumum(sale_date), and maximum(sale_date) and plop a record for each day whether it exists in the DB or not. (How is this not already a basic function?)
Or am I just missing something super simple?
Isn't there any way to dynamically generate missing dates between {?PM-Start} to {?PM-End}? Using a lookup table to check if all days from 1/1/2005-currentdate match is 90,000,000,000 extra bloops to check.
By using a query like below you can generate a calendar view to fill missing date:
;with years(yyyy) as (
select 2005
union all
select yyyy + 1
from years
where yyyy < datepart(year, getdate())
), months(mm) as (
select 1
union all
select mm + 1
from months
where mm < 12
), allDays(dd) as (
select 1
union all
select dd + 1
from allDays
where dd < 31
), calendar as (
select --datefromparts(y.yyyy, m.mm, d.dd) [date]
cast(cast(y.yyyy as varchar(4))+'-'+cast(m.mm as varchar(2))+'-'+cast(d.dd as varchar(2)) as date) [date]
from allDays d
cross join
months m
cross join
years y
where isdate(cast(y.yyyy as varchar(4))+ '/'+cast(m.mm as varchar(2))+'/'+cast(d.dd as varchar(2))) <> 0
)
select *
from calendar;
SQL Fiddle Demo
I'm a non profit lawyer trying to set up a SQL Server database for my agency. The issue I'm having is query based: I need a simple query that will aggregate the total number of rows on a table, not the sum of the cell contents.
I working with 4 columns of I to: attorney's name, client name, trial date and remedy (the last 2 are date and dollar amount, so integers].
*** Script for SelectTopNRows command from SSMS***
SELECT TOP 100
[attorney]
,[client]
,[trial_date]
,[remedy]
FROM [MyLegalDB]
WHERE [trial_date] between '20160101' and '20160531'
I'm trying to find a way (script, batch file, etc) that will populate a total number of cases by month (according to trial date) total number of clients, and sum the remedy column.
Sorry for the vagueness. There are privilege rules in place. Hope that helps clarify.
Thanks
Assuming that your case history spans years, not just months, try this:
SELECT
,YEAR([trial_date]) AS [Year]
,MONTH([trial_date]) AS [Month]
,COUNT(1) AS [Trial_Count]
FROM [MyLegalDB]
WHERE [trial_date] between '20160101' and '20160531'
GROUP BY YEAR([trial_date]), MONTH([trial_date])
If you want to separate this by attorney, you would need to add that column to the SELECT list, as well as the GROUP BY clause, as such:
SELECT
[attorney]
,YEAR([trial_date]) AS [Year]
,MONTH([trial_date]) AS [Month]
,COUNT(1) AS [Trial_Count]
FROM [MyLegalDB]
WHERE [trial_date] between '20160101' and '20160531'
GROUP BY [attorney], YEAR([trial_date]), MONTH([trial_date])
This is a very general answer to a very general question. If you want me to be more specific, I'm going to have to understand your goal a little better. Hope it helps.
Relatively new to MDX but familiar with SSRS and I have a query that works just as I want it to in the query designer in SSMS however I'm not quite sure how to parameterize this in SSRS.
Query returns department and shift as dimensions and Hours and Period as Measures with the query being filtered by a fiscal year and the Period measure as a specific period for that year.
Query works as expected but do not know how to convert to SSRS by implementing parameters for Fiscal Period and Fiscal Year.
Query:
WITH MEMBER [measures].[Period] AS
SUM([Date].[Fiscal Period].[P9],[Measures].[Hours])
SELECT NON EMPTY [Department].[Department].[Department] * [Associate].[Current Shift].[Current Shift] on Rows,
{[Measures].[Hours], [Measures].[Period]} ON Columns
FROM Personnel
WHERE [Date].[Fiscal Year].[FY2015]
Create two parameters for Fiscal year and Fiscal Period. Then use the below query:
SELECT NON EMPTY [Department].[Department].[Department] * [Associate].[Current Shift].[Current Shift] on Rows,
[Measures].[Hours] ON Columns
FROM Personnel
WHERE
(
StrToMember(#FiscalYear, CONSTRAINED),
StrToMember(#FiscalPeriod, CONSTRAINED)
)
I need to create a query that will sum the Amount of Open Accounts Receivable for each month.
Each record in my table has Amount,Posting Date, and Closed at Date. So for example, one record might be: Amount : 5000, Posting Date : 1/1/15, Closed at Date : 3/5/15. So for this record, the 5000 would need to be added to my January Open AR calculation because it is open as of 1/31/15, added to my February Open AR calculation because it is open as of 2/28/15, but excluded from my March Open AR calculation because it is closed as of 3/31/15.
I have managed to create a query that will work for an individual month, given a single inputted month-end date, i.e. 1/31/15 or 2/28/15, etc..., which goes into the WHERE clause.
SELECT SUM(Amount), threemonthavg, SUM(Amount)/threemonthavg AS DSO
FROM tbl1
WHERE PostDate <= #enddate AND ClosedDate > #enddate
Am I on the right track to expand this out to include each month in one query or would I be better off taking what I have, and creating a stored procedure to run this query for each month and union the results together?
I have a table that has a date/time field and am trying to figure out how to run a report so that I can view the sum of the amount between each month separately in the 2012 year.
The table has 2 fields, Amount and TimeStamp, and I'm trying to return a report like this:
January, 2012: $221.20
February, 2012: $150.20
etc etc.
Anyone have any ideas how to accomplish this easily in SQL Server? I want to avoid writing a seperate query for each individual month.
This can be solved by using the MONTH and YEAR Functions on SQL.
SELECT
SUM(Amount) as [Amount]
,MONTH(TimeStamp) as [Month]
,YEAR(TimeStamp) as [Year]
FROM
[MyTable]
GROUP BY
MONTH(TimeStamp)
,YEAR(TimeStamp)