Solved

tsql to Subtract two values

Posted on 2014-01-05
3
369 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 50

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore 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.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

831 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