473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Complex Sql Query

I'm not sure if this is possible but what i want to do is as follows:
I need to

Select all values in a table that are smaller then the preceding one
timewise or ID wise. Which ever.
I have three columns. ID, timestamp and value.

I'm using SQL server 2000. Is it possible to do this withou transact
sql?

Thanks

Mark
Jul 20 '05 #1
5 13222
It helps others understand your requirements better if you post DDL and
sample INSERT statements. TIMESTAMP is a reserved word for the Timestamp
datatype but I will assume that what you mean in this case is a DATETIME
column. I've also assumed that the DATETIME column is unique.

When you say "without transact SQL" do you mean you want to avoid using
proprietary features?

Try this:

CREATE TABLE SomeTable (id INTEGER PRIMARY KEY, ts DATETIME NOT NULL UNIQUE,
value INTEGER NOT NULL)

INSERT INTO SomeTable (id,ts,value) VALUES (1,'2004-01-01T00:00:00',100)
INSERT INTO SomeTable (id,ts,value) VALUES (2,'2004-01-01T00:01:00',90)
INSERT INTO SomeTable (id,ts,value) VALUES (3,'2004-01-01T00:02:00',95)
INSERT INTO SomeTable (id,ts,value) VALUES (4,'2004-01-01T00:03:00',94)

SELECT T.id, T.ts, T.value
FROM SomeTable AS T
JOIN
(SELECT S1.value, MIN(S2.ts) AS ts
FROM SomeTable AS S1
JOIN SomeTable AS S2
ON S1.ts < S2.ts
GROUP BY S1.id, S1.value) AS S
ON S.ts = T.ts AND S.value > T.value

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2
That Actually worked! Except it was way to slow. Currently we have
about 5000 values and it took about 7-10 seconds to run that query.
That's definately unacceptable. Any better ways to go about it?

I'm still impressed at how that worked. Do you know of any site that
could explain to me how that worked. I guess the part i didn't really
understand was the join with the '<' operator.
Jul 20 '05 #3
This could be a little quicker:

SELECT T.id, T.ts, T.value
FROM SomeTable AS T
WHERE value <
(SELECT value
FROM Sometable
WHERE ts =
(SELECT MAX(ts)
FROM Sometable
WHERE ts<T.ts))
could explain to me how that worked. I guess the part i didn't really
understand was the join with the '<' operator.


The purpose of the self-join was just to join each row to the previous ones
(S1.ts < S2.ts) but in actual fact I was trying to be too clever!

--
David Portas
SQL Server MVP
--
Jul 20 '05 #4
Wow it worked great and fast. Thanks a lot. And i thought i knew all
about SQL queries

Mark
Jul 20 '05 #5
DROP TABLE Foobar;
CREATE TABLE Foobar
(clock_in DATETIME NOT NULL PRIMARY KEY,
val INTEGER NOT NULL);

INSERT INTO Foobar VALUES ('2004-01-01', 100)
INSERT INTO Foobar VALUES ('2004-01-02', 90)
INSERT INTO Foobar VALUES ('2004-01-03', 95)
INSERT INTO Foobar VALUES ('2004-01-04', 94)
INSERT INTO Foobar VALUES ('2004-01-05', 101)
INSERT INTO Foobar VALUES ('2004-01-06', 97)
INSERT INTO Foobar VALUES ('2004-01-07', 95)
INSERT INTO Foobar VALUES ('2004-01-08', 94)

SELECT F1.clock_in, F1.val,
SUM (CASE WHEN F2.val < F1.val THEN 1 ELSE 0 END) AS future_lesser_vals
FROM Foobar AS F1, Foobar AS F2
WHERE F2.clock_in > F1.clock_in
GROUP BY F1.clock_in, F1.val;
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Starbuck | last post by:
OK, first let me say that I am no DB person. But I have a user here who keeps getting this error whenever she does, whatever it is she does, with databases... A google search takes me to...
2
by: Mikel | last post by:
I am trying to get around the problem "The expression you have entered is too complex" for a select query. (The example below is not the expression that is giving me headaches.) So I am thinking...
4
by: ED | last post by:
I am attempting to to write a query that has a numerous nested IIf statements. The problem that I am having is that it is to long of a query to be built in design mode and when I build it in sql...
8
by: Matt | last post by:
Hi all, Thank you for taking the time. I have a database with 45 tables on it. 44 tables are linked to a main table through a one to one relationship. My question is, is there no way i can...
2
by: Ben de Vette | last post by:
Hi, I'm using the querybuilder when updating a record in a table (Access). However, I get a "Query is too complex" message. The Primary key is autonumbered. Why is it making such a complex...
1
by: arun | last post by:
Query is too complex -------------------------------------------------------------------------------- Hi, I was trying to solve this problem since last two days but couldn't find any solution. ...
1
by: Randy Volkart | last post by:
I'm trying to fix a glitch in a complex access database, and have a fairly complex problem... unless there's some obscure easy fix I don't know being fairly new with Access. Basically, the area...
19
by: kawaks40 | last post by:
Hi everyone :) I just recently started using access/sql. and right away I ran into this problem "SQL expression too complex" I google'd a lot on what it means, and the only workaround I've...
3
by: Eric Davidson | last post by:
DB2 9.5 I keep geting the message. SQL0101N The statement is too long or too complex. SQLSTATE=54001 When one of my sql statements takes over 60 seconds to compile the sql statement. Is...
0
crystal2005
by: crystal2005 | last post by:
Hi, I am having trouble with some complex SQL queries. I’ve got winestore database, taken from Web Database Application with PHP and MySQL book. And some question about queries as the following ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.