简体   繁体   English

SQL 上个月所有日期的分区

[英]SQL Partition by on all dates of the past month

This is my table:这是我的桌子:

Id ID col山口 old老的 new新的 time时间
39 39 stat统计 A一个 B 2022-01-01 10:21:59 UTC 2022-01-01 10:21:59 UTC
23 23 stat统计 B C C 2022-01-01 18:21:59 UTC 2022-01-01 18:21:59 UTC
68 68 stat统计 B C C 2022-01-02 20:21:59 UTC 2022-01-02 20:21:59 UTC
39 39 stat统计 B C C 2022-01-02 21:21:59 UTC 2022-01-02 21:21:59 UTC

What I want is to get all the ids with new = C as the latest entry for that id for all days ie for 2022-01-01, 2022-01-02, 2022-01-03, ... .我想要的是获取所有带有 new = C 的 id 作为该 id 的最新条目,即 2022-01-01、2022-01-02、2022-01-03、...。 So basically how many Ids had new = C by 2022-01-01, by 2022-01-02, by 2022-01-03, and so on.所以基本上到 2022 年 1 月 1 日,到 2022 年 1 月 2 日,到 2022 年 1 月 3 日,有多少个 ID 有新的 = C,依此类推。 Now in order to get the latest change, I was using:现在为了获得最新的变化,我正在使用:

SELECT Id,time
FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY Id ORDER BY time DESC) as rn 
      FROM table where col = 'stat')
Where new = 'C' and rn = 1

What I can't understand is how to get the change count for every day in the past (starting from a particular day) with that day being the latest change time.我无法理解的是如何获取过去每一天(从特定日期开始)的更改计数,而那一天是最新的更改时间。 So what I want is:所以我想要的是:

time时间 Id ID
2022-01-01 2022-01-01 23 23
2022-01-02 2022-01-02 68 68
2022-01-02 2022-01-02 39 39

Thanks in advance.提前致谢。

EDIT: Ok, I'd not seen Google-BigQuery before, did a quick lookup for a demo, and now I see why you provided the data in the manner that you did.编辑:好的,我之前没见过 Google-BigQuery,快速查找了一个演示,现在我明白了为什么你以你的方式提供数据。 I'm thinking that you could still provide at least a CSV for someone to import but I don't know for sure.我认为您仍然可以提供至少一个 CSV 供某人导入,但我不确定。

First (considering the edit above), I don't know about "Google-BigQuery" and so I don't know if the following will work but this is how I'd do it in SQL Server.首先(考虑到上面的编辑),我不知道“Google-BigQuery”,所以我不知道以下是否可行,但这就是我在 SQL 服务器中的做法。 Perhaps the example can help you science it out in Google-BigQuery, which should have similar capabilities.也许该示例可以帮助您在 Google-BigQuery 中科学地了解它,它应该具有类似的功能。 So far, you hadn't gotten any answers and so I hope this helps a bit.到目前为止,您还没有得到任何答案,所以我希望这会有所帮助。

Second, to help you keep from getting down-votes on your questions and to help us help you better, always post your sample data as "Readily Consumable" data instead of a paste job from a spreadsheet.其次,为了帮助您避免对您的问题投反对票并帮助我们更好地帮助您,请始终将您的示例数据发布为“易于使用”的数据,而不是从电子表格中粘贴作业。 Here's one example of to do that.这是执行此操作的一个示例。 (EDIT: They probably don't have CONVERT but there should be something close and you'll probably need to convert the IIF's to CASE WHEN THEN ELSE END.) (编辑:他们可能没有 CONVERT 但应该有一些接近的东西,你可能需要将 IIF 转换为 CASE WHEN THEN ELSE END。)

 SELECT *
   INTO dbo.SomeTable
   FROM (VALUES
         (39,'stat','A','B',CONVERT(DATETIME,'2022-01-01 10:21:59')) 
        ,(23,'stat','B','C',CONVERT(DATETIME,'2022-01-01 18:21:59'))
        ,(68,'stat','B','C',CONVERT(DATETIME,'2022-01-02 20:21:59'))
        ,(39,'stat','B','C',CONVERT(DATETIME,'2022-01-02 21:21:59'))
        )v(Id,col,old,new,time)
;

And, here's my answer from SQL Server.而且,这是我来自 SQL 服务器的回答。 Again, I don't know if it'll work for you in Google-BigQuery but the should be enough similarities for you to figure it out.同样,我不知道它在 Google-BigQuery 中是否适合你,但应该有足够的相似之处让你弄清楚。

   WITH CTE AS
(
 SELECT [time] = CONVERT(DATE,[time])
        ,Id
        ,RowNum = ROW_NUMBER() OVER (PARTITION BY Id ORDER BY [time] DESC)
   FROM dbo.SomeTable
  WHERE New = 'C'
)
 SELECT *
   FROM CTE
  WHERE RowNum = 1
  ORDER BY [time],Id
;

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

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