Assistance Needed for SQL Code

Assistance Needed for SQL Code

Hi Team -

I am having difficulty getting a UNION ALL query to work correctly.

For the first part of my query, I need to returns all requests that were created in the previous month, which I tried like this with no luck:

  1. SELECT
          "Site"
        , "Request Custom FIelds"."Request Type" AS "Request Type"
        , "Sub Category"
        , "Request Status"
        , "Requester"
    FROM "Request"
    INNER JOIN "Request Custom Fields" ON "Request Custom FIelds"."RequestID" = "Request"."RequestID"
    WHERE "Request"."Created Time" = last_nmonth("Request"."Created Time", 1)

It's returning entries back since inception, which is wrong. It needs to return all entries from the PREVIOUS month no matter what day I am in in the CURRENT month.

And then the UNION ALL needs to be similar as the above with a different WHERE clause.  This piece should return all entries that have a created time OLDER than the previous month AND have been closed in the previous month

OR

have a created time OLDER than the previous month have not be closed  or resolved yet.

  1. UNION ALL
    SELECT
          "Site"
        , "Request Custom FIelds"."Request Type" AS "Request Type"
        , "Sub Category"
        , "Request Status"
        , "Requester"
    FROM "Request"
    INNER JOIN "Request Custom Fields" ON "Request Custom FIelds"."RequestID" = "Request"."RequestID"
    WHERE ("Request"."Created Time" OLDER than previous month AND closed/resolved within the PREVIOUS month)
        OR "Request"."Created Time" OLDER than the previous month and "Request Status" not in ('Closed', 'Resolved')

Can somone help me with that?

UPDATE: 2-24-2019 5:58PM

I think I figured it out:
  1. SELECT
             "Site",
             "Request"."RequestID",
             "Request Custom FIelds"."Request Type" AS "Request Type",
             "Sub Category",
             "Request Status",
             "Requester",
             "Created Time",
             "Completed Time",
             "Resolved Time"
    FROM  "Request" JOIN "Request Custom Fields" ON "Request Custom FIelds"."RequestID"  = "Request"."RequestID" 
    WHERE     "Request"."Created Time"  >= previous_nmonth(subdate(Current_Date(), day(Current_Date()) -1), 1)
     AND    "Request"."Created Time"  < subdate(Current_Date(), day(Current_Date()) -1)
    UNION ALL
     SELECT
             "Site",
             "Request"."RequestID",
             "Request Custom FIelds"."Request Type" AS "Request Type",
             "Sub Category",
             "Request Status",
             "Requester",
             "Created Time",
             "Completed Time",
             "Resolved Time"
    FROM  "Request" JOIN "Request Custom Fields" ON "Request Custom FIelds"."RequestID"  = "Request"."RequestID" 
    WHERE     ("Request"."Created Time"  < previous_nmonth(subdate(Current_Date(), day(Current_Date()) -1), 1)
     AND    "Request"."Completed Time"  < subdate(Current_Date(), day(Current_Date()) -1)
     AND    "Request"."Completed Time"  > subdate(Current_Date(), day(Current_Date()) -2))
     OR    ("Request"."Created Time"  < previous_nmonth(subdate(Current_Date(), day(Current_Date()) -1), 1)
     AND    "Request"."Resolved Time"  < subdate(Current_Date(), day(Current_Date()) -1)
     AND    "Request"."Resolved Time"  > subdate(Current_Date(), day(Current_Date()) -2))
     OR    ("Request"."Created Time"  < previous_nmonth(subdate(Current_Date(), day(Current_Date()) -1), 1)
     AND    "Request"."Request Status"  NOT IN ( 'Closed'  , 'Resolved'  ))
     



    Access your files securely from anywhere

        Zoho Developer Community




                                  Zoho Desk Resources

                                  • Desk Community Learning Series


                                  • Digest


                                  • Functions


                                  • Meetups


                                  • Kbase


                                  • Resources


                                  • Glossary


                                  • Desk Marketplace


                                  • MVP Corner


                                  • Word of the Day



                                      Zoho Marketing Automation
                                              • Sticky Posts

                                              • Announcing Zoho Analytics 6.0 Beta!

                                                We are delighted to open up the next major version, Zoho Analytics 6.0 Beta! The new version comes packed with a wide range of functionalities for all persona, namely business users, data analysts, data engineers, and data scientists. Zoho Analytics team
                                              • What's New in Zoho Analytics - August 2024

                                                Hello Users! We are back with the latest updates and enhancements made to Zoho Analytics. Keep reading to learn more about them. Connect to the data hosted in the cloud without allow-listing the IP addresses Utilize Zoho Databridge to connect to the data
                                              • We are coming to your city! Zoho Analytics Community Meetup

                                                Hello, business leaders and data enthusiasts! We are delighted to announce that registrations are now open for the ZUG meetups, and we can't wait for you to be a part of them. Our in-house analytics experts are geared up to lead discussions on constructing
                                              • Zoho Analytics: 2021 Look Back

                                                As we start a new year in 2022, here's some of our top moments from 2021. Zoho Analytics in 2021
                                              • [Customer Talk] PREMO Group's Analyst Interview at Zoho Day 2022

                                                Premo Group, a 50 year old Spanish Manufacturing Company, has been our long-standing customer with #ZohoAnalytics. They've been using our platform for their end-to-end, unified business analytics solution.  Hear more from Claudio Cabeza, Director at PREMO


                                              Manage your brands on social media



                                                    Zoho TeamInbox Resources

                                                      Zoho DataPrep Resources



                                                        Zoho CRM Plus Resources

                                                          Zoho Books Resources


                                                            Zoho Subscriptions Resources

                                                              Zoho Projects Resources


                                                                Zoho Sprints Resources


                                                                  Qntrl Resources


                                                                    Zoho Creator Resources



                                                                        Zoho Campaigns Resources


                                                                          Zoho CRM Resources

                                                                          • CRM Community Learning Series

                                                                            CRM Community Learning Series


                                                                          • Kaizen

                                                                            Kaizen

                                                                          • Functions

                                                                            Functions

                                                                          • Meetups

                                                                            Meetups

                                                                          • Kbase

                                                                            Kbase

                                                                          • Resources

                                                                            Resources

                                                                          • Digest

                                                                            Digest

                                                                          • CRM Marketplace

                                                                            CRM Marketplace

                                                                          • MVP Corner

                                                                            MVP Corner





                                                                              Design. Discuss. Deliver.

                                                                              Create visually engaging stories with Zoho Show.

                                                                              Get Started Now