简体   繁体   English

如何编写 SQL 查询来计算一个项目在一周内出现的次数?

[英]How do I write a SQL query to count how many times a week an item shows up in a week?

I'm trying to write a PROC SQL query to count how many times a week a particular object is bought.我正在尝试编写一个 PROC SQL 查询来计算每周购买特定对象的次数。 The issue is, I'm not sure how to code for the 7 day part.问题是,我不确定如何为 7 天部分编码。 I was originally thinking of writing code that looks like this:我原本想写这样的代码:

PROC SQL;
SELECT COUNT(DATE_FILE)
FROM DATA 
WHERE DATE BETWEEN (03-07-2021) and (03-14-2021)
GROUP BY OBJECT 
QUIT;

The thing is, I can't go in and manually look at all of the dates in a week - I need to have the code automatically and accurately account for a week.问题是,我无法进入并手动查看一周内的所有日期 - 我需要让代码自动准确地计算一周。 How should I write this?我应该怎么写这个?

I am not sure if your data looks like this, but you can use week function and also find the beg and end dates to represent a week.我不确定您的数据是否像这样,但是您可以使用周函数并找到代表一周的开始日期和结束日期。 I used sample code:我使用了示例代码:

  data test;
  input items $4. date date9.;
  week_no=week(date);
  format date date9.;
  datalines;
  obj1 1-Jan-21
  obj2 2-Jan-21
  obj1 3-Jan-21
  obj3 4-Jan-21
  obj1 5-Jan-21
  obj2 6-Jan-21
  obj3 7-Jan-21
  obj4 7-Jan-21
  obj1 8-Jan-21
  obj2 9-Jan-21
  obj3 10-Jan-21
  obj1 11-Jan-21
  obj2 12-Jan-21
  obj3 13-Jan-21
  obj1 14-Jan-21
  obj1 15-Jan-21
  obj2 16-Jan-21
  obj3 17-Jan-21
  obj4 18-Jan-21
  obj2 19-Jan-21
  obj3 20-Jan-21
  obj1 21-Jan-21
  obj2 22-Jan-21
  obj1 23-Jan-21
  obj2 24-Jan-21
  obj4 25-Jan-21
  obj4 26-Jan-21
  obj1 27-Jan-21
  obj2 28-Jan-21
  obj4 29-Jan-21
  obj1 30-Jan-21
  obj1 31-Jan-21
  obj4 1-Feb-21
  obj2 2-Feb-21
  obj2 3-Feb-21
  obj3 3-Feb-21
  ;
  run;
  
  data find_week last_of_each_week(keep=week_no beg_end);
  length beg_end $50.;
   set test;
    by week_no;
  
    retain beg_end '';
    if first.week_no then beg_end=put(date, date9.);
    else if last.week_no then beg_end=catx("-",beg_end,put(date, date9.));
    
    output find_week;
    if last.week_no then output last_of_each_week;
  run;
  
  proc sql;
  create table week_data
  as
  select date, items, a.week_no, b.beg_end
  from find_week a
  inner join last_of_each_week b
  on a.week_no=b.week_no;
  quit;
  
  proc sql;
  create table count_obj
  as
  select beg_end, week_no, items, count(items)
  from week_data
  group by beg_end, week_no, items
  order by week_no, items;
  quit;

  proc print data=count_obj; run;

You can see final output by week number, week dates and count of objects.您可以按周数、周日期和对象数查看最终输出。 Output will look like this:输出将如下所示: 样本输出

It might be as simple as the week function :它可能像周函数一样简单:

PROC SQL;
  SELECT week(date,'U') as weeknum, COUNT(DATE_FILE) as per_week
    FROM DATA 
    GROUP BY week(date,'U');  *'u' argument means to start on Sunday;
QUIT;

This doesn't work across years though, so if you have more data, either include also the year function or use intnx .但这并不适用于intnx ,因此如果您有更多数据,也可以包括year 函数或使用intnx

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

相关问题 如果我希望将每月的1号降为T-SQL的1号周,该如何编写查询 - How do I write query if I want 1st day of the month falls in 1st week in T-SQL SQL查询以查找一周内有多少员工加入 - SQL query to find How many employees joined in one week SQL:如何将列周(即第 1 周、第 2 周等)中的值组合到周范围(第 1-2 周或第 1、2 周)? - SQL: How to combine values in column week (i.e. week 1, week 2, etc) into week range (week 1-2 or week 1, 2)? 如何写一个查询显示相关的记录增长,一周又一周? - How to write a query showing associated record growth, week after week? SQL - 了解如何根据名称在列中出现的次数编写案例语句 - SQL - Finding out how to write a case statement depending on how many times a name shows up in a column 如何获得每周显示的计数 - how to get the count showing up iper week SQL查询按条件按周计数 - SQL query count by week with criteria Count() 一个名字与其余信息一起出现在表格中的次数 - Count() how many times a name shows up in a table with the rest of info 如何编写查询以获取一周前在 JPA 中创建的记录 - How do I write query to get record that are created one week ago in JPA 如何按用户级别(仅在过去一周内)从一列中总计“是”? 使用PHP和SQL - How do I total up “yes's” from a column, by a userlevel and only for the past week? Using PHP and SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM