在列列表中使用子查询

发布于 2024-11-08 00:51:13 字数 282 浏览 0 评论 0原文

我想创建一个查询来计算过去 7 天、14 天和 28 天创建的记录数。我的结果将返回类似以下内容的内容:

7Days  14Days  28Days  
21     35      56

我知道如何在每个时间(例如 7 天)内执行此操作,但我是否可以在一个查询中捕获所有三个?

select count(*) from Mytable
where Created > DATEADD(day,-8, getdate()) 

I would like to create a query that would count how many records were created in the last 7, 14 and 28 days. My result would return something like:

7Days  14Days  28Days  
21     35      56

I know how to for each timepsan e.g. 7 days, but I do I capture all three in one query?

select count(*) from Mytable
where Created > DATEADD(day,-8, getdate()) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风筝有风,海豚有海 2024-11-15 00:51:14

也不漂亮,但不依赖子查询(表/列名称来自 AdventureWorks)。如果 case 语句符合您的条件,则返回 1,否则返回 0 - 那么您只需对结果求和:

select sum(case when datediff(day, modifieddate, getdate()) <= 7
                then 1 else 0 end) as '7days',
       sum(case when datediff(day, modifieddate, getdate()) > 7
                     and datediff(day, modifieddate, getdate()) <= 14
                then 1 else 0 end) as '14days',
       sum(case when datediff(day, modifieddate, getdate()) > 14
                     and datediff(day, modifieddate, getdate()) <= 28
                then 1 else 0 end) as '28days'
from sales.salesorderdetail

编辑: 更新了 datediff 函数 - 按照其编写方式,它将返回负数(假设moddeddate 是过去的)导致所有项目都属于第一种情况。感谢 Andriy M 指出了这一点

Also not pretty, but doesn't rely on subqueries (table/column names are from AdventureWorks). The case statement returns 1 if it falls within your criteria, 0 otherwise - then you just sum the results :

select sum(case when datediff(day, modifieddate, getdate()) <= 7
                then 1 else 0 end) as '7days',
       sum(case when datediff(day, modifieddate, getdate()) > 7
                     and datediff(day, modifieddate, getdate()) <= 14
                then 1 else 0 end) as '14days',
       sum(case when datediff(day, modifieddate, getdate()) > 14
                     and datediff(day, modifieddate, getdate()) <= 28
                then 1 else 0 end) as '28days'
from sales.salesorderdetail

Edit: Updated the datediff function - the way it was written, it would return a negative number (assuming modifieddate was in the past) causing all items to fall under the first case. Thanks to Andriy M for pointing that out

慕巷 2024-11-15 00:51:14

它不是世界上最漂亮的代码,但它确实有效。尝试从三个子查询中进行选择,每个子查询对应一个范围。

select * from 
(select COUNT(*) as Cnt from    Log_UrlRewrites where CreateDate >= DATEADD(day, -7, getdate())) as Seven
inner join (select  COUNT(*) as Cnt from    Log_UrlRewrites where CreateDate >= DATEADD(day, -14, getdate())) as fourteen on 1 = 1
inner join (select  COUNT(*) as Cnt from    Log_UrlRewrites where CreateDate >= DATEADD(day, -28, getdate())) as twentyeight on 1 = 1

It isn't the prettiest code in the world, but it does the trick. Try selecting from three subqueries, one for each range.

select * from 
(select COUNT(*) as Cnt from    Log_UrlRewrites where CreateDate >= DATEADD(day, -7, getdate())) as Seven
inner join (select  COUNT(*) as Cnt from    Log_UrlRewrites where CreateDate >= DATEADD(day, -14, getdate())) as fourteen on 1 = 1
inner join (select  COUNT(*) as Cnt from    Log_UrlRewrites where CreateDate >= DATEADD(day, -28, getdate())) as twentyeight on 1 = 1
谢绝鈎搭 2024-11-15 00:51:14
select
(
  select count(*)
  from Mytable
  where Created > DATEADD(day,-8, getdate())
) as [7Days],
(
  select count(*)
  from Mytable
  where Created > DATEADD(day,-15, getdate())
) as [14Days], 
(
  select count(*)
  from Mytable
  where Created > DATEADD(day,-29, getdate())
) as [28Days] 
select
(
  select count(*)
  from Mytable
  where Created > DATEADD(day,-8, getdate())
) as [7Days],
(
  select count(*)
  from Mytable
  where Created > DATEADD(day,-15, getdate())
) as [14Days], 
(
  select count(*)
  from Mytable
  where Created > DATEADD(day,-29, getdate())
) as [28Days] 
梦回旧景 2024-11-15 00:51:14
SELECT
  [7Days]  = COUNT(CASE UtmostRange WHEN  7 THEN 1 END),
  [14Days] = COUNT(CASE UtmostRange WHEN 14 THEN 1 END),
  [28Days] = COUNT(CASE UtmostRange WHEN 28 THEN 1 END)
FROM (
  SELECT
    *,
    UtmostRange = CASE
      WHEN Created > DATEADD(day,  -8, GETDATE()) THEN  7
      WHEN Created > DATEADD(day, -15, GETDATE()) THEN 14
      WHEN Created > DATEADD(day, -29, GETDATE()) THEN 28
    END
  FROM Mytable
) s
SELECT
  [7Days]  = COUNT(CASE UtmostRange WHEN  7 THEN 1 END),
  [14Days] = COUNT(CASE UtmostRange WHEN 14 THEN 1 END),
  [28Days] = COUNT(CASE UtmostRange WHEN 28 THEN 1 END)
FROM (
  SELECT
    *,
    UtmostRange = CASE
      WHEN Created > DATEADD(day,  -8, GETDATE()) THEN  7
      WHEN Created > DATEADD(day, -15, GETDATE()) THEN 14
      WHEN Created > DATEADD(day, -29, GETDATE()) THEN 28
    END
  FROM Mytable
) s
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文