473,386 Members | 1,842 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,386 software developers and data experts.

query problem

Hi.

I have a table containing:

id, date, open, close, high, low, volumen, share_id

The table is used for daily stock information. I want to make a query that
count rows where open, close, high,low, volumen, share_id are the same for
the last 2 days that are in the date field.

example:

id | date | open| close | high | low | volumen | share_id
1 | 2005-03-22 | 61.5 | 61 | 61.5 | 61 | 12369 | 1
283 | 2005-03-23 | 61 | 61 | 61.5 | 61 | 29141 | 1
565 | 2005-03-24 | 61 | 61 | 61.5 | 61 | 29141 | 1
In this case i want a count so i know that there are 1 (or more) rows that
are identical with the day before.

This will be used to determine if the last day was a nonstockday. If 99% of
the rows with the highest date is identical with the secound highest date it
is properly a nonstockday.

I run mysql 4.0.23 so i cant use the subquery. I really dont have any idea
how to solve this. I cant even get a query to return the secound highest
date to work. I tried:

SELECT max(date) from daily_rate_of_exchange_1
where date <= DATE_ADD(max(date),interval -1 day);

but get : Invalid use of group function.

If i insert a date instead of the max(date) in the DATE_ADD function it
works. But i need it to be dynamic.

As you can tell im new to mysql queries, but would be thankfull if someone
would post an idea, or point in a direction where the anwser might be.

Thanks

-D Kjærgaard

Jul 23 '05 #1
5 1217
On 29/03/2005, Danny Kjærgaard wrote:
The table is used for daily stock information. I want to make a query
that count rows where open, close, high,low, volumen, share_id are
the same for the last 2 days that are in the date field.
[...]
In this case i want a count so i know that there are 1 (or more) rows
that are identical with the day before.

This will be used to determine if the last day was a nonstockday. If
99% of the rows with the highest date is identical with the secound
highest date it is properly a nonstockday.


With

mysql> select * from stockinfo;
+----+------------+------+-------+--------+
| id | d | open | close | volume |
+----+------------+------+-------+--------+
| 1 | 2005-03-25 | 10 | 11 | 1000 |
| 2 | 2005-03-26 | 10 | 11 | 1000 |
| 3 | 2005-03-27 | 10 | 11 | 1000 |
| 4 | 2005-03-28 | 12 | 14 | 1100 |
+----+------------+------+-------+--------+
4 rows in set (0.01 sec)

the query

mysql> SELECT
-> s1.d AS DATE,
-> IF(s1.open = s2.open AND
-> s1.close = s2.close AND
-> s1.volume = s2.volume, 'NO', 'YES') AS STOCKDAY
-> FROM stockinfo AS s1
-> LEFT JOIN stockinfo AS s2 ON DATEDIFF(s1.d, s2.d) = 1;

gives the following result:

+------------+----------+
| DATE | STOCKDAY |
+------------+----------+
| 2005-03-25 | YES |
| 2005-03-26 | NO |
| 2005-03-27 | NO |
| 2005-03-28 | YES |
+------------+----------+
4 rows in set (0.00 sec)

Is this what you want?
--
felix
Jul 23 '05 #2
Well.

I dont work over a weekend. With the DATEDIFF =1. And i would like i to
return a count (like 700 out of 750 returned as a nonstockday, thats 93%
that were like the last stockday and it is likely a nonstockday then).

But its nice and i got some input for a way to work this out.

Thanks.

"Felix Geerinckx" <fe*************@gmail.com> wrote in message
news:xn***************@news.easynews.com...
On 29/03/2005, Danny Kjærgaard wrote:
The table is used for daily stock information. I want to make a query
that count rows where open, close, high,low, volumen, share_id are
the same for the last 2 days that are in the date field.
[...]
In this case i want a count so i know that there are 1 (or more) rows
that are identical with the day before.

This will be used to determine if the last day was a nonstockday. If
99% of the rows with the highest date is identical with the secound
highest date it is properly a nonstockday.


With

mysql> select * from stockinfo;
+----+------------+------+-------+--------+
| id | d | open | close | volume |
+----+------------+------+-------+--------+
| 1 | 2005-03-25 | 10 | 11 | 1000 |
| 2 | 2005-03-26 | 10 | 11 | 1000 |
| 3 | 2005-03-27 | 10 | 11 | 1000 |
| 4 | 2005-03-28 | 12 | 14 | 1100 |
+----+------------+------+-------+--------+
4 rows in set (0.01 sec)

