SQL Daily Totals - sql

Ok so what I am trying to make is a ssrs report that gets daily totals of clients seen per employee and list that day by day. So the top row would show every day in a month e.g 1st ,2nd,3rd. The first column should show the names of the employee.
I'm pretty sure I nee to use a matrix to do this but am unsure how to go about this with listing out the days. If anyone can help me or point me in the right direction that would be great.
To add more info I have a table called Employees with all the employees I want to list and a table appointments with all the clients that the employees have seen. I want to total the clients seen for each employee for every day of the month with the days listed across the top of the table.
for example
| 1st | 2nd | 3rd | 4th | 5th | 6th | 7th .....
emp1 2 3 5 4 5 5 6
emp2 3 4 5 9 1 3 5
emp3 1 0 0 4 5 9 2
emp4 8 3 7 2 9 3 4

If you want your rows to be columns, you need to use pivot like so:
Example:
SELECT VendorID, [250] AS Emp1, [251] AS Emp2, [256] AS Emp3, [257] AS Emp4, [260] AS Emp5
FROM
(SELECT PurchaseOrderID, EmployeeID, VendorID
FROM Purchasing.PurchaseOrderHeader) p
PIVOT
(
COUNT (PurchaseOrderID)
FOR EmployeeID IN
( [250], [251], [256], [257], [260] )
) AS pvt
ORDER BY pvt.VendorID;
If you want your columns to be rows, you would use UNPIVOT.

You are correct to use a matrix.
Assuming you already have a query selecting the necessary information and joining properly:
Create a Column Group, grouping by the appointment date. Format your column header using Day(Fields!AppointmentDateTime.Value) to get the value from 1 to 31.
Create a Row Group, grouping by Employee. In the data cell, total the visits by using =Count(AppointmentDateTime.Value).
If this report will span several months, include a parent column group where you group by Month(Fields!AppointmentDateTime.Value). You can also add totals by right-clicking on a textbox in the row group and selecting "Add Total".

Related

Nested Impala query to find occurrences of an event twice

I have a table which contains employee name, employee ID and timestamp of times logged.
employee id
employee name
event_time
1
Harry
2021-11-18T20:03:25Z
1
Harry
2021-11-19T20:03:25Z
1
Harry
2021-11-20T20:03:25Z
2
Charlie
2021-11-18T20:03:25Z
I need to find out compliance percentage. Basically the percentage of days logged in out of the difference between max day and min day . This can be done through the following in Impala
SELECT employee_id,employee_name
(count(distinct(cast(event_time as timestamp))))/(datediff(cast(max(event_time) as TIMESTAMP),cast(min(event_time) as timestamp))) * 100.0 as compliance_percentage
FROM employee
group by employee_id,employee_name;
Now if the event_time is to be considered compliant only if it occurs twice i.e if a person logs two times and only two times how can we write a query in Impala so that we get the required result?
For example if Harry has logged for exactly 2 times on 8 different days between 1st Feb 2021 and 1st Feb 2022 this would be the expected result(cp would be (8/365 * 100)
employee_id
employee_name
cp
1
Harry
2.19

SQL - Monthly cumulative count of new customer based on a created date field

Thanks in advance.
I have Customer records that look like this:
Customer_Number
Create_Date
34343
01/22/2001
54554
03/03/2020
85296
01/01/2001
...
I have about a thousand of these records (customer number is unique) and the bossman wants to see how the number of customers has grown over time.
The output I need:
Customer_Count
Monthly_Bucket
7
01/01/2021
9
02/01/2021
13
03/01/2021
20
04/01/2021
The customer count is cumulative and the Monthly Bucket will just feed the graphing package to make a nice bar chart answering the question "how many customers to we have in total in a particular month and how is it growing over time".
Try the following SELECT SQL with a sub-query:
SELECT Customer_Count=
(
SELECT COUNT(s.[Create_Date])
FROM [Customer_Sales] s
WHERE MONTH(s.[Create_Date]) <= MONTH(t.[Create_Date])
), Monthly_Bucket=MONTH([Create_Date])
FROM Customer_Sales t
WHERE YEAR(t.[Create_Date]) = ????
GROUP BY MONTH(t.[Create_Date])
Where [Customer_Sales] is the sales table and ??? = your year

How to retreive equivalent records for a specific person's record in a specific month using sql

I have an assignment which is to view names of the top 3 high achievers in my department. A high achiever is a regular employee who stayed the longest hours in the company for a certain month and all tasks assigned to him/her with a deadline of this month are fixed. My query :
SELECT distinct top 3 Regular_Employees.username ,
DATEDIFF(HOUR,start_time,end_time)
FROM Regular_Employees INNER JOIN Staff_Members on
Regular_Employees.username=Staff_Members.username INNER JOIN Tasks ON
Staff_Members.username=Tasks.regular_employee INNER join Attendance_Records
on Regular_Employees.username = Attendance_Records.staff
WHERE MONTH(deadline) = MONTH('11/11/2017') AND YEAR(deadline) =
YEAR('11/11/2017') AND department=Staff_Members.department
ORDER BY DATEDIFF(HOUR,start_time,end_time) DESC
I wanted to retrieve top 3 records in November and the result was
daniel.magdi 2
farid.elsoury 2
joy.ahmed 2
where the employees are the right employees while the records '2' is the record for one day not the whole month. How can i compute the record for each employee per whole month and view the results ?
I want to insert date as ' 11/11/2017' and the result would be
Result
Employee HOURS
daniel.magdi 180
farid.elsoury 179
joy.ahmed 175
where those are the highest records in November

Sql server- convert row into column

I have a problem and can't find a way to solve it.
This is my part of my query that I cannot solve.
Select Datepart(day,date) as Day,
Department,SUM(hourly) as workhours
from MIVDb.dbo.evid as p
Inner Join MIVdb.dbo.workers as r
On p.rad=r.rad
Group by Department,
Datepart(day,date)
Nothing special about it. My problem is that I get table looking like
Days Dept Hours
1 Sales 8
2 Cust 10
And i need:
Dept 1 2 ....etc
Sales 8 10 ....etc
So I need days to be columns. Problem is that Days column is dynamic, it can have on day, of 20 day, it can be week or even month. Can someone point help me with this.

MDX last order date and last order value

I've googled but I cannot get the point
I've a fact table like this one
fact_order
id, id_date, amount id_supplier
1 1 100 4
2 3 200 4
where id_date is the primary key for a dimension that have
id date month
1 01/01/2011 january
2 02/01/2011 january
3
I would like to write a calculated member that give me the last date and the last amount for the same supplier.
Last date and last amount -- it's a maximum values for this supplier?
If "yes", so you can create two metrics with aggregation "max" for fields id_date and amount.
And convert max id_date to appropriate view in the following way:
CREATE MEMBER CURRENTCUBE.[Measures].[Max Date]
AS
IIF([Measures].[Max Date Key] is NULL,NULL,
STRTOMEMBER("[Date].[Calendar].[Date].&["+STR([Measures].[Max Date Key])+"]").name),
VISIBLE = 1 ;
It will works, If maximum dates in your dictionary have maximum IDs. In my opinion You should use date_id not 1,2,3..., but 20110101, 20110102, etc.
If you don't want to obtain max values - please provide more details and small example.