1
0
mirror of https://github.com/Microsoft/sql-server-samples.git synced 2025-12-08 14:58:54 +00:00
Files
sql-server-samples/samples/demos/BelgradeProductCatalogDemo/sql-scripts/A1 fraud-detection.sql
Jovan Popovic 9d6ceffb2d Updates BelgradeDemo
Added number on sample sql files.
2016-10-27 16:02:25 -07:00

35 lines
933 B
Transact-SQL

USE ProductCatalogDemo
GO
with
history as (
select ProductID, Name, Price, DateModified,
LAG (Price, 1, 1) over (partition by ProductID order by DateModified) as PrevPrice,
LEAD (Price, 1, 1) over (partition by ProductID order by DateModified) as NextPrice
from dbo.Product for system_time all
),
spikes as (
select DateModified, ProductID, Name, PrevPrice, Price, NextPrice
from history
where PrevPrice = NextPrice
AND ABS(PrevPrice - Price)/PrevPrice >=0.5
),
logs as (
SELECT Page, [User], Time, Origin
FROM OPENROWSET(BULK N'c:\JSON\logANSI.txt',
FORMATFILE = 'c:\\JSON\ldjfmt.txt',
CODEPAGE = '65001') as log
CROSS APPLY
OPENJSON (log.log_entry)
WITH ( Page varchar(30), [User] varchar(20),Time datetime2, Origin varchar(20))
)
select Page, [User], Time, Origin, PrevPrice, Price, NextPrice
from logs JOIN spikes
on ABS(DATEDIFF(second, Time, spikes.DateModified)) < 3000
order by Time