Getting Running Total of Time column using T-SQL in SQL server - sql-server

I have a table XYZ with employee login duration details in TIME datatype column.
EmployeeID | DomainID | LoginDuration
----------------------------------------------------------------
1111 12 02:32:55:0000000
1111 4 00:57:17.0000000
1111 12 01:06:25.0000000
1111 11 03:31:23.0000000
2222 11 02:42:17.0000000
2222 4 03:54:52.0000000
2222 10 04:08:29.0000000
Apart from the above columns, I also have LoginTimeStamp and LoginWeek columns, which I am using in a JOIN statement.
I am trying to obtain running totals for the LoginDuration Column as follows:
EmployeeID | DomainID | HoursBefore | LoginDuration | HoursAfter |
---------------------------------------------------------------------------------
1111 12 00:00:00.0000000 02:32:55:0000000 **00:00:00.0000000**
1111 4 02:32:55.0000000 00:57:17.0000000 03:30:12.0000000
1111 12 03:30:12.0000000 01:06:25.0000000 04:36:37.0000000
1111 11 04:36:37.0000000 03:31:23.0000000 08:08:00.0000000
2222 11 00:00:00.0000000 02:42:17.0000000 **00:00:00.0000000**
2222 4 01:32:31.0000000 03:54:52.0000000 04:14:48.0000000
2222 10 04:14:48.0000000 04:08:29.0000000 08:09:40.0000000
HoursBefore is Previous Value of HoursAfter(00:00:00 for first row of each employee)
HoursAfter = HoursBefore+LoginDuration
For this purpose,I wrote the below query, But I am getting an error with the HoursAfter Column. It is not adding up the current value and previous value for each employee.
SELECT
a.EmployeeID,a.LoginDuration,
COALESCE(CAST(
DATEADD(ms,
SUM(DATEDIFF(ms,0,CAST(b.LoginDuration as datetime)))
, 0)
as time)
,'00:00:00') AS HoursBefore,
a.LoginDuration as Hours,
COALESCE(CAST(
DATEADD(ms,
SUM(DATEDIFF(ms,0,CAST(b.LoginDuration as datetime)))
, a.Loginduration)
as time)
,'00:00:00') As HoursAfter
FROM XYZ AS a
LEFT OUTER JOIN XYZ AS b
ON (a.EmployeeID = b.EmployeeID)
AND (a.LoginWeek = b.LoginWeek)
AND (b.LoginTimeStamp < a.LoginTimeStamp)
GROUP BY a.EmployeeID, a.LoginTimeStamp,a.LoginDuration
ORDER BY a.LoginWeek, a.EmployeeID, a.LoginTimeStamp;
I need help with the query such that the HoursAfter column for each employee is appropriate.
Any help would be greatly appreciated.
(This is my first query, reply if you may need any further details.)
Thanks.

Pity SQL Server doesn't support period datatype yet, it would make the math so much simpler.
However, it dos have rather good support for window functions in newer versions, which we can use to solve this:
declare #t table (ID int, EmployeeID int, DomainID int, LoginDuration time)
insert #t
values
(1, 1111, 12, '02:32:55.0000000'),
(2, 1111, 4, '00:57:17.0000000'),
(3, 1111, 12, '01:06:25.0000000'),
(4, 1111, 11, '03:31:23.0000000'),
(5, 2222, 11, '02:42:17.0000000'),
(6, 2222, 4, '03:54:52.0000000'),
(7, 2222, 10, '04:08:29.0000000')
;with x as (
select *, dateadd(second, sum(datediff(second, 0, loginduration)) over (partition by employeeid order by id), 0) sum_duration_sec,
row_number() over (partition by employeeid order by id) rn
from #t
)
select
employeeid,
domainid,
convert(time, isnull(lag(sum_duration_sec) over (partition by employeeid order by id),0)) hoursbefore,
loginduration,
convert(time, case when rn = 1 then 0 else sum_duration_sec end) hoursafter
from x
I introduced the ID column for brevity to establish the sequence, you'd probably want to use the (LoginWeek, LoginTimestamp) to order by.
Also, not sure about the requirement that HoursAfter should be 0 in 1st and 5th row - if not, delete the row_number() thing altogether.

use OUTER APPLY to calculate the Hours After. Hours Before is just Hours After subtracting current duration
SELECT a.EmployeeID, a.DomainID,
HoursBefore = CONVERT(TIME, DATEADD(SECOND, b.after_secs - DATEDIFF(SECOND, 0, a.LoginDuration), 0)),
a.LoginDuration,
HoursAfter = CONVERT(TIME, DATEADD(SECOND, b.after_secs, 0))
FROM XYZ AS a
OUTER APPLY
(
SELECT after_secs = SUM(DATEDIFF(SECOND, 0, x.LoginDuration))
FROM XYZ x
WHERE x.EmployeeID = a.EmployeeID
AND x.LoginWeek = a.LoginWeek
AND x.LoginTimeStamp <= a.LoginTimeStamp
) b

Related

How to calculate running total over specific date or better?

I would like to calculate the what orders can be completed and what dates are missing (diff) after completing as many orders as possible at the moment. Picked in order of FEFO.
When thinking about the problem I think that some kind of a running sum based on both the dates of the stock and the orders would be one way to go. Based on Calculate running total / running balance and other similar threads it seems like a good fit for the problem - but I'm open to other solutions.
Example code
DECLARE #stockTable TABLE (
BATCH_NUM nvarchar(16),
QUANTITY int,
DATE_OUTGO DATE
)
DECLARE #orderTable TABLE (
ORDER_ID int,
QUANTITY int,
DATE_OUTGO DATE
)
INSERT INTO #stockTable (BATCH_NUM, QUANTITY, DATE_OUTGO)
VALUES
('1000', 10, '2017-08-25'),
('1001', 20, '2017-08-26'),
('1002', 10, '2017-08-27')
INSERT INTO #orderTable (ORDER_ID, QUANTITY, DATE_OUTGO)
VALUES
(1, 10, '2017-08-25'),
(1, 12, '2017-08-25'),
(2, 10, '2017-08-26'),
(3, 10, '2017-08-26'),
(4, 16, '2017-08-26')
SELECT
DATE_OUTGO,
SUM(RunningTotal) AS DIFF
FROM (
SELECT
orderTable.DATE_OUTGO AS DATE_OUTGO,
RunningTotal = SUM(stockTable.QUANTITY - orderTable.QUANTITY ) OVER
(ORDER BY stockTable.DATE_OUTGO ROWS UNBOUNDED PRECEDING)
FROM
#orderTable orderTable
INNER JOIN #stockTable stockTable
ON stockTable.DATE_OUTGO >= orderTable.DATE_OUTGO
GROUP BY
orderTable.DATE_OUTGO,
stockTable.DATE_OUTGO,
stockTable.QUANTITY,
orderTable.QUANTITY
) A
GROUP BY DATE_OUTGO
Results
The correct result would look like this.
-------------------------
| OT_DATE_OUTGO | DIFF |
-------------------------
| 2017-08-25 | 0 |
-------------------------
| 2017-08-26 | -18 |
-------------------------
My result currently looks like this.
-------------------------
| OT_DATE_OUTGO | DIFF |
-------------------------
| 2017-08-25 | 80 |
-------------------------
| 2017-08-26 | 106 |
-------------------------
I've taken out complexities like item numbers, different demands simultaneously (using the exact date only and date or better) etc. to simplify the core issue as much as possible.
Edit 1:
Updated rows in both tables and results (correct and with original query).
First answer gave a diff of -12 on 2017-08-25 instead of 0. But 2017-08-26 was correct.
You can use the following query:
;WITH ORDER_RUN AS (
SELECT SUM(SUM(QUANTITY)) OVER (ORDER BY DATE_OUTGO) AS ORDER_RUNTOTAL,
DATE_OUTGO
FROM #orderTable
GROUP BY DATE_OUTGO
), STOCK_RUN AS (
SELECT SUM(SUM(QUANTITY)) OVER (ORDER BY DATE_OUTGO) AS STOCK_RUNTOTAL,
DATE_OUTGO
FROM #stockTable
GROUP BY DATE_OUTGO
)
SELECT ORR.DATE_OUTGO AS OT_DATE_OUTGO,
X.STOCK_RUNTOTAL - ORDER_RUNTOTAL AS DIFF
FROM ORDER_RUN AS ORR
OUTER APPLY (
SELECT TOP 1 STOCK_RUNTOTAL
FROM STOCK_RUN AS SR
WHERE SR.DATE_OUTGO <= ORR.DATE_OUTGO
ORDER BY SR.DATE_OUTGO DESC) AS X
The first CTE calculates the order running total, whereas the second CTE calculates the stock running total. The query uses OUTER APPLY to get the stock running total up to the date the current order has been made.
Edit:
If you want to consume the stock of dates that come in the future with respect to the order date, then simply replace:
WHERE SR.DATE_OUTGO <= ORR.DATE_OUTGO
with
WHERE STOCK_RUNTOTAL <= ORDER_RUNTOTAL
in the OUTER APPLY operation.
Edit 2:
The following improved query should, at last, solve the problem:
;WITH ORDER_RUN AS (
SELECT SUM(SUM(QUANTITY)) OVER (ORDER BY DATE_OUTGO) AS ORDER_RUNTOTAL,
DATE_OUTGO
FROM #orderTable
GROUP BY DATE_OUTGO
), STOCK_RUN AS (
SELECT SUM(SUM(QUANTITY)) OVER (ORDER BY DATE_OUTGO) AS STOCK_RUNTOTAL,
SUM(SUM(QUANTITY)) OVER () AS TOTAL_STOCK,
DATE_OUTGO
FROM #stockTable
GROUP BY DATE_OUTGO
)
SELECT ORR.DATE_OUTGO AS OT_DATE_OUTGO,
CASE
WHEN X.STOCK_RUNTOTAL - ORDER_RUNTOTAL >= 0 THEN 0
ELSE X.STOCK_RUNTOTAL - ORDER_RUNTOTAL
END AS DIFF
FROM ORDER_RUN AS ORR
OUTER APPLY (
SELECT TOP 1 STOCK_RUNTOTAL
FROM STOCK_RUN AS SR
WHERE STOCK_RUNTOTAL >= ORDER_RUNTOTAL -- Stop if stock quantity has exceeded order quantity
OR
STOCK_RUNTOTAL = TOTAL_STOCK -- Stop if the end of stock has been reached
ORDER BY SR.DATE_OUTGO) AS X

SQL Select Sequential Dates with Additional Lookup Values

I am trying to grab a series of dates and the corresponding values (if any) that exist in my database.
I have two parameters - today (date using getDate()) - and a number of days (integer). For this example, I'm using the value 10 for the days.
Code to get the sequential dates for 10 days after today:
SELECT top 10 DATEADD(DAY, ROW_NUMBER()
OVER (ORDER BY object_id), REPLACE(getDate(),'-','')) as Alldays
FROM sys.all_objects
I now need to look up several values for each day in the sequential days code, which may or may not exist in the time table (we assume 8 hours for all dates, unless otherwise specified). The lookup would be on the field recordDateTime. If no "hours" value exists in the table cap_time for that date, I need to return a default value of 8 as the number of hours. Here's the base query:
SELECT u.FullName as UserName, d2.department,
recordDateTime, ISNULL(hours,8) as hours
FROM cap_time c
left join user u on c.userID = u.userid
left join dept d2 on u.deptID = d2.DeptID
WHERE c.userid = 38 AND u.deptID = 1
My end result for the next 10 days should be something like:
Date (sequential), Department, UserName, Number of Hours
I can accomplish this using TSQL and a temp table, but I'd like to see if this can be done in a single statement. Any help is appreciated.
Without any DDL or sample data it's hard to determine exactly what you need.
I think this will get you pretty close (note my comments):
-- sample data
------------------------------------------------------------------------------------------
DECLARE #table TABLE
(
fullName varchar(10),
department varchar(10),
[hours] tinyint,
somedate date
);
INSERT #table VALUES
('bob', 'sales', 5, getdate()+1),
('Sue', 'marketing', 3, getdate()+2),
('Sue', 'sales', 12, getdate()+4),
('Craig', 'sales', 4, getdate()+8),
('Joe', 'sales', 18, getdate()+9),
('Fred', 'sales', 10, getdate()+10);
--SELECT * FROM #table
;
-- solution
------------------------------------------------------------------------------------------
WITH alldays([day]) AS -- logic to get your dates for a LEFT date table
(
SELECT TOP (10)
CAST(DATEADD
(
DAY,
ROW_NUMBER() OVER (ORDER BY object_id),
getdate()
) AS date)
FROM sys.all_objects
)
SELECT d.[day], t.fullName, department, [hours] = ISNULL([hours], 8)
FROM alldays d
LEFT JOIN #table t ON d.[day] = t.somedate;
Results:
day fullName department hours
---------- ---------- ---------- -----
2017-04-12 bob sales 5
2017-04-13 Sue marketing 3
2017-04-14 NULL NULL 8
2017-04-15 Sue sales 12
2017-04-16 NULL NULL 8
2017-04-17 NULL NULL 8
2017-04-18 NULL NULL 8
2017-04-19 Craig sales 4
2017-04-20 Joe sales 18
2017-04-21 Fred sales 10
Maybe a subquery and the in statement, like:
SELECT u.FullName as UserName, d2.department,
recordDateTime, ISNULL(hours,8) as hours
FROM cap_time c
left join user u on c.userID = u.userid
left join dept d2 on u.deptID = d2.DeptID
WHERE c.userid = 38 AND u.deptID = 1 and recordDateTime in
(SELECT top 10 DATEADD(DAY, ROW_NUMBER()
OVER (ORDER BY object_id), REPLACE(getDate(),'-','')) as Alldays
FROM sys.all_objects)

apply query to each part of table individually

I need to run some query against each rowset in a table (Azure SQL):
ID CustomerID MsgTimestamp Msg
-------------------------------------------------
1 123 2017-01-01 10:00:00 Hello
2 123 2017-01-01 10:01:00 Hello again
3 123 2017-01-01 10:02:00 Can you help me with my order
4 123 2017-01-01 11:00:00 Are you still there
5 456 2017-01-01 10:07:00 Hey I'm a new customer
What I want to do is to extract "chat session" for every customer from message records, that is, if the gap between someone's two consecutive messages is less than 30 minutes, they belong to the same session. I need to record the start and end time of each session in a new table. In the example above, start and end time of the first session for customer 123 are 10:00 and 10:02.
I know I can always use cursor and temp table to achieve that goal, but I'm thinking about utilizing any pre-built mechanism to reach better performance. Please kindly give me some input.
You can use window functions instead of cursor. Something like this should work:
declare #t table (ID int, CustomerID int, MsgTimestamp datetime2(0), Msg nvarchar(100))
insert #t values
(1, 123, '2017-01-01 10:00:00', 'Hello'),
(2, 123, '2017-01-01 10:01:00', 'Hello again'),
(3, 123, '2017-01-01 10:02:00', 'Can you help me with my order'),
(4, 123, '2017-01-01 11:00:00', 'Are you still there'),
(5, 456, '2017-01-01 10:07:00', 'Hey I''m a new customer')
;with x as (
select *, case when datediff(minute, lag(msgtimestamp, 1, '19000101') over(partition by customerid order by msgtimestamp), msgtimestamp) > 30 then 1 else 0 end as g
from #t
),
y as (
select *, sum(g) over(order by msgtimestamp) as gg
from x
)
select customerid, min(msgtimestamp), max(msgtimestamp)
from y
group by customerid, gg

Select only if more than X occurrences

