Can anyone help me to calculate the time difference(in hh:mm) between the two rows, when the Procs_ID changes from 01 to another Procs_ID.
Procs_ID meter_id date
01 0000012 2015-10-12 09:07:22.530
03 0000013 2015-10-12 09:11:51.733
01 0000014 2015-10-12 09:12:38.550
02 0000015 2015-10-12 10:38:52.923
03 0000016 2015-10-12 10:40:33.467
01 0000017 2015-10-12 10:40:56.013
Thanks in advance
Here is an example with LEAD which is avaliable for SQL Server 2012+
select
*,
case
when Procs_Id = '01'
and lead(Procs_ID) over (order by [date]) <> Procs_ID then datediff(mi,[date],lead([date]) over (order by [date]))
end as TimeDiffInMin
from YourTable
Related
I have made the amendments as per comments and entered here as not enough characters allowed in comments. I think there may be a few issues on the OVER and with the JOIN. Updated query:
SELECT RIGHT('0' + CAST(day(oh_datetime) AS VARCHAR (2)), 2),
SUM(SUM((CASE oh_sot_id WHEN 1 THEN 1 WHEN 4 THEN -1 END) * oht_net)) over (ORDER BY day(oh_datetime) ROWS UNBOUNDED PRECEDING) AS 'Orders In($)',
SUM((CASE oh_cd_id WHEN 11728 THEN 1 END) * oht_net) AS 'Target($)',
SUM(SUM((CASE ih_credit WHEN 'false' THEN 1 WHEN 'true' THEN -1 END) * ih_net)) over (ORDER BY day(ih_datetime) ROWS UNBOUNDED PRECEDING) AS 'Sales($)'
FROM order_header_total
JOIN order_header ON order_header_total.oht_oh_id = order_header.oh_id
JOIN invoice_header ON order_header.oh_id = invoice_header.ih_oh_id
WHERE oh_datetime >= DATEADD(day, 1, EOMONTH(GETDATE(), -1)) AND oh_datetime < DATEADD(day, 1, EOMONTH(GETDATE()))
GROUP BY day(oh_datetime), day(ih_datetime)
UNION SELECT 'YAxis','0','0','0'
Table should display:
Date
Orders In
Target
Sales
01
6402.19
12321.57
128539.13
02
17795.94
24643.14
148258.63
03
50703.09
36964.71
171231.14
04
116034.29
49286.28
188157.69
28
353989.36
345004.00
446808.05
I have not done the full month for sake of space, but included the last day to show how it should accumulate. Instead it looks like this:
Date
Orders In
Target
Sales
01
5507.32
5507.32
01
5833.52
5833.52
01
6402.19
29377.18
02
10312.35
188157.69
02
16592.09
16023.42
02
17795.94
54950.68
03
30439.28
28666.76
03
30581.03
28808.51
04
36029.72
24700.77
04
37082.36
38767.55
23
144191.70
143992.45
Again a selection of data and then showing the last row for the sake of space. The date is not grouping, 'Orders In' and 'Sales' are not accumulating, and there is no data displaying for 'Target'.
I have a table called Transaction. In that a column Time with TimeStamp datatype is found.
So the data will be looking like 2015-01-17 08:12:48.000
I want to display like 8 am
For example
`2015-01-17 08:12:48.000` `8 AM`
`2015-01-17 14:12:48.000` `2 PM`
now i got the result like above. This is my result
Hour
----
01 PM
02 PM
04 PM
05 PM
06 PM
07 AM
07 PM
08 AM
09 AM
10 AM
11 AM
12 PM
This is the query for above result.
SELECT
FORMAT(CAST(Time as datetime),'hh tt') hour,
COUNT(TransactionNumber) Total_Transaction,
SUM(Total) salesCost
FROM
[HQMatajer].[dbo].[Transaction]
WHERE
StoreID = '1001'
AND YEAR(Time) = '2015'
AND MONTH(Time) = '01'
AND DAY(Time) = '15'
GROUP BY
FORMAT(CAST(Time as datetime),'hh tt')`
Now I want to sort the hours. It should display like
07 AM
08 AM
09 AM
10 AM
11 AM
12 PM
01 PM
02 PM
.
.
07 PM
Thanks
Try adding this to the end of your statement:
, convert(varchar(2), [time], 8)
order by convert(varchar(2), [time], 8)
Resulting in this:
SELECT
FORMAT(CAST(Time as datetime),'hh tt') hour,
COUNT(TransactionNumber) Total_Transaction,
SUM(Total) salesCost
FROM
[HQMatajer].[dbo].[Transaction]
WHERE
StoreID = '1001'
AND YEAR(Time) = '2015'
AND MONTH(Time) = '01'
AND DAY(Time) = '15'
GROUP BY
FORMAT(CAST(Time as datetime),'hh tt')
, convert(varchar(2), [time], 8)
order by convert(varchar(2), [time], 8)
convert(varchar(2),[time],8) returns the datetime with style 8 in the following format: hh:mi:ss, and using varchar(2) truncates it to hh.
Documentation for convert and styles.
As Shakeer Mirza posted, using datepart() works as well.
Documentation for datepart.
Use DATEPART function simply
Select DATEPART(HH, YOUR_DATETIME_COL) AS HR, ......
........ --Write your Statements
........
ORDER BY HR
DATEPART will give result in integer format. So the Order by will give exact order
I cannot seem to work this one out to be exactly what need.
I'm using MS SQL Management Studio 2008.
I have a table (several actually) but lets keep it simple. The table contains daily stock figures for each item (SKU).
SKU DataDate Web_qty
2 2014-11-17 00:00:00 404
2 2014-11-18 00:00:00 373
2 2014-11-19 00:00:00 1350
66 2014-11-17 00:00:00 3624
66 2014-11-18 00:00:00 3576
66 2014-11-19 00:00:00 3570
67 2014-11-17 00:00:00 9353
67 2014-11-18 00:00:00 9297
67 2014-11-19 00:00:00 9250
I simply need the Select Query to return this:
SKU DataDate Difference
2 2014-11-17 00:00:00 ---
2 2014-11-18 00:00:00 -31
2 2014-11-19 00:00:00 +977
66 2014-11-17 00:00:00 ---
66 2014-11-18 00:00:00 -48
66 2014-11-19 00:00:00 -6
67 2014-11-17 00:00:00 ---
67 2014-11-18 00:00:00 -56
67 2014-11-19 00:00:00 -47
I do not need the --- parts, I have just shown that to draw attention to the fact that this one cannot be calculated as it is the first record.
I've tried using derived tables, but its getting a little confusing, i need to play with a working example so I can understand it better.
If someone could point me in the right direction I'm sure I'll be able to join the other tables back together (i.e. SKU Description and prices).
Really appreciate everyone's time
Kev
Try this. Use correlated sub-query to find rolling difference
CREATE TABLE #tem
(SKU INT,DataDate DATETIME,Web_qty INT)
INSERT #tem
VALUES( 2,'2014-11-17 00:00:00',404),
(2,'2014-11-18 00:00:00',373),
(2,'2014-11-19 00:00:00',1350),
(66,'2014-11-17 00:00:00',3624),
(66,'2014-11-18 00:00:00',3576),
(66,'2014-11-19 00:00:00',3570),
(67,'2014-11-17 00:00:00',9353),
(67,'2014-11-18 00:00:00',9297),
(67,'2014-11-19 00:00:00',9250)
SELECT *,
Web_qty - (SELECT Web_qty
FROM #tem a
WHERE a.sku = b.SKU
AND a.DataDate = Dateadd(dd, -1, b.DataDate)) Roll_diff
FROM #tem b
I know it is an old thread but I happened to have a similar problem and I ended up solving it with Window functions. It works in SQL 2014 but not sure about 2008.
It also solves the problem of potentially non-continuous data as well as rows with no changes. Hopefully it helps someone out there!
CREATE TABLE #tem
(SKU INT,DataDate DATETIME,Web_qty INT)
INSERT #tem
VALUES( 2,'2014-11-17 00:00:00',404),
(2,'2014-11-18 00:00:00',373),
(2,'2014-11-19 00:00:00',1350),
(2,'2014-11-20 00:00:00',1350),
(2,'2014-11-21 00:00:00',1350),
(66,'2014-11-17 00:00:00',3624),
(66,'2014-11-18 00:00:00',3576),
(66,'2014-11-19 00:00:00',3570),
(66,'2014-11-20 00:00:00',3590),
(66,'2014-11-21 00:00:00',3578),
(67,'2014-11-17 00:00:00',9353),
(67,'2014-11-18 00:00:00',9297),
(67,'2014-11-19 00:00:00',9250),
(67,'2014-11-20 00:00:00',9250),
(67,'2014-11-21 00:00:00',9240)
;WITH A AS (
SELECT
SKU,
DataDate,
Web_Qty,
Web_qty - LAG(Web_qty,1, 0)
OVER (PARTITION BY SKU ORDER BY DataDate) Roll_diff
FROM #tem b
)
SELECT
SKU,
DataDate ValidFromDate,
Lead(DataDate, 1, DateFromParts(9999,12,31)) OVER (PARTITION BY SKU ORDER BY DataDate) ValidToDate,
Web_Qty
FROM A WHERE Roll_diff <> 0
I am having an issue and I have been working on it for the past three hours or so and have not found a solution. Running on a SQL Server platform. I have a single table that looks like this.
PT ITM VAL
-- --- ---
01 01 A
01 02 B
01 03 C
02 01 A
02 03 C
03 01 A
03 02 B
I am trying to find which PTs are missing the item numbers.
In the case above PT02 and PT03 are missing two item. This is the base what where I started, but I am not sure if I am even on the right track.
select t.PT,t.ITM
FROM MYTABLE t
GROUP BY t.PT,t.ITM
HAVING COUNT(*) > 1
Thanks
jlimited
If you're expecting 3 ITM per PT, the query would be
select PT
FROM MYTABLE
GROUP BY PT
HAVING COUNT(ITM) < 3
for other conditions a more complicated query is required.
Here was the solution that worked. I had to select a VAL that was populated, to find the values that were not.
select stg.PT,COUNT(stg.ITM) AS ITM_CNT
FROM MYTABLE stg
WHERE stg.ITM IS NOT NULL
AND stg.VAL IN (11)
GROUP BY stg.PT
HAVING COUNT(stg.ITM) > 1
EXCEPT
select stg.PT,COUNT(stg.ITM) AS ITM_CNT
FROM MYTABLE stg
WHERE stg.ITM IS NOT NULL
AND stg.VLA IN (4,5)
GROUP BY stg.PT
HAVING COUNT(stg.ITM) > 1
I have the following SQL Server 2008 table:
ID Destination Last Result
01 (555) 319-5170 Disconnect
02 (555) 319-5170 Duplicate
03 (555) 319-5170 Duplicate
04 (555) 261-5000 Duplicate
05 (555) 261-5000 Duplicate
06 (555) 261-7325 Busy
07 (555) 261-7325 Duplicate
08 (555) 261-7345 No Answer
09 (555) 261-7345 Duplicate
My goal is to dynamically set the 'Last Result' column for all matching phone numbers. So, in the example table above for Destination((555) 319-5170), the result for all matching phone numbers(555) 319-5170) with the value 'Disconnect'. The sample update statement is below:
UPDATE C
SET [Last Result] = (
SELECT Destination
FROM Call
WHERE [Last Result] NOT IN ('Duplicate','No Phone #')
GROUP BY Destination
HAVING COUNT(Destination) > 1)
FROM Call C
INNER JOIN
(
SELECT Destination
FROM Call
WHERE [Last Result] NOT IN ('Duplicate','No Phone #')
GROUP BY Destination
HAVING COUNT(Destination) > 1
) Dup ON Dup.Destination = C.Destination
WHERE C.[Last Result] NOT IN ('Duplicate','No Phone #')
But I am getting an error:
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
**EXPECTED OUTPUT**:
ID Destination Last Result
01 (555) 319-5170 Disconnect
02 (555) 319-5170 Disconnect
03 (555) 319-5170 Disconnect
04 (555) 261-5000 Duplicate
05 (555) 261-5000 Duplicate
06 (555) 261-7325 Busy
07 (555) 261-7325 Busy
08 (555) 261-7345 No Answer
09 (555) 261-7345 No Answer
Thanks in advance for any assistance with a possible solution.
You can look up the first Last Result with the row_number() window function. However, there has to be a way to define "first". In the example, I'm assuming you have an identity column:
update t1
set [Last Result] = t2.[Last Result]
from Table1 t1
join (
select row_number() over (partition by Destination
order by Id) as rn
, *
from Table1
) t2
on t1.Destination = t2.Destination
and t2.rn = 1;
Example at SQL Fiddle.
If you do not have an identity column, please update the question with how you define "first". There could be a CreateDt column for example.