-3

这是我的桌子

Create table gpscli
(
   cliente int,/*this is my id client*/
   inicio datetime,/*this is start time*/
   fin datetime,/this is finished time**/
   fecha datetime/*this is my date because inicio and fin could be a bad date*/
)

填满的表是这样的

select cliente, inicio, fin, fecha from gpscli order by fecha, cliente

1 '23-04-2012 10:23:51' '23-04-2012 10:26:38' '23-04-2012 00:00:000'
2 '23-04-2012 10:28:41' '23-04-2012 10:30:12' '23-04-2012 00:00:000'
3 '23-04-2012 10:33:58' '23-04-2012 10:37:24' '23-04-2012 00:00:000'
4 '23-04-2012 10:40:42' '23-04-2012 10:43:12' '23-04-2012 00:00:000'
5 '23-04-2012 10:45:46' '23-04-2012 10:57:18' '23-04-2012 00:00:000'
1 '24-04-2012 10:23:12' '24-04-2012 10:26:28' '24-04-2012 00:00:000'
2 '24-04-2012 10:23:29' '24-04-2012 10:26:58' '24-04-2012 00:00:000'
3 '24-04-2012 10:23:23' '24-04-2012 10:26:56' '24-04-2012 00:00:000'
4 '24-04-2012 10:23:12' '24-04-2012 10:26:28' '24-04-2012 00:00:000'
5 '24-04-2012 10:23:29' '24-04-2012 10:26:58' '24-04-2012 00:00:000'
1 '24-05-2012 10:23:12' '24-05-2012 10:26:28' '24-05-2012 00:00:000'
2 '24-05-2012 10:23:29' '24-05-2012 10:26:58' '24-05-2012 00:00:000'
3 '24-05-2012 10:23:23' '24-05-2012 10:26:56' '24-05-2012 00:00:000'
4 '24-05-2012 10:23:12' '24-05-2012 10:26:28' '24-05-2012 00:00:000'
5 '24-05-2012 10:23:29' '24-05-2012 10:26:58' '24-05-2012 00:00:000'

此信息由用户保存,此时将称为“主管”

主管是在商店里去商店询问客户想要什么产品的人。

所以他(主管)在车里移动。当他打开一个窗口(在 pockec pc 中)时,“inicio”被保存,当主管关闭窗口时,“fin”被保存在数据库中。

现在有了这些信息,我可以得到主管与客户相处的时间,但我真正需要的是主管在客户之间移动的时间。

不是客户 1 是一天中第一个客户所必需的,第一个客户是在inicio列上具有最小值的客户。与 fin 列的最后一个客户端相同。

主管在客户之间转移需要多长时间。

我需要一些东西

  • 对于第一个客户端传输时间为 0。
  • 对于第二个客户,传输时间是inicio(此inicio列是当前的)- fin(这fin是以前的客户)
  • 对于第三个客户,传输时间是inicio(这个 inicio 列是当前的)- fin(这fin是以前的客户)

最后我需要group by month,client

我不知道如何获得它(不使用循环,我不想使用它)

4

1 回答 1

0

我可以解决

create table #datos
(
cliente int,
fecha datetime,
inicio datetime,
fin datetime,
id int identity
)


insert into #datos (cliente,fecha,inicio,fin)
select cliente,fecha,  isnull(t_inicio,'01/01/1900') inicio,isnull(t_fin,'01/01/1900') fin
from gpscli order by  fecha, t_inicio,t_fin


alter table #datos
add idmas1 int

update #datos set idmas1=id+1


select d.cliente, d.fecha, d.inicio, d.fin,d.id,d2.fin fin_anterior from #datos d
inner join #datos d2 on
d.id=d2.idmas1

现在我有一个名为“fin_anterior”的完成时间,现在我可以按客户、月份或必要的方式分组

于 2012-11-26T19:15:46.957 回答