My data is something like this:
Client Number | Order Date | Order Amount | Sequence (created with Row_Number())
I have created a sequence with Row_Number(), so I can see how many orders a client has.
If I use WHERE Sequence > 3, I lose the orders prior to 3. I can't use HAVING because I need to see every orders. How can I select the Client Numbers with more than 3 orders?
I would like to see:
Client Number | Order Date | Order Amount | Sequence
1111 Jan 01 100 1
1111 Jan 02 100 2
1111 Jan 03 100 3
1112 Jan 01 100 1
1112 ... ... ...
1112 Jan 20 100 20
So only those with Sequence above 3, while still keeping the line with sequence 1 and 2.
SELECT *
FROM data
WHERE ClientNumber IN
(
SELECT ClientNumber
FROM data
GROUP BY ClientNumber
HAVING COUNT(1) >= 3
);
create table #test(clientnumber int, orderdate datetime, orderamount int)
insert into #test values
(1110, '01/01/2016', 100),
(1110, '01/02/2016', 100),
(1111, '01/01/2016', 100),
(1111, '01/02/2016', 100),
(1111, '01/03/2016', 100),
(1112, '01/01/2016', 100),
(1112, '01/02/2016', 100),
(1112, '01/03/2016', 100),
(1112, '01/04/2016', 100);
with cte as(
select clientnumber, orderdate, orderamount,
count(*) over(partition by clientnumber ) as ran
from #test)
select * from cte
where ran >= 3

Get payment total minus amount received

I have two tables.
1.Invoice
invoice_Id invoice_no client_id date total_price
-----------------------------------------------------------------------------
2 INV00001 9 2014-10-15 200.00
7 INV00002 9 2014-10-16 560.00
8 INV00003 9 2014-10-21 100.00
11 INV00004 9 2014-10-27 101.00
2.Invoice_payment
InvPayment_id client_id Invoice_Id receipt_no payment_date amount_received discount
--------------------------------------------------------------------------------------------
6 9 8 REC00002 2014-10-31 5.00 0.00
Now I want to get Total Amount Due by Client by totalling the invoice amounts and subtracting any amounts received.
Expected Result:
client_id Total_price Due_Amount
-----------------------------------------------------------------------------
9 961.00 956.00
NOTES:
There will be zero rows if no payments are taken to date.
There could be multiple rows if more than one payment is taken.
Here is what I have tried:
;WITH cte (clientid, invoiceid, paid, disc)
As
(
Select client_id clientId, invoice_Id invoiceId, sum(amount_received) paid, sum(discount) disc
From tbl_Invoice_Payment
Group by invoice_id, client_id
)
Select I.client_id, invoice_Id, invoice_no, I.due_date
,SUM(I.total_price), Isnull(SUM(paid), 0) Paid, (SUM(Total_price) - Isnull(SUM(paid),0) - Isnull(SUM(disc),0)) Balance
--,I.total_price, Isnull(paid, 0) Paid, (Total_price - Isnull(paid,0) - Isnull(disc,0)) Balance
From tbl_invoice I Left join cte On I.client_id = cte.clientId
And I.invoice_id = cte.invoiceid
left join tbl_client C on C.client_id = I.client_id
group by I.client_id, invoice_Id, invoice_no, due_date, account_type, company_name, total_price, paid, disc
order by company_name
But it's not working as intended.
Instead of using a CTE, you can simply add a subquery to return the result of payments made and subtract that value from the total:
SQL Fiddle Demo
Schema Setup:
CREATE TABLE Invoice
([invoice_Id] int, [invoice_no] varchar(8), [client_id] int,
[date] datetime, [total_price] decimal(5,2));
INSERT INTO Invoice
([invoice_Id], [invoice_no], [client_id], [date], [total_price])
VALUES
(2, 'INV00001', 9, '2014-10-15 00:00:00', 200.00),
(7, 'INV00002', 9, '2014-10-16 00:00:00', 560.00),
(8, 'INV00003', 9, '2014-10-21 00:00:00', 100.00),
(11, 'INV00004', 9, '2014-10-27 00:00:00', 101.00);
CREATE TABLE Invoice_Payment
([InvPayment_id] int, [client_id] int, [Invoice_Id] int, [receipt_no] varchar(8),
[payment_date] datetime, [amount_received] decimal(5,2), [discount] int);
INSERT INTO Invoice_Payment
([InvPayment_id], [client_id], [Invoice_Id], [receipt_no], [payment_date],
[amount_received], [discount])
VALUES
(6, 9, 8, 'REC00002', '2014-10-31 00:00:00', 5.00, 0.00);
Query to generate output::
SELECT i.client_id , SUM(i.total_price) AS Total_price,
SUM(i.total_price) - ( SELECT SUM(ip.amount_received)
FROM dbo.Invoice_Payment ip
WHERE i.client_id = ip.client_id
) AS DueAmount
FROM dbo.Invoice i
WHERE client_id = 9
GROUP BY client_id
Results:
| CLIENT_ID | TOTAL_PRICE | DUEAMOUNT |
|-----------|-------------|-----------|
| 9 | 961 | 956 |
Please select the values you want. The problem is related to grouping.
In the group by clause I.client_id, invoice_Id, invoice_no, due_date, account_type, company_name, total_price, paid, disc this much fields are specified. invoice_Id, invoice_no prevent grouping which you need. Remove both invoice_Id and invoice_no from select and group by and try again.

Resources