Results 1 to 15 of 15
  1. #1
    DaveT99 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2018
    Location
    SPAIN
    Posts
    94

    UNION QUERY How to include to count NULL Values


    I created a UNION query to put the Time Since Last Issue (TSLI) for line items in the warehouse. I counted the number in ranges 0-1 year, 2-3 etc

    I used the SQL format that i had used in the past, but I noted for the 2 of the ranges it gave the wrong count. I then created a simple BETWEEN query which gave the correct answer, so I then copied the WHERE clause and it all worked

    My 1st question is "is my original Union Query format incorrect
    and
    My 2nd Question is how to include a count for the NULL values in the Union Query, there are 14 values ie with no recorded issue

    I have documented the problem as a pdf, I can also upload the access file if needed

    Many Thanks
    Dave
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Use NZ() function?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    ano is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    204
    1st i would try group by with iif to group years to gether.
    2nd between is same as <= and >=
    3rd is null is not hte best way to treat null use function isnull

  4. #4
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Please just copy data from a table/query/spreadsheet and paste into a post. Paste code/sql withing code tags. If anyone wants to test query edits, they can't do much with pictures. Besides, with your link we end up downloading files we don't want or need.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Provide data as a table in post (copy/paste or use Go Advanced editor menu) or attach file.

    Avoid UNION when possible as can slow performance. I don't think need UNION for this calculation. Use Switch() function or a custom VBA function.

    Code:
    Switch(TSLlyr >= 5, "5 to 10", TSLlyr >= 3, "3 to 4.999", TSLlyr >= 2, "2 to 2.999", TSLlyr >= 1, "1 to 1.999", True, "0 to 0.999") & " yrs" AS Rng
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    DaveT99 is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Mar 2018
    Location
    SPAIN
    Posts
    94
    Quote Originally Posted by June7 View Post
    Provide data as a table in post (copy/paste or use Go Advanced editor menu) or attach file.

    Avoid UNION when possible as can slow performance. I don't think need UNION for this calculation. Use Switch() function or a custom VBA function.

    Code:
    Switch(TSLlyr >= 5, "5 to 10", TSLlyr >= 3, "3 to 4.999", TSLlyr >= 2, "2 to 2.999", TSLlyr >= 1, "1 to 1.999", True, "0 to 0.999") & " yrs" AS Rng
    Thank you for all replies, especially June7 for the TIP about not using attachments, for the future and the expression. the Switch function is NEW to me, will google this

    Can I ask what do I enter for the CODE and [/CODE][/QUOTE] in the above switch, I tried TSLIyr for code but obviously got an error. Also how would I include NULL values in the Switch Function

    I have attached the dB as a ZIP to keep the size within the limit

    Many Thanks for the help
    Attached Files Attached Files

  7. #7
    ano is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    204
    Code:
    SELECT STOCK_NO, LAST_ISSUE2, DL_DATE, DL_DATE-LAST_ISSUE2 AS TSLI, TSLI/365 AS TSLIyr, Switch(TSLIyr >= 5,5, TSLIyr >= 3, 4, TSLIyr >= 2, 3, TSLIyr >= 1,2, True, 1) AS rng
    FROM ISSUE_DATA;
    Code:
    SELECT  Switch(rng = 5, "5 to 10", rng = 4, "3 to 4.999", rng=3, "2 to 2.999",rng=2, "1 to 1.999", True, "0 to 0.999") & " yrs" as rang,Count(TSLIyr) AS CountOfTSLIyrFROM grouping
    group by rng ;

  8. #8
    DaveT99 is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Mar 2018
    Location
    SPAIN
    Posts
    94
    Quote Originally Posted by ano View Post
    Code:
    SELECT STOCK_NO, LAST_ISSUE2, DL_DATE, DL_DATE-LAST_ISSUE2 AS TSLI, TSLI/365 AS TSLIyr, Switch(TSLIyr >= 5,5, TSLIyr >= 3, 4, TSLIyr >= 2, 3, TSLIyr >= 1,2, True, 1) AS rng
    FROM ISSUE_DATA;
    Code:
    SELECT  Switch(rng = 5, "5 to 10", rng = 4, "3 to 4.999", rng=3, "2 to 2.999",rng=2, "1 to 1.999", True, "0 to 0.999") & " yrs" as rang,Count(TSLIyr) AS CountOfTSLIyrFROM grouping
    group by rng ;
    many thanks for your response

    For the 2nd SQL statement i get this error message

    "The SELECT statement includes a reserved word or an argument name that is misspelled, or the punctuation is incorrect"

  9. #9
    ano is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    204
    from has no spACE

  10. #10
    DaveT99 is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Mar 2018
    Location
    SPAIN
    Posts
    94
    DO YOU MEAN AS CountOfTSLIyrFROMgrouping AS YOURS WAS AS CountOfTSLIyrFROM grouping

  11. #11
    ano is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    204
    btw june7 did not include the null , easy bec not mention in switch the last (true) should be null if the rest is correct.
    btw advanced shuld include recognize the sql structure and recognize thr from clause

  12. #12
    DaveT99 is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Mar 2018
    Location
    SPAIN
    Posts
    94
    Quote Originally Posted by ano View Post
    btw june7 did not include the null , easy bec not mention in switch the last (true) should be null if the rest is correct.
    btw advanced shuld include recognize the sql structure and recognize thr from clause
    Many Thanks
    Dave

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Need space in front of FROM
    Code:
    AS CountOfTSLIyr FROM grouping
    And yes, any record not captured by the explicit range categories will fall into the last parameter.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    DaveT99 is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Mar 2018
    Location
    SPAIN
    Posts
    94
    Quote Originally Posted by june7 View Post
    need space in front of from
    Code:
    as countoftsliyr from grouping
    and yes, any record not captured by the explicit range categories will fall into the last parameter.
    many thanks june

  15. #15
    ano is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    204
    first after looking at all , i wonder if dl_date in the table should not be date function or form field
    i have optimized the switch from jun7 (perfect option)
    i have changed 1 dl_date lower then the last_issue2
    Code:
    SELECT 
    STOCK_NO, 
    LAST_ISSUE2, 
    DL_DATE, 
    DL_DATE-LAST_ISSUE2 AS TSLI, 
    TSLI/365 AS TSLIyr, 
    Switch( 
    TSLIyr >= 5,5, 
    TSLIyr >= 3, 4, 
    TSLIyr >= 2, 3, 
    TSLIyr >= 1,2, 
    TSLIyr >= 0,1, 
    isnull(TSLIyr),-1, 
    True, 0) AS rng
    FROM ISSUE_DATA ;
    Code:
    SELECT Switch( 
    rng = 5, "5 to 10", 
    rng = 4, "3 to 4.999", 
    rng = 3, "2 to 2.999", 
    rng = 2, "1 to 1.999", 
    rng = 1, "0 to 0.999", 
    rng = 0," ???", 
    true,"null") & " yrs" AS rang, Count(*) AS CountOfTSLIyr
    FROM grouping 
    where rng < 6 
    GROUP BY rng ;

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 23
    Last Post: 07-04-2021, 02:47 AM
  2. How to include null values in query
    By ittechguy in forum Queries
    Replies: 2
    Last Post: 10-20-2015, 04:45 PM
  3. Replies: 2
    Last Post: 10-11-2012, 10:50 PM
  4. Can I count Null values in a totals query?
    By bgephart in forum Queries
    Replies: 2
    Last Post: 08-29-2012, 10:13 AM
  5. Count() and Null Values
    By jpvonhemel in forum Queries
    Replies: 4
    Last Post: 10-21-2011, 03:37 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums