本文介绍了SQL查询时间间隔。语法错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个看似简单的练习变成什么都不是的情况。
$ b

读数表包含多个用户的坐标和时间读数。所有我想在这个时间为每个用户连续组织它。

b $ b

  CREATE TABLE [读数](
user_id varchar(10),
reading_time int,
x decimal(10,2),$ b $十进制(10,2));


INSERT INTO读数值
('u1',60,345,400),
('u1',100,560,300),
('u2',35,1024,250),
('u1',90,450,450),
('u3',150,600,100),
(' u3',100,500,125);

我的错误代码

  SELECT r.user_id,r.reading_time start_time,rm.reading_time end_time,
(CONVERT(varchar,rx)+';'+ CONVERT(varchar,ry ))start_point,
(CONVERT(varchar,rm.x)+';'+ CONVERT(varchar,rm.y))end_point
FROM读数r
JOIN(SELECT TOP 1 r2。 user_id,r2.reading_time,r2.x,r2.y
FROM读数r2
WHERE r2.user_id = r.user_id和
r2.reading_time< r.reading_time
ORDER BY r2.reading time desc)rm
ON r.user_id = rm.user_id
ORDER BY 1,2 desc;

你能指出我的语法错误吗?


APPLY

  SELECT r.user_id,
r.reading_time start_time,
rm.reading_time end_time,$ b $ CONVERT(varchar(10),rm.x)+';'+ CONVERT(varchar(10),rx)+';'+ CONVERT(varchar(10),ry))start_point,$ b $ CONVERT(varchar(10),rm.y))end_point
FROM读数r
交叉应用

SELECT TOP 1 r2.user_id,r2.reading_time,r2.x, r2.y
FROM读数r2
WHERE r2.user_id = r.user_id和
r2.reading_time< r.reading_time
ORDER BY r2.reading_time desc
) rm
ORDER BY 1,2 desc;

请参阅


This is the case where a seemingly simple exercise turned to be nothing but. I couldn't even get my syntax straight.

Readings table contains coordinates and time readings for multiple users. All I want at this time to organize it as consecutive time intervals for each user.

Data:

CREATE TABLE [Readings] (
user_id varchar(10),
reading_time int,
x decimal(10,2),
y decimal(10,2) );


INSERT INTO Readings VALUES
('u1', 60, 345, 400),
('u1', 100, 560,300),
('u2', 35, 1024, 250),
('u1', 90, 450, 450),
('u3', 150, 600, 100),
('u3', 100, 500, 125);

My erroneous code:

SELECT r.user_id, r.reading_time start_time, rm.reading_time end_time,
       (CONVERT(varchar, r.x)+' ; '+CONVERT(varchar, r.y)) start_point,
       (CONVERT(varchar, rm.x)+' ; '+CONVERT(varchar, rm.y)) end_point
FROM Readings r
JOIN (SELECT TOP 1 r2.user_id, r2.reading_time, r2.x, r2.y
      FROM Readings r2
      WHERE r2.user_id=r.user_id and
            r2.reading_time < r.reading_time
      ORDER BY r2.reading time desc) rm
ON r.user_id=rm.user_id
ORDER BY 1,2 desc;

Can you please point out my syntax error(s)?

解决方案

Well part of your problem is you are referencing a table alias inside of a subquery, you can only do that if you are using an APPLY:

SELECT r.user_id,
  r.reading_time start_time,
  rm.reading_time end_time,
  (CONVERT(varchar(10), r.x)+' ; '+CONVERT(varchar(10), r.y)) start_point,
  (CONVERT(varchar(10), rm.x)+' ; '+CONVERT(varchar(10), rm.y)) end_point
FROM Readings r
CROSS APPLY
(
  SELECT TOP 1 r2.user_id, r2.reading_time, r2.x, r2.y
  FROM Readings r2
  WHERE r2.user_id=r.user_id and
      r2.reading_time < r.reading_time
  ORDER BY r2.reading_time desc
) rm
ORDER BY 1,2 desc;

See SQL Fiddle with Demo

这篇关于SQL查询时间间隔。语法错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 07:51