the query

mysql> SELECT
-> s1.d AS DATE,
-> IF(s1.open = s2.open AND
-> s1.close = s2.close AND
-> s1.volume = s2.volume, 'NO', 'YES') AS STOCKDAY
-> FROM stockinfo AS s1
-> LEFT JOIN stockinfo AS s2 ON DATEDIFF(s1.d, s2.d) = 1;

gives the following result:

+------------+----------+
| DATE | STOCKDAY |
+------------+----------+
| 2005-03-25 | YES |
| 2005-03-26 | NO |
| 2005-03-27 | NO |
| 2005-03-28 | YES |
+------------+----------+
4 rows in set (0.00 sec)

Is this what you want?
--
felix

Jul 23 '05 #3
DATEDIFF() was added in MySQL 4.1.1.

So i dont even work on my version.

"Danny Kjærgaard" <tu****@GeilerHiFi.dk> wrote in message
news:42***********************@dreader2.cybercity. dk...
Well.

I dont work over a weekend. With the DATEDIFF =1. And i would like i to
return a count (like 700 out of 750 returned as a nonstockday, thats 93%
that were like the last stockday and it is likely a nonstockday then).

But its nice and i got some input for a way to work this out.

Thanks.

"Felix Geerinckx" <fe*************@gmail.com> wrote in message
news:xn***************@news.easynews.com...
On 29/03/2005, Danny Kjærgaard wrote:
The table is used for daily stock information. I want to make a query
that count rows where open, close, high,low, volumen, share_id are
the same for the last 2 days that are in the date field.
[...]
In this case i want a count so i know that there are 1 (or more) rows
that are identical with the day before.

This will be used to determine if the last day was a nonstockday. If
99% of the rows with the highest date is identical with the secound
highest date it is properly a nonstockday.


With

mysql> select * from stockinfo;
+----+------------+------+-------+--------+
| id | d | open | close | volume |
+----+------------+------+-------+--------+
| 1 | 2005-03-25 | 10 | 11 | 1000 |
| 2 | 2005-03-26 | 10 | 11 | 1000 |
| 3 | 2005-03-27 | 10 | 11 | 1000 |
| 4 | 2005-03-28 | 12 | 14 | 1100 |
+----+------------+------+-------+--------+
4 rows in set (0.01 sec)

the query

mysql> SELECT
-> s1.d AS DATE,
-> IF(s1.open = s2.open AND
-> s1.close = s2.close AND
-> s1.volume = s2.volume, 'NO', 'YES') AS STOCKDAY
-> FROM stockinfo AS s1
-> LEFT JOIN stockinfo AS s2 ON DATEDIFF(s1.d, s2.d) = 1;

gives the following result:

+------------+----------+
| DATE | STOCKDAY |
+------------+----------+
| 2005-03-25 | YES |
| 2005-03-26 | NO |
| 2005-03-27 | NO |
| 2005-03-28 | YES |
+------------+----------+
4 rows in set (0.00 sec)

Is this what you want?
--
felix


Jul 23 '05 #4
I just upgraded the sql server to 4.1.10 so i can do subqueries and the
datediff now works. Just an information if someone feels like posting a way.

Thanks.

-Danny

"Danny Kjærgaard" <tu****@GeilerHiFi.dk> wrote in message
news:42***********************@dreader2.cybercity. dk...
DATEDIFF() was added in MySQL 4.1.1.

So i dont even work on my version.

"Danny Kjærgaard" <tu****@GeilerHiFi.dk> wrote in message
news:42***********************@dreader2.cybercity. dk...
Well.

I dont work over a weekend. With the DATEDIFF =1. And i would like i to
return a count (like 700 out of 750 returned as a nonstockday, thats 93%
that were like the last stockday and it is likely a nonstockday then).

But its nice and i got some input for a way to work this out.

Thanks.

"Felix Geerinckx" <fe*************@gmail.com> wrote in message
news:xn***************@news.easynews.com...
On 29/03/2005, Danny Kjærgaard wrote:

> The table is used for daily stock information. I want to make a query > that count rows where open, close, high,low, volumen, share_id are
> the same for the last 2 days that are in the date field.
> [...]
> In this case i want a count so i know that there are 1 (or more) rows > that are identical with the day before.
>
> This will be used to determine if the last day was a nonstockday. If
> 99% of the rows with the highest date is identical with the secound
> highest date it is properly a nonstockday.

