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

Help with a query

Greetings,

I have the follow table structure:

CREATE TABLE [dbo].[Strategies_data] (
[Symbol] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[dateTraded] [smalldatetime] NOT NULL ,
[Strategy] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[Signal] [smallint] NOT NULL ,
[priceTrigger] [decimal](18, 5) NOT NULL
) ON [PRIMARY]

with some sample data:

Symbol dateTraded Strategy Signal priceTrigger
CSCO 8/5/2003 Reversal -1 19.32
CSCO 7/22/2003 Reversal 1 18.03
CSCO 7/5/2003 Reversal -1 18.30
CSCO 8/4/2003 Swing -1 19.20
CSCO 7/25/2003 Swing 1 18.50
INTC 8/6/2003 Reversal -1 25.30
INTC 7/27/2003 Reversal 1 24.40
INTC 7/22/2003 Swing 1 23.00
INTC 7/06/2003 Swing -1 23.95
I'd like to find the newest row for each symbol/strategy combination.
For example, from the above data, I need this result:

Symbol dateTraded Strategy Signal priceTrigger
CSCO 8/5/2003 Reversal -1 19.32
CSCO 8/4/2003 Swing -1 19.20
INTC 8/6/2003 Reversal -1 25.30
INTC 7/22/2003 Swing 1 23.00
Any help would be appreciated.

TIA
Jul 20 '05 #1
4 4113
Air coded...

SELECT Symbol, dateTraded, Strategy, Signal, priceTrigger
FROM Strategies_data S
INNER JOIN (
SELECT Symbol, MAX(dateTraded) AS DateTraded, Strategy
FROM Strategies_data
GROUP BY Symbol Strategy) T ON T.Symbol = S.Symbol AND T.dateTraded
= S.DateTraded AND T.Strategy = S.Strategy
"Tom Nunamaker" <to***********@randolph.af.mil> wrote in message
news:9a**************************@posting.google.c om...
Greetings,

I have the follow table structure:

CREATE TABLE [dbo].[Strategies_data] (
[Symbol] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[dateTraded] [smalldatetime] NOT NULL ,
[Strategy] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[Signal] [smallint] NOT NULL ,
[priceTrigger] [decimal](18, 5) NOT NULL
) ON [PRIMARY]

with some sample data:

Symbol dateTraded Strategy Signal priceTrigger
CSCO 8/5/2003 Reversal -1 19.32
CSCO 7/22/2003 Reversal 1 18.03
CSCO 7/5/2003 Reversal -1 18.30
CSCO 8/4/2003 Swing -1 19.20
CSCO 7/25/2003 Swing 1 18.50
INTC 8/6/2003 Reversal -1 25.30
INTC 7/27/2003 Reversal 1 24.40
INTC 7/22/2003 Swing 1 23.00
INTC 7/06/2003 Swing -1 23.95
I'd like to find the newest row for each symbol/strategy combination.
For example, from the above data, I need this result:

Symbol dateTraded Strategy Signal priceTrigger
CSCO 8/5/2003 Reversal -1 19.32
CSCO 8/4/2003 Swing -1 19.20
INTC 8/6/2003 Reversal -1 25.30
INTC 7/22/2003 Swing 1 23.00
Any help would be appreciated.

TIA

Jul 20 '05 #2
Fantastic. With only minor corrections, it worked perfectly. That's a nice
technique to use a sub-query as a pseudo table. I haven't done that in a long
time and that certainly did the trick. Here's the working query:

SELECT S.Symbol, S.dateTraded, S.Strategy, Signal, priceTrigger
FROM Strategies_data S INNER JOIN
(SELECT Symbol, MAX(dateTraded) AS DateTraded, Strategy
FROM Strategies_data
GROUP BY Symbol, Strategy) T ON T.Symbol = S.Symbol AND T.dateTraded =
S.DateTraded AND T.Strategy = S.Strategy

Thanks again!


In article <C3*******************@news20.bellglobal.com>, Yannick Turgeon
says...

Air coded...

