Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL adding 2 query's to make one

This is the data I’m working with in my SQL database:

Symbol | Close_Below
  AAPL       1        
  TSLA       0                
  AAPl       0
  AAPL       1        
  SPY        0                
  TSLA       1      
  SPY        0                
  AAPL       1 

SQL Query I’m using to count how many times a symbol is in the database:

SELECT Symbol, count(*) as SymbolCount FROM data GROUP BY Symbol;

Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Symbol | SymbolCount 
  AAPL       4        
  TSLA       2                
  SPY        2

Another SQL Query I’m using to sum all the 1’s for each symbol:

SELECT  Symbol, SUM(Close_Below) as Close_Below_Sum FROM data GROUP BY Symbol;

Output:

Symbol | Close_Below_Sum 
  AAPL        3        
  TSLA        1                
  SPY         0

How would you go about getting the query to show the percentage of how many 1’s to the symbol count for each symbol? Example below.

Desired Output:

Symbol | Perc
  AAPL    0.75        
  TSLA    0.50                
  SPY     0.00

>Solution :

SELECT SYMBOL, AVG(Close_Below) AS PERC FROM DATA GROUP BY SYMBOL;

Output:

SYMBOL  PERC
AAPL    0.75
SPY     0.00
TSLA    0.50
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading