Solved

Need help with a SQL Statement

Posted on 2012-04-06
9
342 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 100 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 100 total points
ID: 37817034
have you tried :

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

Accepted Solution

by:
Scott Pletcher earned 300 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

726 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