0

I have two saved queries in my database called Free_Cash_Flow and Quarterly_Growth_Rates. They are shown here:

Free_Cash_Flow: enter image description here

Quarterly_Growth_Rates: enter image description here

When I use this code:

SELECT * _
FROM Free_Cash_Flow AS C _
INNER JOIN Quarterly_Growth_Rates AS G _
ON (C.Ticker = G.Ticker) AND ((IIF(C.Period = 4, C.Year + 1, C.Year)) = G.Year) AND ((IIF(C.Period = 4, 1, C.Period + 1)) = G.Qtr)

This is returned: enter image description here

The column Free_Cash_Flow is blank. But as you can see in the Free_Cash_Flow table, there is data in that column. Why is is not being pulled into the new table? I run this query with other tables of similar format and everything works great. Any suggestions?

Additional Info

I'd like to add that the Free_Cash_Flow column is populated using an equation in the SQL statement of its respective saved query. I think this may have something to do with my problem. I'm really stuck here and I desperately need to figure this out.

Here is the code containing the SQL statement used for the Free_Cash_Flow saved query:

Variables:

Dim Calculation = “Free_Cash_Flow”

Dim Formula = “(SELECT (SUM(su.Net_Cash_Flow_Operating) - SUM(su.Capital_Expenditures)) _
FROM (SELECT Ticker, [Year], Period, Net_Cash_Flow_Operating, Capital_Expenditures _
FROM Cash_Flow_Statements UNION ALL SELECT Ticker, [Year] + 1, Period - 4, Net_Cash_Flow_Operating, Capital_Expenditures _
FROM Cash_Flow_Statements) su _
WHERE su.Ticker = c.Ticker AND su.[Year] = c.[Year] AND (su.Period Between c.Period - 3 And c.Period))”

Dim Where_Statement = "WHERE i.Period < 5"

SQL Statement:

"CREATE PROC " & Calculation & " AS SELECT i.Ticker, i.[Year], i.Period, " & Formula & " AS " & Calculation & " _
FROM (Income_Statements AS i _
INNER JOIN Balance_Sheets AS b _
ON (i.Ticker = b.Ticker) AND (i.[Year] = b.[Year]) AND (i.Period = b.Period)) _
INNER JOIN Cash_Flow_Statements AS c ON (b.Ticker = c.Ticker) AND (b.[Year] = c.[Year]) AND (b.Period = c.Period) " & Where_Statement & ""
gromit1
  • 577
  • 2
  • 14
  • 36
  • 1
    The key is probably in the SQL statement you used for the `Free_Cash_Flow` saved query. Can you include it in your question? – Twinkles Nov 14 '13 at 12:38
  • @Twinkles I've added code containing the SQL statement I used for the Free_Cash_Flow saved query to my original post. I also think this is they key to my problem. Any thoughts? – gromit1 Nov 14 '13 at 15:15

6 Answers6

1

Dont specifically know why, but using "*" is typically not preferred, but you might have better / proper answer if you explicitly query the named columns, such as

select 
      C.Ticker,
      C.Year,
      C.Period,
      C.Free_Cash_Flow,
      G.Year as GrowthYear,
      G.Period as GrowthPeriod 
   from ....

It MIGHT be getting confused because the table name is the same as the column and ignoring it. By qualifying the columns with your "C" and "G" aliases respectively, might do what you are looking for.

DRapp
  • 47,638
  • 12
  • 72
  • 142
  • I tried your suggestion but I got the same results. I should also mention that `Free_Cash_Flow` and `Quarterly_Growth_Rates` aren't tables, they are saved queries and that the `Free_Cash_Flow` column is populated using an equation in the SQL statement of its respective saved query. I think this may have something to do with my problem. – gromit1 Oct 29 '13 at 19:02
  • I've put a bounty on this question. Would be able to help me anymore on this problem? – gromit1 Nov 08 '13 at 19:32
1

This may have nothing to do with your JOIN. I wonder if Free_Cash_Flow is of a type that OLEDB doesn't understand. It looks like it's in Open Office as a currency value, right? Do you know for a fact that OLEDB can see that value at all? Try a simpler query like

SELECT C.Free_Cash_Flow FROM Free_Cash_Flow AS C

and see if that turns up anything at all.

