473,405 Members | 2,261 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,405 software developers and data experts.

Make table from a query

38
Hi All,
I have a query that outputs a "ver_number" unique field and five different date fields (date1, date2, ...date5) that can have null values.
I would like to make a table out of this query that takes the "ver_number" field records and assigns for each "ver_number" a date called "final_date" selected from the five dates in this order:
final_date=date1 if null then
final_date=date2 if null then
final_date=date3 and so on...
if all five dates are null then this ver_number is skipped and not added to the table.
Sep 20 '07 #1
18 1687
mlcampeau
296 Expert 100+
Hi All,
I have a query that outputs a "ver_number" unique field and five different date fields (date1, date2, ...date5) that can have null values.
I would like to make a table out of this query that takes the "ver_number" field records and assigns for each "ver_number" a date called "final_date" selected from the five dates in this order:
final_date=date1 if null then
final_date=date2 if null then
final_date=date3 and so on...
if all five dates are null then this ver_number is skipped and not added to the table.
Try something like this....

Expand|Select|Wrap|Line Numbers
  1. SELECT [Ver_number], IIf([date1] Is Not Null,[date1],IIf([date2] Is Not Null,[date2],IIf([date3] Is Not Null,[date3],IIf([date4] Is Not Null,[date4],IIf([date5] Is Not Null,[date5],""))))) AS Final_Date INTO NewTableName
  2. FROM yourTableName
  3. WHERE (((IIf([date2] Is Not Null,[date2],IIf([date3] Is Not Null,[date3],IIf([date4] Is Not Null,[date4],IIf([date5] Is Not Null,[date5],""))))) Is Not Null));
Sep 20 '07 #2
ADezii
8,834 Expert 8TB
Hi All,
I have a query that outputs a "ver_number" unique field and five different date fields (date1, date2, ...date5) that can have null values.
I would like to make a table out of this query that takes the "ver_number" field records and assigns for each "ver_number" a date called "final_date" selected from the five dates in this order:
final_date=date1 if null then
final_date=date2 if null then
final_date=date3 and so on...
if all five dates are null then this ver_number is skipped and not added to the table.
Expand|Select|Wrap|Line Numbers
  1. SELECT Ver_Number, Date1, Date2, Date3, Date4, Date5, fCalculateFinalDate([Date1],[Date2],[Date3],[Date4],[Date5]) AS Final_Date INTO tblNewFinalDate
  2. FROM tblFinalDate;
Expand|Select|Wrap|Line Numbers
  1. Public Function fCalculateFinalDate(Date_1, Date_2, Date_3, Date_4, Date_5) As Variant
  2. If IsNull(Date_1) Then
  3.   If IsNull(Date_2) Then
  4.     If IsNull(Date_3) Then
  5.       If IsNull(Date_4) Then
  6.         If IsNull(Date_5) Then
  7.           fCalculateFinalDate = Null
  8.         End If
  9.       Else
  10.         fCalculateFinalDate = Date_4
  11.       End If
  12.     Else
  13.       fCalculateFinalDate = Date_3
  14.     End If
  15.   Else
  16.     fCalculateFinalDate = Date_2
  17.   End If
  18. Else
  19.   fCalculateFinalDate = Date_1
  20. End If
  21. End Function
Sep 20 '07 #3
mlcampeau
296 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. SELECT Ver_Number, Date1, Date2, Date3, Date4, Date5, fCalculateFinalDate([Date1],[Date2],[Date3],[Date4],[Date5]) AS Final_Date INTO tblNewFinalDate
  2. FROM tblFinalDate;
Expand|Select|Wrap|Line Numbers
  1. Public Function fCalculateFinalDate(Date_1, Date_2, Date_3, Date_4, Date_5) As Variant
  2. If IsNull(Date_1) Then
  3.   If IsNull(Date_2) Then
  4.     If IsNull(Date_3) Then
  5.       If IsNull(Date_4) Then
  6.         If IsNull(Date_5) Then
  7.           fCalculateFinalDate = Null
  8.         End If
  9.       Else
  10.         fCalculateFinalDate = Date_4
  11.       End If
  12.     Else
  13.       fCalculateFinalDate = Date_3
  14.     End If
  15.   Else
  16.     fCalculateFinalDate = Date_2
  17.   End If
  18. Else
  19.   fCalculateFinalDate = Date_1
  20. End If
  21. End Function
Just out of curiousity ADezii, do you know if your method is more efficient than what I suggested? (I've never actually used a Make Table query until I tested my sql posted above) It just seems like so much more work to get the same result, but if it reduces bloat and is faster, then I could see the point.
Sep 20 '07 #4
fd1
38
Try something like this....

Expand|Select|Wrap|Line Numbers
  1. SELECT [Ver_number], IIf([date1] Is Not Null,[date1],IIf([date2] Is Not Null,[date2],IIf([date3] Is Not Null,[date3],IIf([date4] Is Not Null,[date4],IIf([date5] Is Not Null,[date5],""))))) AS Final_Date INTO NewTableName
  2. FROM yourTableName
  3. WHERE (((IIf([date2] Is Not Null,[date2],IIf([date3] Is Not Null,[date3],IIf([date4] Is Not Null,[date4],IIf([date5] Is Not Null,[date5],""))))) Is Not Null));
Thanks mlcampeau, it did work but one thing it didn't do is ignore the [ver_number] that had all 5 dates null and not add it to the new table.
Sep 20 '07 #5
fd1
38
Thanks ADezii, that worked too
how can I tweak your code to not include the "ver_number" that has all 5 dates null?
Sep 21 '07 #6
fd1
38
ADezii, your code seems to have missed Date5.
All records that have only Date5 as not null are having null Final_Date in the new table
Sep 21 '07 #7
ADezii
8,834 Expert 8TB
Thanks ADezii, that worked too
how can I tweak your code to not include the "ver_number" that has all 5 dates null?
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tblNewFinalDate ( Ver_Number, Date1, Date2, Date3, Date4, Date5, Final_Date )
  2. SELECT Ver_Number, Date1, Date2, Date3, Date4, Date5, fCalculateFinalDate([Date1],[Date2],[Date3],[Date4],[Date5]) AS Final_Date
  3. FROM tblFinalDate
  4. WHERE Date1 Is Not Null OR Date2 Is Not Null OR Date3 Is Not Null OR Date4 Is Not Null OR Date5 Is Not Null;
Sep 21 '07 #8
ADezii
8,834 Expert 8TB
ADezii, your code seems to have missed Date5.
All records that have only Date5 as not null are having null Final_Date in the new table
Expand|Select|Wrap|Line Numbers
  1. Public Function fCalculateFinalDate(Date_1, Date_2, Date_3, Date_4, Date_5) As Variant
  2. If IsNull(Date_1) Then
  3.   If IsNull(Date_2) Then
  4.     If IsNull(Date_3) Then
  5.       If IsNull(Date_4) Then
  6.         If IsNull(Date_5) Then
  7.           fCalculateFinalDate = Null
  8.         Else
  9.           fCalculateFinalDate = Date_5
  10.         End If
  11.       Else
  12.         fCalculateFinalDate = Date_4
  13.       End If
  14.     Else
  15.       fCalculateFinalDate = Date_3
  16.     End If
  17.   Else
  18.     fCalculateFinalDate = Date_2
  19.   End If
  20. Else
  21.   fCalculateFinalDate = Date_1
  22. End If
  23. End Function
Sep 21 '07 #9
NeoPa
32,556 Expert Mod 16PB
Late to the party here, but wouldn't Nz() make life a lot easier for the dates?
Sep 21 '07 #10
mlcampeau
296 Expert 100+
Thanks mlcampeau, it did work but one thing it didn't do is ignore the [ver_number] that had all 5 dates null and not add it to the new table.
Expand|Select|Wrap|Line Numbers
  1. SELECT [Ver_number], IIf([date1] IS NOT NULL,[date1],IIf([date2] IS NOT NULL,[date2],IIf([date3] IS NOT NULL,[date3],IIf([date4] IS NOT NULL,[date4],IIf([date5] IS NOT NULL,[date5],""))))) AS Final_Date INTO NewTableName
  2. FROM Table1
  3. WHERE (((IIf([date2] IS NOT NULL,[date2],IIf([date3] IS NOT NULL,[date3],IIf([date4] IS NOT NULL,[date4],IIf([date5] IS NOT NULL,[date5],Null))))) IS NOT NULL));
This worked in my test database. I changed the "" to Null in the final IIF statement
Sep 21 '07 #11
fd1
38
Expand|Select|Wrap|Line Numbers
  1. Public Function fCalculateFinalDate(Date_1, Date_2, Date_3, Date_4, Date_5) As Variant
  2. If IsNull(Date_1) Then
  3.   If IsNull(Date_2) Then
  4.     If IsNull(Date_3) Then
  5.       If IsNull(Date_4) Then
  6.         If IsNull(Date_5) Then
  7.           fCalculateFinalDate = Null
  8.         Else
  9.           fCalculateFinalDate = Date_5
  10.         End If
  11.       Else
  12.         fCalculateFinalDate = Date_4
  13.       End If
  14.     Else
  15.       fCalculateFinalDate = Date_3
  16.     End If
  17.   Else
  18.     fCalculateFinalDate = Date_2
  19.   End If
  20. Else
  21.   fCalculateFinalDate = Date_1
  22. End If
  23. End Function
Thanks ADezii, that seems to work now
Sep 21 '07 #12
fd1
38
Expand|Select|Wrap|Line Numbers
  1. SELECT [Ver_number], IIf([date1] IS NOT NULL,[date1],IIf([date2] IS NOT NULL,[date2],IIf([date3] IS NOT NULL,[date3],IIf([date4] IS NOT NULL,[date4],IIf([date5] IS NOT NULL,[date5],""))))) AS Final_Date INTO NewTableName
  2. FROM Table1
  3. WHERE (((IIf([date2] IS NOT NULL,[date2],IIf([date3] IS NOT NULL,[date3],IIf([date4] IS NOT NULL,[date4],IIf([date5] IS NOT NULL,[date5],Null))))) IS NOT NULL));
This worked in my test database. I changed the "" to Null in the final IIF statement
Thanks mlcampeauIt, it worked now after adding the condition for date1 in the WHERE clause which was missing in your code.
I like the fact that I have 2 sets of code to work with, that way I'm able to verify results.
Sep 21 '07 #13
ADezii
8,834 Expert 8TB
Thanks ADezii, that seems to work now
Not a problem, glad it's working for you. This is a classic example of more than 1 way to skin an apple. You have 2 diverse approaches which arrive at the same solution via different execution plans. If you are ever working with a large number of Records, you may want to see which approach is faster. My initial guess would be that the Function approach would be significantly faster because of the nature of IIF() being notoriously innefficient, the fact that it evaluates both conditions, and there are simply too many of them in the statement. Do not go by my say so since I am definately prejudiced against certain Functions, notably IIf().
Sep 21 '07 #14
ADezii
8,834 Expert 8TB
Late to the party here, but wouldn't Nz() make life a lot easier for the dates?
Welcome to the party, NeoPa! It probably would make life easier, I just have this thing about using the Nz() Function with Dates.
Sep 21 '07 #15
mlcampeau
296 Expert 100+
Thanks mlcampeauIt, it worked now after adding the condition for date1 in the WHERE clause which was missing in your code.
I like the fact that I have 2 sets of code to work with, that way I'm able to verify results.
Whoops, didn't notice I missed date1! Glad you got it to work anyhow!
Sep 21 '07 #16
NeoPa
32,556 Expert Mod 16PB
Welcome to the party, NeoPa! It probably would make life easier, I just have this thing about using the Nz() Function with Dates.
Oh well, multiple solutions and ideas is always worthwhile anyway, as not everyone can appreciate that mine are always the best :D
BTW Only the VBA IIf() function evaluates both sides. The SQL IIf() function is optimised and doesn't suffer from this performance (and logic) penalty.
Sep 22 '07 #17
ADezii
8,834 Expert 8TB
Oh well, multiple solutions and ideas is always worthwhile anyway, as not everyone can appreciate that mine are always the best :D
BTW Only the VBA IIf() function evaluates both sides. The SQL IIf() function is optimised and doesn't suffer from this performance (and logic) penalty.
Still sticking by the IIF(), are we? (LOL).
Sep 22 '07 #18
NeoPa
32,556 Expert Mod 16PB
Not at all. My solution would use Nz(). However, Michelle's solution would not have suffered by using the IIf() as it was in SQL.
I try to use what is appropriate for the occasion (none of these anti-function prejudices for me :D)
Sep 22 '07 #19

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

Similar topics

1
by: PMB | last post by:
Thank you in advance for any and all assistance. I'm trying to use a make table query to pull the last transactionID, so I can use an append query to reset the transactionID to the next...
4
by: Oreo Bomb | last post by:
I have a secured database that contains a Read-Only group. This group has permissions to view reports, but cannot add, edit, or delete any DB objects. One of the reports the group needs access to...
2
by: Kathy Krizl | last post by:
I'm probably doing something stupid, but I have a make table query. One of the tables I reference has some check box fields in it. Their Data Type is Yes/No, their field property format is Yes/No,...
0
by: LesM | last post by:
This is a change of behaviour between Access 2000 SP3 and Access 2002 SP3. I have Progress table that is linked via ODBC into Access using OpenLink Lite for Progress 9.0b. For over a year, using...
3
by: Hanif Merali | last post by:
Hello, I'm having some difficulties creating a make table query. The source table which I'm basing the make table query has the fields: CIF BusLine1-2001 BusLine1-2002 BusLine1-2003...
7
by: rednexgfx_k | last post by:
All, Problem Summary: I've running about 30 make table queries via VBA in Access 2000, and my database goes from 14,000k to over 2,000,000k. In addition, the longer the procedure runs, the...
24
by: Bob Alston | last post by:
Anyone know a way to make all access to a linked table, in another Access MDB, read only? I really don't want all the hassle of implementing full access security. I can't do this at the server...
1
by: danijela.simunovic | last post by:
Hi! Is there a way that when I run a "make table query" and an "append query" that i won't be asked those 2 or 3 questions from access like :"you are about to run a make table...","the existing...
4
by: ken | last post by:
Hi, I use this command to run a make table query without opening it... CurrentDb.Execute "make table query name" Access tells me that it can't execute a select query...? Its a make table query...
3
by: Robertf987 | last post by:
Hi, I'm a bit stuck with an access database. I'm using access 2000 if that's any help. Right, it's 3:40am right now and I'm rather tired, but I *hope* this makes sense. I have a table which...
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
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,...
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
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,...
0
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...
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...

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.