SELECT Symbol, dateTraded, Strategy, Signal, priceTrigger
FROM Strategies_data S
INNER JOIN (
SELECT Symbol, MAX(dateTraded) AS DateTraded, Strategy
FROM Strategies_data
GROUP BY Symbol Strategy) T ON T.Symbol = S.Symbol AND T.dateTraded
= S.DateTraded AND T.Strategy = S.Strategy
"Tom Nunamaker" <to***********@randolph.af.mil> wrote in message
news:9a**************************@posting.google. com...
Greetings,

I have the follow table structure:

CREATE TABLE [dbo].[Strategies_data] (
[Symbol] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[dateTraded] [smalldatetime] NOT NULL ,
[Strategy] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[Signal] [smallint] NOT NULL ,
[priceTrigger] [decimal](18, 5) NOT NULL
) ON [PRIMARY]

with some sample data:

Symbol dateTraded Strategy Signal priceTrigger
CSCO 8/5/2003 Reversal -1 19.32
CSCO 7/22/2003 Reversal 1 18.03
CSCO 7/5/2003 Reversal -1 18.30
CSCO 8/4/2003 Swing -1 19.20
CSCO 7/25/2003 Swing 1 18.50
INTC 8/6/2003 Reversal -1 25.30
INTC 7/27/2003 Reversal 1 24.40
INTC 7/22/2003 Swing 1 23.00
INTC 7/06/2003 Swing -1 23.95
I'd like to find the newest row for each symbol/strategy combination.
For example, from the above data, I need this result:

Symbol dateTraded Strategy Signal priceTrigger
CSCO 8/5/2003 Reversal -1 19.32
CSCO 8/4/2003 Swing -1 19.20
INTC 8/6/2003 Reversal -1 25.30
INTC 7/22/2003 Swing 1 23.00
Any help would be appreciated.

TIA



Jul 20 '05 #3
Tom,

When I read your post it remembered me one of my first post in this group! I
just looked at it and I posted it exactly 3 years ago: August 6th, 2003! You
can read it here if you'd like:
http://groups.google.com/groups?selm...0SPAMiname.com

Glad it worked without too much changes. I've written it very quickly.
"Tom Nunamaker" <To********@newsguy.com> wrote in message
news:bg********@drn.newsguy.com...
Fantastic. With only minor corrections, it worked perfectly. That's a nice technique to use a sub-query as a pseudo table. I haven't done that in a long time and that certainly did the trick. Here's the working query:

SELECT S.Symbol, S.dateTraded, S.Strategy, Signal, priceTrigger
FROM Strategies_data S INNER JOIN
(SELECT Symbol, MAX(dateTraded) AS DateTraded, Strategy
FROM Strategies_data
GROUP BY Symbol, Strategy) T ON T.Symbol = S.Symbol AND T.dateTraded =
S.DateTraded AND T.Strategy = S.Strategy

Thanks again!

Jul 20 '05 #4
A somewhat different approach using a correlated subquery:

CREATE TABLE [dbo].[Strategies_data]
([Symbol] [varchar] (20) NOT NULL ,
[dateTraded] [smalldatetime] NOT NULL ,
[Strategy] [varchar] (50) NOT NULL ,
[Signal] [smallint] NOT NULL ,
[priceTrigger] [decimal](18, 5) NOT NULL )

insert strategies_data (symbol, datetraded,strategy,signal,pricetrigger)
values('CSCO' , '8/5/2003' , 'Reversal' , -1 , 19.32)
insert strategies_data (symbol, datetraded,strategy,signal,pricetrigger)
values('CSCO' , '7/22/2003' , 'Reversal' , 1 , 18.03)
insert strategies_data (symbol, datetraded,strategy,signal,pricetrigger)
values('CSCO' , '7/5/2003' , 'Reversal' , -1 , 18.30)
insert strategies_data (symbol, datetraded,strategy,signal,pricetrigger)
values('CSCO' , '8/4/2003' , 'Swing' , -1 , 19.20)
insert strategies_data (symbol, datetraded,strategy,signal,pricetrigger)
values('CSCO' , '7/25/2003' , 'Swing' , 1 , 18.50)
insert strategies_data (symbol, datetraded,strategy,signal,pricetrigger)
values('INTC' , '8/6/2003' , 'Reversal' , -1 , 25.30)
insert strategies_data (symbol, datetraded,strategy,signal,pricetrigger)
values('INTC' , '7/27/2003' , 'Reversal' , 1 , 24.40)
insert strategies_data (symbol, datetraded,strategy,signal,pricetrigger)
values('INTC' , '7/22/2003' , 'Swing' , 1 , 23.00)
insert strategies_data (symbol, datetraded,strategy,signal,pricetrigger)
values('INTC' , '7/06/2003' , 'Swing' , -1 , 23.95)

