473,408 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Complex Query not showing all Results

I have a rather complicating query (the SQL is about a page long) so I hope I can solve this without needing to get into specifics.

Basically, the database I am working on has information about investment firms, and what companies they hold shares of. I set up the query to find all the stocks that a specified company held for the most recent date. But we need to display data for the past dates as well.

The way I got the most recent date to display was to make a seperate query that found the max date. I then put that query into the main query and joined that query with the 'filing date' field in the 'Holdings' table that has the share information.

I have been able to get the query to display other dates as well by putting the 'holdings' table in again (So now there is a 'holdings' 'holdings_2', 'holdings_3', etc), and joining them to their respecive dates the same way as I did the original 'holdings' table.

Everything looked like it worked fine as I could then make the query display back multiple dates. But then I realized that it only displayed information if the investment firm held stocks in that company on the most recent date (original 'holdings' table). Sometimes, an institution will have owned the stock for several quarters, but then sell it all off so that they currently own nothing. If that is the case, then the query is not displaying that stock at all. What I want would be for it to show the stock in the results, with appropriate totals for each date.

Lets say that I am looking at the shares of ABC and XYZ companies that an institution hold. If the institution held 100 shares of ABC on Date1 and 200 shares on Date2, then everything comes out correctly. However if the institution owned 200 shares of XYZ on Date2 and zero on Date1, then XYZ will not appear in the final query at all. (Had they owned XYZ on Date1 instead of Date2, then it shows up). The problem might have something to do with the 'holdings' table having no row with XYZ and Date1.

So basically, how do I make the query show results that may have null or zero values in some of the dates but have values for different dates?
Apr 11 '08 #1
2 2449
Stewart Ross
2,545 Expert Mod 2GB
Hi Mark. On the face of it, it sounds like you need to consider the joins between your stock holding table and your institution table and use a left-join between the stock holding and the institution at some point (a left-join includes all instances of the originating table, not just those where there is equality in the joined fields with whatever they are joined to).

However, in multi-table queries the introduction of left (or right) joins has to be done consistently; if one join is done this way all joins will have to follow suit, as Access will protest at inconsistent join types. I break down complex queries into simpler segments, using left-joins sparingly in separate queries which feed into others that are inner-joined as normal.

There is a HowTo article by our highly experienced admin contributor NeoPa on SQL Joins, linked here for reference.

It is difficult to advise further on this without seeing the SQL concerned, or knowing what the structure of the underlying tables is. I think there may be some issues relating to how the query is currently structured - very long SQL is often very difficult to read, understand and maintain, and tends to suggest structural difficulties in the underlying tables or what is being attempted.

-Stewart
Apr 12 '08 #2
Thanks for the help. I have been able to make a lot of progress with the LEFT JOIN feature. However, I run into a problem.

When I use the LEFT JOIN, I end up having a whole bunch of rows with no information in them on the report. So I add a where clause with: sharesheld is not null or sharesheld2 is not null etc. This makes it actually display the correct results, but it takes a LONG time to run the query. Here is my SQL:

SELECT

dbo_SecuritiesUniverse.SecurityId,
dbo_SecuritiesUniverse.SecurityTicker,
[Copy Of Test_Securities_SixQuarters_Test_Date1_2].MarketValue,
[Copy Of Test_Securities_SixQuarters_Test_Date1_2].SharePrice,
[Copy Of Test_Securities_SixQuarters_Test_Date1_2].SharesHeld,
[Copy Of Test_Securities_SixQuarters_Test_Date2_2].Held2,
[Copy Of Test_Securities_SixQuarters_Test_Date3_2].Held3,
[Copy Of Test_Securities_SixQuarters_Test_Date3_2].Held4P,
[Copy Of Test_Securities_SixQuarters_Test_Date5_2].Held5,
[Copy Of Test_Securities_SixQuarters_Test_Date5_2].Held6


FROM

