Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Seconds()
Message
From
11/02/2019 06:30:47
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Title:
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows 10
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Desktop
Miscellaneous
Thread ID:
01666213
Message ID:
01666253
Views:
80
>Thanks Sir Cetin Basoz for helping, your query work fine with sqlserver 2008.
>
>But I need little more help
>
>Query fills nSeconds in a sequence, I mean from First record to Last record.
>
>Now here I want fill this with batches like this
>
>
>CREATE CURSOR junk(id n(2),date t,batch n(4),nSeconds n(6))
>INSERT INTO junk values(1,{^2019-02-08 01:25:28 AM},1,	0)
>INSERT INTO junk values(2,{^2019-02-08 01:25:29 AM},1,	0)
>INSERT INTO junk values(3,{^2019-02-08 01:25:55 AM},1,	0)
>INSERT INTO junk values(4,{^2019-02-09 02:28:29 AM},2,	0)
>INSERT INTO junk values(5,{^2019-02-09 02:50:18 AM},2,	0)
>INSERT INTO junk values(6,{^2019-02-09 08:59:17 PM},2,	0)
>INSERT INTO junk values(7,{^2019-02-09 10:25:39 PM},2,	0)
>INSERT INTO junk values(8,{^2019-02-12 03:05:19 AM},5,	0)
>
>
>Need result like this
>
>
>
>
>Every new group has nSeconds=0, (shown in red font)
>Calculation is based on batch column.
>
>When batch changes then calculation must strart with Zero.
>
>Please

What is a "group"? I assume you mean a 'date' as a new group or batch (your original question output was not like that):
with tmp ([date], dGroup, rcNo) as 
(
  select [date],
  cast([date] as date),
  row_number() over (partition by cast([date] as date) order by [date])
  from sample
),
secs ([date], [seconds]) as
(
  select t1.[date], datediff(second,t2.[date], t1.[date])
  from tmp t1 
  left join tmp t2 on t1.dGroup = t2.dGroup and t1.rcNo = t2.rcNo+1
)
update sample
   set nSeconds = coalesce(secs.[seconds],0)
   from secs 
   where secs.[date] = sample.[date];
Demo here:
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=fcbcefd03deacf94dc1e0a8e089c8062

PS: Next time please be clear about what you are asking (and please do not use "junk" as a table name, it doesn't make any sense, does it?).
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform