Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Seconds()
Message
De
11/02/2019 06:30:47
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
 
À
10/02/2019 22:14:58
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Titre:
Versions des environnements
Visual FoxPro:
VFP 9
OS:
Windows 10
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Desktop
Divers
Thread ID:
01666213
Message ID:
01666253
Vues:
81
>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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform