Please note this is regarding a homework problem, the homework is done and works to the spec, but it’s slow so I’m trying to lessen how many SQL calls I make. And I’m still learning my SQL-fu.
My table looks like:
Ticker |Name |Industry
A |Agilent Technologies |Information Technology
AA |Alcoa Inc |Materials
AAPL |Apple Inc. |Information Technology
Right now I am running a SQL statement like this in a loop. Each iteration I change which industry to look at.
SELECT ticker FROM company WHERE industry = 'Information Technology'
But that means I have to make a call to the database for each industry just to get the tickers related to it. What I want is to make one database call that returns multiple results. So the first result would be a list of all the tickers for the first industry, the second result would be all the tickers for the second industry etc.
I tried this SQL, but it just gave me one ticker for each industry, not a full list
SELECT ticker, industry FROM company GROUP BY industry
Unfortunately, you are probably doing it the best way if you need to have separate result sets for each industry. However, if you want to do a little manipulation in your code, you could return the entire table ordered by the industry then the ticker. Then in your code you could read until the industry changed and then do whatever magic you want and then read again until the industry changed again. Basically, do your loop on the result set instead of using your loop to make database calls. If you did it this way, your query would look like this:
You would make one call to the database and do your looping through the results in your code (or wherever you are working on your data).