如何将SQL查询结果按日期和地点进行旋转,以获取最近3个报告日期的数据?

huangapple go评论58阅读模式
英文:

How can I pivot SQL query results by date and location for the last 3 report dates?

问题

Division Site 2023-05-19 2023-05-24 2023-05-26
US South Texas 0 1 0
US South Florida 0 1 0
US South Ohio   0 1 1
US South Kansas 5 0 0
US South Idaho   0 1 0
US South Utah   0 1 0
US South Georgia 0 1 1
英文:

I have table (UserReport), that lists users that are missing company required AD attributes. In this table I have ReportDate column (which is the date of the weekly report I run). I have a different locations field called Site rolls up to Division. I want to give a historical pivot per site for that last 3 reportdates and the counts per site. Below is what I am getting now.

SQL Query Ran

SELECT division, site, reportdate, COUNT(reportdate) as CountUsers 
FROM UserReport
WHERE division = 'US South'
GROUP BY division, site, reportdate

RESULTS

Division Site ReportDate CountUsers
US South Texas 2023-05-24 1
US South Florida 2023-05-24 1
US South Ohio   2023-05-24 1
US South Ohio   2023-05-26 1
US South Kansas 2023-05-19 5
US South Idaho   2023-05-24 1
US South Utah   2023-05-24 1
US South Georgia 2023-05-24 1
US South Georgia 2023-05-26 1

RESULTS that I want

Division Site 2023-05-19 2023-05-24 2023-05-26
US South Texas 0 1 0
US South Florida 0 1 0
US South Ohio   0 1 1
US South Kansas 5 0 0
US South Idaho   0 1 0
US South Utah   0 1 0
US South Georgia 0 1 1

答案1

得分: 0

你的枢轴方法中缺少一些内容:拆分日期成为你需要的三个日期。一旦你拆分了这些值,你可以应用条件聚合,然后你会得到你的输出。

在某列没有任何非空值的情况下,使用 SUM 替代 COUNT 可以避免问题。

SELECT division, 
       site, 
       SUM(CASE WHEN reportdate = '2023-05-19' THEN 1 ELSE 0 END) AS date1, 
       SUM(CASE WHEN reportdate = '2023-05-24' THEN 1 ELSE 0 END) AS date2,
       SUM(CASE WHEN reportdate = '2023-05-26' THEN 1 ELSE 0 END) AS date3
FROM UserReport
WHERE division = 'US South'
GROUP BY division, site

这应该可以,考虑到你指出你只想要最后三个日期。

尽管如果将来你需要在日期上进行通用化处理,那么你将需要一个动态查询,这会影响性能并且有一些与之相关的问题。进行这样的查询还需要了解你当前使用的数据库管理系统,因为每个数据库管理系统都使用不同的动态查询语法。

英文:

You're missing something into your pivot approach: splitting the date in the three dates you need. Once you split those values, you can apply conditional aggregation, and you'll get your output.

Using the SUM in place of the COUNT could avoid you issues, in case one column does not have any non-null value.

SELECT division, 
       site, 
       SUM(CASE WHEN reportdate = '2023-05-19' THEN 1 ELSE 0 END) AS date1, 
       SUM(CASE WHEN reportdate = '2023-05-24' THEN 1 ELSE 0 END) AS date2,
       SUM(CASE WHEN reportdate = '2023-05-26' THEN 1 ELSE 0 END) AS date3
FROM UserReport
WHERE division = 'US South'
GROUP BY division, site

This should be good, given that you indicated you want only those last three dates.

Although if in future you would require to generalize on dates, then you would need a dynamic query, which is heavier in performance and has some issues tied to it. Doing such query also would require knowledge ofthe DBMS you're currently using, as each DBMS employs different syntax for dynamic querying.

huangapple
  • 本文由 发表于 2023年5月25日 00:19:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定