简体   繁体   English

如何创建一个 SQL 查询,该查询返回超过一周的列过滤条目?

[英]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:我有一个 data.table 使用 SQL 具有以下相关列:

  • customer_registration: an 8 digit customer code for every customer that is created when they apply to get a product customer_registration:每个客户在申请获取产品时创建的 8 位客户代码
  • state: the state that a customer lives in state:客户所在的state
  • 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 application_date:客户发送订单接收申请的日期,格式为 MM/DD/YYYY
  • product_issued_date: the date that a product a customer applied for was actually sent out to them in MM/DD/YYYY format product_issued_date:客户申请的产品实际发送给他们的日期,格式为MM/DD/YYYY

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目前我的查询成功返回列,除了每个县未发送的申请数量和 state 有客户外,我还可以在其中看到申请总数

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?我的问题:我正在尝试创建一个名为“7 天前已接受未发送的应用程序”的新列,该列提供当前日期前一周已发送应用程序且县和 state 未发送任何产品的应用程序数量. 有没有人对如何做到这一点有任何建议?

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.这里对application_date列的类型有些疑惑:是DateTime还是varchar。 If you ever find yourself worrying about the format a date value with SQL, you've done something horribly wrong with your schema!如果您发现自己担心格式为 SQL 的日期值,那么您的架构就出现了严重错误!

Assuming the correct DateTime option over the wrong and broken varchar option, you can use conditional aggregation .假设正确的DateTime选项超过错误和损坏的varchar选项,您可以使用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: Sql 服务器看起来像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL 如何在 WHERE 子句中添加 3 个或更多条件? - SQL how do I add 3 or more conditions into the WHERE clause? 如何获得 Google Big Query SQL 中整个列的最接近匹配 vlookup? - How do I get the closest match vlookup for entire column in Google Big Query SQL? 在 Athena 中创建视图,但“多次指定列名” - Create View in Athena but "column name specified more than once" SQL 使用子查询条件创建列 - SQL Create a column with conditions from a sub query 如何在 Big Query 中编写多个连接 - how to write more than one joins in Big Query GCP / gcloud:您的应用程序可能没有超过 N 个版本 - 如何自动删除旧版本? - GCP / gcloud : Your app may not have more than N versions - how to remove old versions automatically? 如何提取 0 后的字符串:在大查询 sql - How do I extract the string after 0: in big query sql 如何使用 Redshift 查询编辑器 v2 创建查询计划? - How do I create a schedule for a query using the Redshift Query editor v2? 如何使用 SQL 在表中创建一个新列以仅显示来自另一列的特定数据? - How can I create a new column in a table to show only specific data from another column using SQL? 如何从日期列中包含 Null 的表在 Google Bigquery 中查询? - How do I query in Google Bigquery from a table that has Null in a date column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM