简体   繁体   中英

How do I create a SQL Query that returns a column filtering entries that are more than a week old?

I have a data.table using SQL that has the following relevant columns:

  • customer_registration: an 8 digit customer code for every customer that is created when they apply to get a product
  • state: the state that a customer lives in
  • county: the county that a customer lives in
  • application_date: the date that a customer sent in an application for an order to be received in MM/DD/YYYY format
  • product_issued_date: the date that a product a customer applied for was actually sent out to them in MM/DD/YYYY format

Currently my query successfully returns columns that where I can see the total amount of applications in addition to the number of unsent applications for each county and state that there are customers

My question: I am trying to create a new column called 'Unsent Apps Accepted 7+ Days Ago' which delivers the number of applications where the application has been sent a week before the current date and no product has been sent out by county and state. Does anyone have any advice on how to do this?

SELECT 
    state,
    county,
    COUNT (CASE WHEN customer_registration IS NOT NULL THEN 1 END) AS "Accepted Apps Total",
    COUNT (CASE WHEN customer_registration IS NOT NULL AND product_issued_date IS NULL THEN 1 END) AS "Unmailed Apps Total", /* below is where I want to write the code to determine unissued products that have been applied for in more than a week */
    COUNT() AS 'Unsent Apps Accepted 7+ Days Ago'
FROM
    public.product_data
GROUP BY 
    state, county
ORDER BY 
    state, county

There's some doubt here about the type of the application_date column: whether it's DateTime or varchar. If you ever find yourself worrying about the format a date value with SQL, you've done something horribly wrong with your schema!

Assuming the correct DateTime option over the wrong and broken varchar option, you can use conditional aggregation . What exactly that looks like depends no which database product you're using (the date functions are a little different in each product). Sql Server would look like this:

SELECT 
   state
   ,county
   ,COUNT (case when customer_registration is not null then 1 end) as "Accepted Apps Total"
   ,COUNT(case when customer_registration is not null and product_issued_date is null then 1 end) as "Unmailed Apps Total" /* below is where I want to write the code to determine unissued products that have been applied for in more than a week */
   ,SUM(CASE WHEN 
            application_date < DATEADD(day, -7, current_timestamp) AND product_issued_date IS NULL 
        THEN 1 ELSE 0 END) as [Unsent Apps Accepted 7+ Days Ago]
FROM public.product_data
GROUP BY state, county
ORDER BY state, county

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM