473,395 Members | 1,497 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,395 software developers and data experts.

Complex join problem

Seth Schrock
2,965 Expert 2GB
I have a situation where I'm trying to join a table back to itself to be able to get a record number within a specific month. The problem that I'm running into is when the PK field isn't in the same order as the date field. So for example, lets say that I have the following data:
Expand|Select|Wrap|Line Numbers
  1. AchID_pk,   EffectiveDate
  2. 3385        4/1/2016
  3. 3394        4/7/2016
  4. 23          4/14/2016
  5. 36          4/18/2016
  6. 43          4/21/2016
  7. 61          4/28/2016
I need to know I'm needing to know which record is the 6th one of that month. Normally, I use the PK field in a join to count the number of records with a lower PK value. However, I really need them in date order, but since there can be multiple records on the same date, I can't join using the date field. I tried using both the PK and the date fields in the join, but that didn't work either.

So here is what I have tried with the results (The YearMonth and EffectiveDateFormat fields format the date to be yyyymm so that I can split the count down to a specific month of a year):
Join:
Expand|Select|Wrap|Line Numbers
  1. ON (Main.CustId_pk = qryFileCount.CustId_fk) 
  2. AND (Main.YearMonth = qryFileCount.EffectiveDateFormat) 
  3. AND ((Main.AchFileId_pk >= qryFileCount.AchFileId_pk) 
  4. AND (Main.EffectiveDate >= qryFileCount.EffectiveDate))
Result:
Expand|Select|Wrap|Line Numbers
  1. AchID_pk,   EffectiveDate,   FileCount
  2. 3385        4/1/2016           1
  3. 3394        4/7/2016           2
  4. 23          4/14/2016          1
  5. 36          4/18/2016          2
  6. 43          4/21/2016          3
  7. 61          4/28/2016          4
This is because it is looking for both a lower PK and a lower date.

Join:
Expand|Select|Wrap|Line Numbers
  1. ON (Main.CustId_pk = qryFileCount.CustId_fk) 
  2. AND (Main.YearMonth = qryFileCount.EffectiveDateFormat) 
  3. AND ((Main.AchFileId_pk >= qryFileCount.AchFileId_pk) 
  4. OR (Main.EffectiveDate >= qryFileCount.EffectiveDate))
Result:
Expand|Select|Wrap|Line Numbers
  1. AchID_pk,   EffectiveDate,   FileCount
  2. 3385        4/1/2016           5
  3. 3394        4/7/2016           6
  4. 23          4/14/2016          3
  5. 36          4/18/2016          4
  6. 43          4/21/2016          5
  7. 61          4/28/2016          6
Trying Lower PK OR lower date doesn't work either.

I tried making a calculated field converting the EffectiveDate field to a long and then concatenating the PK value to the end
Expand|Select|Wrap|Line Numbers
  1. CLng(EffectiveDate) & AchId_pk
. At the time I was using the DCount() function instead of the Count() function and the query took forever to run because of all the function calls. I might try it again to see if it works now. Any other suggestions? Basically I want it to look at the EffectiveDate field and then use the lower PK value if there are multiple records with the same date.
Aug 16 '16 #1
5 874
jforbes
1,107 Expert 1GB
I mostly understand what you are doing and it looks like you are using a ranking query of sorts to get your records in order.

It's my understanding that you want the rank to reset for each Month and continue throughout the Month, then I don't think you want this line in the Join (It's a little hard to tell without the entire Select statement, so this is a best guess) as it can cause a reset during the Month:
Expand|Select|Wrap|Line Numbers
  1. ON (Main.CustId_pk = qryFileCount.CustId_fk) 
  2. AND (Main.YearMonth = qryFileCount.EffectiveDateFormat) 
  3. AND ((Main.AchFileId_pk >= qryFileCount.AchFileId_pk) 
  4. AND (Main.EffectiveDate >= qryFileCount.EffectiveDate))
  5.  
Aug 17 '16 #2
Seth Schrock
2,965 Expert 2GB
You are correct that I'm going a ranking query. However, if I remove the PK field from the join, then I get the same rank when there are two records on the same day, which doesn't work for me either.
Aug 17 '16 #3
jforbes
1,107 Expert 1GB
Not to sidetrack you too much, but you might want to try a different approach. If you are really trying to select a record so far into a month, you can use the Top and Order by with a Subquery to attempt to pluck out a single record. I haven't used it much, but it's pretty straight forward SQL.

Basically, create a query that gets selects a recordset upto and including the record you want, then select against that recordset in reverse order and using TOP to grab just the one record. This is a pretty poor example, but it should work as an example only:
Expand|Select|Wrap|Line Numbers
  1. SELECT TOP 1 
  2.   Temp.FileName
  3. , Temp.CreateDate 
  4. FROM (
  5.    SELECT TOP 5 
  6.      Files.FileName
  7.    , Files.CreateDate 
  8.    FROM Files 
  9.    ORDER BY Files.FileName, Files.CreateDate
  10.    ) AS Temp
  11. ORDER BY Temp.FileName DESC , Temp.CreateDate DESC 
It should return the fifth item in the list.
Aug 17 '16 #4
Seth Schrock
2,965 Expert 2GB
I'm not wanting just the one record. I'm wanting all records with their rank. This is for a billing system and I'm generating the invoice and each record is a line item.
Aug 17 '16 #5
Seth Schrock
2,965 Expert 2GB
I ended up adding a field to each subquery to concatenate the long integer value of the EffectiveDate field to the AchFileId_pk field and converted it to a string. There wasn't a big performance hit since I'm doing it in a subquery and not a DCount() function.
Aug 17 '16 #6

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

Similar topics

2
by: michael | last post by:
Gotta post because this is driving me nuts. Trying to DELETE orphans. I can successfully: SELECT GroupID FROM Groups LEFT JOIN Users ON UsersID = UserID WHERE UsersID IS NULL; but when I...
5
by: Marek Kotowski | last post by:
In MySQL online documentation there are some examples with multi-tables left joins. But all of them are like this (taken from the documentation): SELECT ... FROM table1 LEFT JOIN table2 on...
2
by: David Richards | last post by:
Hi, I'm having a problem linking 4 tables in MYSQL. The first table contains customer details, the next item details and the next 2 contain sales information. The final output of the query...
2
by: Bruce Duncan | last post by:
I'm a bit new to MySQL (know MS SQL well...and that may be the problem...getting the syntax confused) and I'm having a join problem...can anyone offer some help? Here's my problem: I have table1...
4
by: Thomas Brown | last post by:
I'm having a bit of a SQL problem and I can't figure it out. I have a working solution, but it could be better. I've got two tables, tblInspections and tblViolations. Each inspection...
8
by: Matt | last post by:
Hello I have to tables ar and arb, ar holds articles and a swedish description, arb holds descriptions in other languages. I want to retreive all articles that match a criteria from ar and...
4
by: Bob Quintal | last post by:
Have two tables linked from SQL server into Access '97, one containing an autonumber primary key and the second table has a text field populated with cstr(). I can create a query that contains...
12
by: info | last post by:
The following query works fine in access and does exactly what I want it to do however I get a syntax error when I port it over to SQL Server 2000. ------------- UPDATE OrdersProducts INNER...
4
by: haryu | last post by:
Max VS Inner Join Problem....this script will show all row in table X2. How to Break it ? ==================== Id Name ==================== 1 a 2 ...
3
by: kolanupaka | last post by:
All, I am novice in SQL/MySQL. I need a query to join two tables say table1 and table2. But the join should happen not based on PK, but based on other columns, which are non-unique. One of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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...
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...

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.