Use a cross tab query so you don't need to "hard-wire" Year
and Qtr
values in your Access SQL.
Also I think ExecuteNonQuery
is the wrong method to retrieve data from a SELECT
query. But, that's on the VB.Net side of this task, and I can only help you with Access SQL.
I stored the sample data from your query source in a table named YourQuery.
In Access 2007, the first query below gave me this result set:
Ticker 2013-1 2013-2 2013-3
AAPL -20.07 -10.27 21.46
AMZN 4.07 4.01 12.59
BBBY 14 10.98 4.11
GOOG 10.39 10.74 0.04
V 10.17 7.66 5.13
It differs from your requested output only in the column order. If you want columns in descending time period order, use the second query.
Note also my data source is a table, but yours is a query. I mentioned that difference because it's possible there might a more direct way to get what you need by starting from your query's source table(s) when building the cross tab.
TRANSFORM First(y.Qtr_Growth) AS FirstOfQtr_Growth
SELECT y.Ticker
FROM YourQuery AS y
GROUP BY y.Ticker
PIVOT y.Year & '-' & y.Qtr;
TRANSFORM First(y.Qtr_Growth) AS FirstOfQtr_Growth
SELECT y.Ticker
FROM YourQuery AS y
GROUP BY y.Ticker
ORDER BY y.Year & '-' & y.Qtr DESC
PIVOT y.Year & '-' & y.Qtr;