473,799 Members | 3,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Complex Query not showing all Results

6 New Member
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 2471
Stewart Ross
2,545 Recognized Expert Moderator Specialist
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
markcarroll
6 New Member
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_SecuritiesU niverse.Securit yId,
dbo_SecuritiesU niverse.Securit yTicker,
[Copy Of Test_Securities _SixQuarters_Te st_Date1_2].MarketValue,
[Copy Of Test_Securities _SixQuarters_Te st_Date1_2].SharePrice,
[Copy Of Test_Securities _SixQuarters_Te st_Date1_2].SharesHeld,
[Copy Of Test_Securities _SixQuarters_Te st_Date2_2].Held2,
[Copy Of Test_Securities _SixQuarters_Te st_Date3_2].Held3,
[Copy Of Test_Securities _SixQuarters_Te st_Date3_2].Held4P,
[Copy Of Test_Securities _SixQuarters_Te st_Date5_2].Held5,
[Copy Of Test_Securities _SixQuarters_Te st_Date5_2].Held6


FROM

(((dbo_Securiti esUniverse
LEFT JOIN [Copy Of Test_Securities _SixQuarters_Te st_Date1_2] ON dbo_SecuritiesU niverse.Securit yId = [Copy Of Test_Securities _SixQuarters_Te st_Date1_2].SecurityId)
LEFT JOIN [Copy Of Test_Securities _SixQuarters_Te st_Date3_2] ON dbo_SecuritiesU niverse.Securit yId = [Copy Of Test_Securities _SixQuarters_Te st_Date3_2].SecurityId)
LEFT JOIN [Copy Of Test_Securities _SixQuarters_Te st_Date5_2] ON dbo_SecuritiesU niverse.Securit yId = [Copy Of Test_Securities _SixQuarters_Te st_Date5_2].SecurityId)
LEFT JOIN [Copy Of Test_Securities _SixQuarters_Te st_Date2_2] ON dbo_SecuritiesU niverse.Securit yId = [Copy Of Test_Securities _SixQuarters_Te st_Date2_2].SecurityId


WHERE

((([Copy Of Test_Securities _SixQuarters_Te st_Date1_2].SharesHeld) Is Not Null))
OR ((([Copy Of Test_Securities _SixQuarters_Te st_Date2_2].Held2) Is Not Null))
OR ((([Copy Of Test_Securities _SixQuarters_Te st_Date3_2].Held3) Is Not Null))
OR ((([Copy Of Test_Securities _SixQuarters_Te st_Date3_2].Held4P) Is Not Null))
OR ((([Copy Of Test_Securities _SixQuarters_Te st_Date5_2].Held5) Is Not Null))
OR ((([Copy Of Test_Securities _SixQuarters_Te st_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_Te st_Date1_2] ON dbo_SecuritiesU niverse.Securit yId = [Copy Of Test_Securities _SixQuarters_Te st_Date1_2].SecurityId AND [Copy Of Test_Securities _SixQuarters_Te st_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
3957
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 screen. I grabbed this code from the net hoping that I could tweak it for my needs. I'm using MySQL, PHP and IIS and they all are running fine. As the code is, it will display the form, but it won't display my result(s). Any suggestions? Cheers,
9
3138
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 SUBSTRING(ProductName, 1, CHARINDEX('(', ProductName)-2). I can get this result, but I had to use several views (totally inefficient). I think this can be done in one efficient/fast query, but I can't think of one. In the case that one query is not...
4
2156
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 over the years. The idea is that from any widget in the database you can look forward and backward to see different versions of the widget through the years. Here are the tables: WIDGETS widget_id name
14
2469
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 far with a loop in a form. Basically, I have key sums Current savings Current Salary Current deposit amount
0
2217
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 server, which in turn has its tables linked to an Oracle back-end. I'm pretty sure I can take out Access as the middleman by just querying against the Oracle database, but that's not my question.
4
2388
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. The second part is to enter the results and submit them to a MySQL table called results the final part echoes the results. The issue is that my form writes the elements that I called up to the this table before I enter the results and click...
6
1766
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 tLast FROM Results INNER JOIN Ensayos ON Results.idEnsayo=Ensayos.idEnsayo WHERE (Ensayos.Reference=9) GROUP BY Reference, Results.idEnsayo, Num_Taladro;
8
2452
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 which is the primary key. The fields I'm comparing are more than hundred. Also the values are in number but I'm showing them both concatinated in the result sheet to know which value has been replaced by which so for this I have also used cstr...
1
2843
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 using a query rather than scrolling down through 100s of names.Also, I would like the results of the query search to show in the form automatically in fields of 'Student name' and 'Student surname' based on the results.Is there any way I can do...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10249
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10219
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9068
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6804
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4138
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.