?
Solved

Need help with a SQL Statement

Posted on 2012-04-06
9
Medium Priority
?
346 Views
Last Modified: 2012-06-22
I need to display an average in one collumn.  The SQL executes, but the average collumn is null.

SELECT Top(12) SYM.SymbolID,SYM.SymbolName,
      100 * (MAX(HIST.ClosePrice) -  MIN(HIST.ClosePrice)) / (SELECT ClosePrice
      FROM StockHist
      WHERE  
    QuoteDate = '2012-02-25'  
      AND SymbolID=SYM.SymbolID ) as PctChange -- Problem is here
      , MAX(HIST.ClosePrice) -  MIN(HIST.ClosePrice) AS Change
      , AVG(HIST.ClosePrice) AS AvgClose  

      FROM StockHist AS HIST INNER JOIN
    Symbol AS SYM ON HIST.SymbolID = SYM.SymbolID
      WHERE
      SYM.MarketID = 1
      AND HIST.ClosePrice > HIST.ClosePricePrev
      AND HIST.ClosePrice >=5  AND HIST.ClosePrice <=45
      AND HIST.QuoteDate >= '2012-02-25'
      AND HIST.QuoteDate <= '2012-4-05'
      GROUP BY SYM.SymbolID,SYM.SymbolName,SYM.SecName  
      Having count(*) > 1
      ORDER BY (AvgClose) DESC


SymbolID  SymbolName       PctChangeChange      Avg Close
5943      DORM            NULL      0.21      44.825000
6132      FCHI            NULL      0.38      44.793333
7524      ROLL            NULL      0.44      44.720000
6598      ITIC            NULL      1.39      44.663333
5848      CVCO            NULL      0.37      44.635000
5792      CRMT            NULL      0.84      44.484000

PctChange should be Change/Avg Close ie. (0.21/44.825) * 100
What should I change in the SQL Statement?
Thanks,
0
Comment
Question by:Dovberman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 10

Assisted Solution

by:plummet
plummet earned 400 total points
ID: 37816993
Hi,

What values (if any) are returned if you display instead of using the following in the calculation?

SELECT ClosePrice
      FROM StockHist
      WHERE  
    QuoteDate = '2012-02-25'  
      AND SymbolID=SYM.SymbolID

ie can you execute something like this to show all the values returned:

SELECT Top(12) 
	SYM.SymbolID,
	SYM.SymbolName,
	MAX(HIST.ClosePrice) as MAXClose,
	MIN(HIST.ClosePrice) as MINClose,
	(SELECT ClosePrice 
		FROM StockHist 
		WHERE  QuoteDate = '2012-02-25'  
		AND SymbolID=SYM.SymbolID ) as ShowClosePrice,
	100 * (MAX(HIST.ClosePrice) -  MIN(HIST.ClosePrice)) / (SELECT ClosePrice 
      			FROM StockHist 
      			WHERE  
    			QuoteDate = '2012-02-25'  
    			AND SymbolID=SYM.SymbolID ) as PctChange -- Problem is here
      , MAX(HIST.ClosePrice) -  MIN(HIST.ClosePrice) AS Change 
      , AVG(HIST.ClosePrice) AS AvgClose   

      FROM StockHist AS HIST INNER JOIN
    Symbol AS SYM ON HIST.SymbolID = SYM.SymbolID 
      WHERE 
      SYM.MarketID = 1 
      AND HIST.ClosePrice > HIST.ClosePricePrev 
      AND HIST.ClosePrice >=5  AND HIST.ClosePrice <=45
      AND HIST.QuoteDate >= '2012-02-25' 
      AND HIST.QuoteDate <= '2012-4-05' 
      GROUP BY SYM.SymbolID,SYM.SymbolName,SYM.SecName  
      Having count(*) > 1 
      ORDER BY (AvgClose) DESC 

Open in new window

0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 400 total points
ID: 37817034
have you tried :

 , AVG(isnull(HIST.ClosePrice, 0)) AS AvgClose
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 1200 total points
ID: 37817044
Feb 25 was a Saturday.

Instead of this:

(SELECT ClosePrice
      FROM StockHist
      WHERE  
    QuoteDate = '2012-02-25'  
      AND SymbolID=SYM.SymbolID )

Should it be this:

(SELECT ClosePrice
      FROM StockHist
      WHERE  
    QuoteDate = HIST.QuoteDate
      AND SymbolID=SYM.SymbolID )
0
Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

 

Author Comment

by:Dovberman
ID: 37817202
To Plummet.

SymbolID SymbolName Max and MinClose ShowClosePrice PctChange     Change    AvgClose
5943      DORM            44.93      44.72          NULL                     NULL         0.21         44.825000
6132      FCHI            44.97      44.59          NULL                     NULL         0.38         44.793333
7524      ROLL            44.94      44.50          NULL                     NULL         0.44         44.720000
6598      ITIC            45.00      43.61          NULL                     NULL         1.39         44.663333
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 37817209
Close is null, so AVG(Close) = Null
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 37817215
forget that I was misaligned
0
 

Author Comment

by:Dovberman
ID: 37817216
To emereau

<<AvgClose>>

AvgClose is already correct.

I need the PctChange.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 37817231
I would do it with a sub-select. what is your last version of this query?
0
 

Author Comment

by:Dovberman
ID: 37817276
To ScottPletcher

Yes, 02/25/2012 is a Saturday.  The market is closed.
My database correclty reflects this by having no row for that date.

Changed to Monday '2012-02-27' .  
All is well now.
                                    PctChange                AvgChange  AvgClosePrice
5943      DORM      0.45132172791      0.21      44.825000
6132      FCHI      0.79514542791      0.38      44.793333
7524      ROLL      0.93597106998      0.44      44.720000
6598      ITIC      3.09576837416      1.39      44.663333

Thanks,
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

764 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