Solved

tsql to Subtract two values

Posted on 2014-01-05
3
368 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 49

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get the latest status 8 32
SQL SERVER 2008 R2 Could not obtain information about Windows NT group/user 5 36
SQL Count issue 24 17
Row_number in SQL 5 10
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

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