catfood
  • 4,267
  • 5
  • 29
  • 55
  • That returns the column with the correct data! I'm just using OpenOffice to view the database. Everything is created through my program as an Access file. Any ideas on what my next step should be? – gromit1 Oct 29 '13 at 19:45
  • 2
    I'd add a small bit of complexity at a time, to wit: SELECT * FROM Free_Cash_Flow AS C INNER JOIN Quarterly_Growth_Rates AS G ON C.Ticker = G.Ticker See if that gets you anything like what you expect in the Free_Cash_Flow column. – catfood Oct 29 '13 at 21:04
  • If I take out the JOIN and run `SELECT * FROM Free_Cash_Flow AS C Quarterly_Growth_Rates AS G` the `Free_Cash_Flow` column returns data. As soon as I try and JOIN using anything I run into the same problem of the `Free_Cash_Flow` column being blank. – gromit1 Oct 30 '13 at 13:00
  • I've put a bounty on this question. Would be able to help me anymore on this problem? – gromit1 Nov 08 '13 at 19:32
1

Have you tried changing the last part to a WHERE and see what that returns?

WHERE ((IIF(C.Period = 4, C.Year + 1, C.Year)) = G.Year) AND
      ((IIF(C.Period = 4, 1, C.Period + 1)) = G.Qtr)
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Yosem
  • 4,685
  • 3
  • 22
  • 29
1

You need to specify which column you need when you rename like C & G below:

SELECT C.*, G.* _
FROM Free_Cash_Flow AS C _
INNER JOIN Quarterly_Growth_Rates AS G _
ON (C.Ticker = G.Ticker) AND ((IIF(C.Period = 4, C.Year + 1, C.Year)) = G.Year) AND ((IIF(C.Period = 4, 1, C.Period + 1)) = G.Qtr)
Twinkles
  • 1,984
  • 1
  • 17
  • 31
1

Theoretically, there is no reason why it should not work, save for the fact that the column name is identical to the table name. Perhaps your database engine is confused by this, try renaming.

Twinkles
  • 1,984
  • 1
  • 17
  • 31
  • I've tried renaming it and that does not solve my problem. Also I've run other queries through this with similar naming structures and there is no problem with those. I like your suggestion of the key probably being in the SQL statement I used for the Free_Cash_Flow saved query. I will load that code to my original post. – gromit1 Nov 14 '13 at 14:44
  • 1
    Another possible naming clash: The table alias C is used twice: in your stored query and in the join. – Twinkles Nov 15 '13 at 08:10
  • This may help. If I substitute another saved query for Free_Cash_Flow everything works fine. For example, I have a saved query named Current_Ratio and when I run this code `SELECT * FROM Current_Ratio AS C INNER JOIN Quarterly_Growth_Rates AS G ON (C.Ticker = G.Ticker) AND ((IIF(C.Period = 4, C.Year + 1, C.Year)) = G.Year) AND ((IIF(C.Period = 4, 1, C.Period + 1)) = G.Qtr)` I get data in all of my columns, including one named Current_Ratio. – gromit1 Nov 15 '13 at 13:35
  • This makes me think that the problem lies in my Free_Cash_Flow saved query. – gromit1 Nov 15 '13 at 13:37
  • I switched the possible naming clash so that the table alias C was not used twice and I received the same error. – gromit1 Nov 15 '13 at 14:37
1

Try replacing the table name like below and check whether Quarterly_Growth_Rates column is coming if that column is also not coming then it may be the name identical issue.

SELECT * _
FROM Quarterly_Growth_Rates AS G _ INNER JOIN Free_Cash_Flow AS C _ON (G.Ticker = C.Ticker) AND (G.Year = (IIF(C.Period = 4, C.Year + 1, C.Year))) AND (G.Qtr = (IIF(C.Period = 4, 1, C.Period + 1)))
Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42
sandeep.wuthoo
  • 33
  • 1
  • 1
  • 8
  • I made the change you suggested and the Free_Cash_Flow column is still blank. The Quarterly_Growth_Rates column does contain data so I don't think that it's an name identical issue. – gromit1 Nov 14 '13 at 14:41
  • 1
    @gromit1 Do one thing store the queries data in different temp table and then have a inner join and I am sure you will definitely get the Free_Cash_Flow column data. – sandeep.wuthoo Nov 15 '13 at 06:43
  • What do you mean store the queries data in different temp table and then have a inner join? How exactly would I do this? – gromit1 Nov 15 '13 at 13:22