With

mysql> select * from stockinfo;
+----+------------+------+-------+--------+
| id | d | open | close | volume |
+----+------------+------+-------+--------+
| 1 | 2005-03-25 | 10 | 11 | 1000 |
| 2 | 2005-03-26 | 10 | 11 | 1000 |
| 3 | 2005-03-27 | 10 | 11 | 1000 |
| 4 | 2005-03-28 | 12 | 14 | 1100 |
+----+------------+------+-------+--------+
4 rows in set (0.01 sec)

the query

mysql> SELECT
-> s1.d AS DATE,
-> IF(s1.open = s2.open AND
-> s1.close = s2.close AND
-> s1.volume = s2.volume, 'NO', 'YES') AS STOCKDAY
-> FROM stockinfo AS s1
-> LEFT JOIN stockinfo AS s2 ON DATEDIFF(s1.d, s2.d) = 1;

gives the following result:

+------------+----------+
| DATE | STOCKDAY |
+------------+----------+
| 2005-03-25 | YES |
| 2005-03-26 | NO |
| 2005-03-27 | NO |
| 2005-03-28 | YES |
+------------+----------+
4 rows in set (0.00 sec)

Is this what you want?
--
felix



Jul 23 '05 #5
On 29/03/2005, Danny Kjærgaard wrote:
I dont work over a weekend. With the DATEDIFF =1.
I don't understand what this has to do with the solution.
And i would like i to return a count (like 700 out of 750 returned as
a nonstockday, thats 93% that were like the last stockday and it is
likely a nonstockday then).


Then use the following query (I assume at most 1 entry per stock per
day):

SELECT
s1.d AS DATE,
SUM(IF(s1.open = s2.open AND
s1.close = s2.close AND
s1.volume = s2.volume, 1, 0)) /
(SELECT COUNT(id) FROM stockinfo WHERE d = s1.d) AS COUNT
FROM stockinfo AS s1
LEFT JOIN stockinfo AS s2 ON DATEDIFF(s1.d, s2.d) = 1
GROUP BY DATE

If the number of stocks is fixed, you can use this fixed number instead
of the SELECT COUNT(id) ...

If you don't have DATEDIFF, you can use TO_DAYS(date1) - TO_DAYS(date2)

--
felix
Jul 23 '05 #6

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

Similar topics

13
by: Wescotte | last post by:
Here is a small sample program I wrote in PHP (running off Apache 1.3.31 w/ PHP 5.0.1) to help illustrates problem I'm having. The data base is using DB2 V5R3M0. The client is WinXP machine using...
3
by: Brian Oster | last post by:
After applying security patch MS03-031 (Sql server ver 8.00.818) a query that used to execute in under 2 seconds, now takes over 8 Minutes to complete. Any ideas on what the heck might be going...
1
by: Jeff Blee | last post by:
I hope someone can help me get this graph outputing in proper order. After help from Tom, I got a graph to display output from the previous 12 months and include the average of that output all in...
8
by: Adam Louis | last post by:
I would like help resolving this problem. I'm a novice who's been hired to query a hospital database and extract useful information, available to me only in a dynamically generated, downloadable...
6
by: Martin Lacoste | last post by:
Ok, before I headbutt the computer... don't know why when I add criteria in a query, I get an 'invalid procedure call'. I also don't know why after searching the help in access, the various access...
4
by: Apple | last post by:
1. I want to create an autonumber, my requirement is : 2005/0001 (Year/autonumber), which year & autonumber no. both can auto run. 2. I had create a query by making relation to a table & query,...
11
by: Andy_Khosravi | last post by:
My problem: I'm having trouble with a query taking much too long to run; a query without any criteria evaluating only 650 records takes over 300 seconds to run (over the network. On local drive...
4
by: Konrad Hammerer | last post by:
Hi! I have the following problem: I have a query (a) using another query (b) to get the amount of records of this other query (b), means: select count(MNR) as Number from...
4
by: Stan | last post by:
I am using MS Office Access 2003 (11.5614). My basic question is can I run a query of a query datasheet. I want to use more that one criteria and can not get that query to work. I thought I...
2
by: existential.philosophy | last post by:
This is a new problem for me: I have some queries that open very slowly in design view. My benchmark query takes about 20 minutes to open in design view. That same query takes about 20 minutes...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.