Solved

tsql to Subtract two values

Posted on 2014-01-05
3
363 Views
Last Modified: 2014-01-05
I have two selected values as follows:

Returns Price1
    (SELECT ClosePrice FROM StockHist
      WHERE QuoteDate='2014-1-3'      
      AND SymbolID=11818)

Returns Price2  
     (SELECT ClosePrice FROM StockHist
      WHERE QuoteDate='2013-12-30'      
      AND SymbolID=11818)

How do I SELECT Price1 - Price2?

     (SELECT ClosePrice FROM StockHist
      WHERE QuoteDate='2014-1-3'      
      AND SymbolID=11818)
  -
     (SELECT ClosePrice FROM StockHist
      WHERE QuoteDate='2013-12-30'      
      AND SymbolID=11818)

Incorrect syntax near '-'

Thank you
0
Comment
Question by:Dovberman
  • 2
3 Comments
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 300 total points
ID: 39757224
Hi,

pls try

SELECT 

   (SELECT ClosePrice FROM StockHist 
      WHERE QuoteDate='2014-1-3'      
      AND SymbolID=11818)  as p1 
  -
     (SELECT ClosePrice FROM StockHist 
      WHERE QuoteDate='2013-12-30'       
      AND SymbolID=11818) as p2

Open in new window

Regards
0
 

Author Comment

by:Dovberman
ID: 39757233
Thanks for the ideas.

This worked:

SELECT
(
  (
   (SELECT ClosePrice FROM StockHist
      WHERE QuoteDate='2014-1-3'      
      AND SymbolID=11818)  
      -
     (SELECT ClosePrice FROM StockHist
      WHERE QuoteDate='2013-12-30'      
      AND SymbolID=11818)
 
   )
 )  * 12 AS Answer
0
 

Author Closing Comment

by:Dovberman
ID: 39757234
Thank you.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now