'My SQL query does not show all results unless prompted by filtering
In my source table called "Input USD", there are the four columns Month (YYYYMM), Global Partner ID 1, Financial Instrument ID and Volume Balances in USD, all of which are short text. I have below piece of code to essentially retrieve the averages (as current month + last month over two) of "Volume Balances in USD" for one specific Partner ID and one specific instrument ID. For example, if, for "Global Partner ID 1" 123, we have populated periods 202102 and 202103 in field "Month (YYYMM)" with USD 1 and USD 2 in the "Volume Balances" field respectively, it will return: 202101 - USD 0, 202102 - USD 0.5, 202103 - USD 1.5 and 202104 - USD 1. Now, when I run the query and export it to Excel, some of the entries go missing and only appear once I filter "Global Partner ID" on one specific ID. So, for above example, 202102 is not in the query result or Excel export, but does show up once I filter on ID 123 in "Global Partner ID 1". It is as though some of the rows don't get triggered to appear unless prompted by filtering. The number of rows stays consistent across the number shown in Access and the row count when I export. But I know that some rows just aren't counted in there and also don't show up if I try to bypass via a create table demand. I am lost as to what could cause the issue (maybe a formatting issue?) and would appreciate any and all help!
SELECT t1.*, (t1.[Volume Balances in USD] + Nz(t2.[Volume Balances in USD])) / 2 AS [Average Volume Balances in USD]
FROM (SELECT * FROM [Input USD]
UNION ALL
SELECT Format(DateAdd('m', 1, CDate(Format(MAX([Month (YYYYMM)]) + '01', '0000-00-00'))), 'yyyymm'), [Global Partner ID 1], [Financial Instrument ID], 0
FROM [Input USD]
GROUP BY [Global Partner ID 1], [Financial Instrument ID]
) AS t1 LEFT JOIN [Input USD] AS t2 ON (t2.[Financial Instrument ID] = t1.[Financial Instrument ID]) AND (t2.[Global Partner ID 1] = t1.[Global Partner ID 1]) AND (DateAdd('m', 1, CDate(Format(t2.[Month (YYYYMM)] + '01', '0000-00-00'))) = CDate(Format(t1.[Month (YYYYMM)] + '01', '0000-00-00')))
ORDER BY t1.[Global Partner ID 1], t1.[Financial Instrument ID], t1.[Month (YYYYMM)];
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