select sd.* from strategies_data sd
order by
sd.symbol, sd.strategy

gives us this:
Symbol dateTraded
Strategy Signal priceTrigger
-------------------- ------------------------------------------------------
-------------------------------------------------- ------ ------------------
--
CSCO 2003-08-05 00:00:00
Reversal -1 19.32000
CSCO 2003-07-22 00:00:00
Reversal 1 18.03000
CSCO 2003-07-05 00:00:00
Reversal -1 18.30000
CSCO 2003-08-04 00:00:00
Swing -1 19.20000
CSCO 2003-07-25 00:00:00
Swing 1 18.50000
INTC 2003-08-06 00:00:00
Reversal -1 25.30000
INTC 2003-07-27 00:00:00
Reversal 1 24.40000
INTC 2003-07-22 00:00:00
Swing 1 23.00000
INTC 2003-07-06 00:00:00
Swing -1 23.95000

and this:

select sd.*
from strategies_data sd
where sd.datetraded = (select max(datetraded)
from strategies_data sd1
where sd.symbol = sd1.symbol
and sd.strategy = sd1.strategy)
order by
sd.symbol, sd.strategy

give us this:

Symbol dateTraded
Strategy Signal priceTrigger
-------------------- ------------------------------------------------------
-------------------------------------------------- ------ ------------------
--
CSCO 2003-08-05 00:00:00
Reversal -1 19.32000
CSCO 2003-08-04 00:00:00
Swing -1 19.20000
INTC 2003-08-06 00:00:00
Reversal -1 25.30000
INTC 2003-07-22 00:00:00
Swing 1 23.00000

which I think is what you wanted to see.
"Tom Nunamaker" <to***********@randolph.af.mil> wrote in message
news:9a**************************@posting.google.c om...
Greetings,

I have the follow table structure:

CREATE TABLE [dbo].[Strategies_data] (
[Symbol] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[dateTraded] [smalldatetime] NOT NULL ,
[Strategy] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[Signal] [smallint] NOT NULL ,
[priceTrigger] [decimal](18, 5) NOT NULL
) ON [PRIMARY]

with some sample data:

Symbol dateTraded Strategy Signal priceTrigger
CSCO 8/5/2003 Reversal -1 19.32
CSCO 7/22/2003 Reversal 1 18.03
CSCO 7/5/2003 Reversal -1 18.30
CSCO 8/4/2003 Swing -1 19.20
CSCO 7/25/2003 Swing 1 18.50
INTC 8/6/2003 Reversal -1 25.30
INTC 7/27/2003 Reversal 1 24.40
INTC 7/22/2003 Swing 1 23.00
INTC 7/06/2003 Swing -1 23.95
I'd like to find the newest row for each symbol/strategy combination.
For example, from the above data, I need this result:

Symbol dateTraded Strategy Signal priceTrigger
CSCO 8/5/2003 Reversal -1 19.32
CSCO 8/4/2003 Swing -1 19.20
INTC 8/6/2003 Reversal -1 25.30
INTC 7/22/2003 Swing 1 23.00
Any help would be appreciated.

TIA

Jul 20 '05 #5

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

Similar topics

11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
5
by: Steve Patrick | last post by:
Hi All You guys are my last hope, despite spending money on books and hours reading them I still can not achieve the results I need. I have designed a database in Access 2000 based on 1 table,...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
3
by: mcmahonb | last post by:
Hey people... I've been searching this forum for a few hours and even though this topic has been went over from many different angles; I cannot seem to figure out how to make things work on my...
4
by: n | last post by:
Hello! Here is a problem I hope you can point me to a solution. It Problem: A teacher needs to know which lesson to teach. A school has a curriculum with 26 lessons, A-Z. For a given class,...
47
by: Jo | last post by:
Hi there, I'm Jo and it's the first time I've posted here. I'm in process of creating a database at work and have come a little unstuck.....I'm a bit of a novice and wondered if anyone could...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.