(((dbo_SecuritiesUniverse
LEFT JOIN [Copy Of Test_Securities_SixQuarters_Test_Date1_2] ON dbo_SecuritiesUniverse.SecurityId = [Copy Of Test_Securities_SixQuarters_Test_Date1_2].SecurityId)
LEFT JOIN [Copy Of Test_Securities_SixQuarters_Test_Date3_2] ON dbo_SecuritiesUniverse.SecurityId = [Copy Of Test_Securities_SixQuarters_Test_Date3_2].SecurityId)
LEFT JOIN [Copy Of Test_Securities_SixQuarters_Test_Date5_2] ON dbo_SecuritiesUniverse.SecurityId = [Copy Of Test_Securities_SixQuarters_Test_Date5_2].SecurityId)
LEFT JOIN [Copy Of Test_Securities_SixQuarters_Test_Date2_2] ON dbo_SecuritiesUniverse.SecurityId = [Copy Of Test_Securities_SixQuarters_Test_Date2_2].SecurityId


WHERE

((([Copy Of Test_Securities_SixQuarters_Test_Date1_2].SharesHeld) Is Not Null))
OR ((([Copy Of Test_Securities_SixQuarters_Test_Date2_2].Held2) Is Not Null))
OR ((([Copy Of Test_Securities_SixQuarters_Test_Date3_2].Held3) Is Not Null))
OR ((([Copy Of Test_Securities_SixQuarters_Test_Date3_2].Held4P) Is Not Null))
OR ((([Copy Of Test_Securities_SixQuarters_Test_Date5_2].Held5) Is Not Null))
OR ((([Copy Of Test_Securities_SixQuarters_Test_Date5_2].Held6) Is Not Null));
What I want to do is add the "Is Not Null" into the LEFT JOIN statement. But if I do that, access creates an error. Apparnelty, this is just an access limitation. Is there any way to make a work around?

IE I want

LEFT JOIN [Copy Of Test_Securities_SixQuarters_Test_Date1_2] ON dbo_SecuritiesUniverse.SecurityId = [Copy Of Test_Securities_SixQuarters_Test_Date1_2].SecurityId AND [Copy Of Test_Securities_SixQuarters_Test_Date1_2].SharesHeld Is Not Null
For each of the LEFT JOIN statements.

The only thing I can think of is somehow putting the query onto SQL server. The problem is that the queries on the right side need to be in access as I use criteria in them based on Access Reports. So any other help would be much appreciated.
Apr 23 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mike Cocker | last post by:
Hello, I'm quite weak at PHP, so I was hoping to get some help understanding the below code. First off, I'm trying to create a "query form" that will allow me to display the results on my...
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
4
by: Diamondback | last post by:
I have two tables, WIDGETS and VERSIONS. The WIDGETS table has descriptive information about the widgets while the VERSIONS table contains IDs relating to different iterations of those widgets...
14
by: Crimsonwingz | last post by:
Need to calculate a sum based on a number of factors over a period of years. I can use formula ^x for some of it, but need totals to carry over in the sum and have only been able to do this thus...
0
by: schan | last post by:
Hi there, I was wondering if someone could shed some light on a problem I have no idea on how to fix. I created an Excel Add-In that uses an ADO connection to an Access database on a file...
4
SHOverine
by: SHOverine | last post by:
I have a 3-part form that I am having trouble with. First part is to select the user group and the week and year that I want to submit results for, this calls the elements that I want to update. ...
6
by: Jon Bilbao | last post by:
I´m trying a select clause in two steps because it´s too complex. First: SELECT Reference, Results.idEnsayo, Results.Num_taladro, min(Results.dTime) + 500 AS tIni, max(Results.dTime) - 500 AS...
8
by: msabbasi | last post by:
Hi everyone! I have two tables in my database, named 'n' and 'o' ... n for new and o for old. I have to compare the two tables to c if any value has been changed in any cell for a given cell Id...
1
by: igor221189 | last post by:
Hello everyone. I have Access 2000 database which holds student records in the school.It stores subject grades for each student.In the 'Student Grade Form', I would like to search student surname...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.