473,406 Members | 2,356 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,406 software developers and data experts.

SELECT MAX statement

The database has four tables (-> = fields):

Account -> AccountID, ClientID, (others)
Security -> SecurityID, AccountID, (others)
SecurityTransactions -> SecurityID, Shares, (others)
SecurityPrices -> SecurityID, PriceDate, Price, (others)

Account and Security are connected by the AccountID key. Security,
SecurityTransactions, and SecurityPrices are connected by the SecurityID
key.

I'm looking to write a SQL statement that will supply the total value of a
client's holdings on a particular day -- as simply as possible.

I've achieved the result using a series of views, but it's slow. I was
hoping someone would help me out with a SQL statement something like this:

SELECT SUM(SecurityTransactions.Shares *
(SELECT MAX(PriceDate) FROM SecurityPrices x WHERE x.PriceDate <=
'4/2/2004' AND x.SecurityID = SecurityPrices.SecurityID) AS
LastSecurityPrice) AS TotalValue
FROM SecurityTransactions INNER JOIN
Account ON
SecurityTransactions.AccountID = Account.AccountID INNER JOIN
SecurityPrices ON
SecurityTransactions.SecurityID = SecurityPrices.SecurityID
WHERE (Accounts.ClientID = 1)

This statement fails at "AS".

Thanks for any help!

--Brent
Jul 20 '05 #1
2 4880
Hi

As you don't post real DDL we don't know what the indexes PKs etc are. You
may want to see what the Index Tuning Wizard suggests or view the query
plan.

John

"Brent" <bbigler @ ahem-'whoopee' . com> wrote in message
news:M0*****************@newssvr25.news.prodigy.co m...
The database has four tables (-> = fields):

Account -> AccountID, ClientID, (others)
Security -> SecurityID, AccountID, (others)
SecurityTransactions -> SecurityID, Shares, (others)
SecurityPrices -> SecurityID, PriceDate, Price, (others)

Account and Security are connected by the AccountID key. Security,
SecurityTransactions, and SecurityPrices are connected by the SecurityID
key.

I'm looking to write a SQL statement that will supply the total value of a
client's holdings on a particular day -- as simply as possible.

I've achieved the result using a series of views, but it's slow. I was
hoping someone would help me out with a SQL statement something like this:

SELECT SUM(SecurityTransactions.Shares *
(SELECT MAX(PriceDate) FROM SecurityPrices x WHERE x.PriceDate <=
'4/2/2004' AND x.SecurityID = SecurityPrices.SecurityID) AS
LastSecurityPrice) AS TotalValue
FROM SecurityTransactions INNER JOIN
Account ON
SecurityTransactions.AccountID = Account.AccountID INNER JOIN
SecurityPrices ON
SecurityTransactions.SecurityID = SecurityPrices.SecurityID
WHERE (Accounts.ClientID = 1)

This statement fails at "AS".

Thanks for any help!

--Brent

Jul 20 '05 #2
On Fri, 02 Apr 2004 22:43:56 GMT, "Brent" <bbigler @ ahem-'whoopee' .
com> wrote:
The database has four tables (-> = fields):

Account -> AccountID, ClientID, (others)
Security -> SecurityID, AccountID, (others)
SecurityTransactions -> SecurityID, Shares, (others)
SecurityPrices -> SecurityID, PriceDate, Price, (others)

Account and Security are connected by the AccountID key. Security,
SecurityTransactions, and SecurityPrices are connected by the SecurityID
key.

I'm looking to write a SQL statement that will supply the total value of a
client's holdings on a particular day -- as simply as possible.

I've achieved the result using a series of views, but it's slow. I was
hoping someone would help me out with a SQL statement something like this:

SELECT SUM(SecurityTransactions.Shares *
(SELECT MAX(PriceDate) FROM SecurityPrices x WHERE x.PriceDate <=
'4/2/2004' AND x.SecurityID = SecurityPrices.SecurityID) AS
LastSecurityPrice) AS TotalValue
FROM SecurityTransactions INNER JOIN
Account ON
SecurityTransactions.AccountID = Account.AccountID INNER JOIN
SecurityPrices ON
SecurityTransactions.SecurityID = SecurityPrices.SecurityID
WHERE (Accounts.ClientID = 1)

This statement fails at "AS".

Thanks for any help!

--Brent


As John says, post the table structure in the form as DDL and some
sample data as inserts, so that we can copy&paste it into Query
Analyser and run some tests.

However, if I don't misread your query, you should at the very least
remove "AS LastSecurityPrice". Then, your query will be syntactically
correct. If it does what you want it to is on another card.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #3

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

Similar topics

21
by: John Fabiani | last post by:
Hi, I'm a newbie and I'm attempting to learn howto create a select statement. When I use >>> string1='18 Tadlock Place' >>> cursor.execute("SELECT * FROM mytest where address = %s",string1) All...
0
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the...
3
by: dumbledad | last post by:
Hi All, I'm confused by how to replace a SELECT statement in a SQL statement with a specific value. The table I'm working on is a list of words (a column called "word") with an index int...
4
by: 001 | last post by:
Hello, The select statement needs only 1 second to complete the query. But the update statement spends 30 minutes. Why? SELECT STATEMENT: declare @IDate smalldatetime select @IDate=col001...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
1
by: Grant McLean | last post by:
Hi First a simple question ... I have a table "access_log" that has foreign keys "app_id" and "app_user_id" that reference the "application_type" and "app_user" tables. When I insert into...
6
by: Bob Stearns | last post by:
I am getting duplicate rows back from a select distinct statement of the form: SELECT DISTINCT 'jhough', '000111', t0.bhid FROM (SELECT lots of good stuff) t0 LEFT OUTER JOIN another_table ...
19
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without...
1
by: microsoft.public.dotnet.languages.vb | last post by:
Hi All, I wanted to know whether this is possible to use multiple variables to use in the select case statement such as follows: select case dWarrExpDateMonth, dRetailDateMonth case...
0
FishVal
by: FishVal | last post by:
Hereby I'm proposing a way of convinient work with properties containing SQL Select statements, particulary RowSource property of ComboBox and ListBox. The usual way is the following. Private...
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: 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...
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...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.