Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need help with a SQL Statement

Posted on 2012-04-06
9
Medium Priority
?
352 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 70

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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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 …
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

618 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