【发布时间】:2021-06-20 18:46:06
【问题描述】:
我想在我的表中添加 4 个额外的列(右下方 4 个)。
- 统计“设备+日期”的出现次数
- 在与行中的“源”值匹配的分隔列中
这是最后2列的Excel公式,我该如何用SQL编写逻辑?
S1_count_in_the_date:
=COUNTIFS(F:F,F2,A:A,"S1")
--countifs(device+date column, device+date value, source column, "S1")--
S2_count_in_the_date:
=COUNTIFS(F:F,F2,A:A,"S2")
--countifs(device+date column, device+date value, source column, "S2")--
我对 S1_count_in_the_date 的 SQL 尝试:
COUNT(source+device+date) OVER (PARTITION BY device, date)
| source | device | date | ... | source+device+date | device+date | S1_count_in_the_date | S2_count_in_the_date |
|---|---|---|---|---|---|---|---|
| S1 | A | 2021-01-03 | … | S1A2021-01-03 | A2021-01-03 | 1 | 2 |
| S2 | A | 2021-01-03 | … | S2A2021-01-03 | A2021-01-03 | 1 | 2 |
| S2 | A | 2021-01-03 | … | S2A2021-01-03 | A2021-01-03 | 1 | 2 |
提前致谢
【问题讨论】:
-
向我们展示你在 SQL 中的最佳尝试......并澄清你的数据,我不明白你为什么拥有你所拥有的价值观。
-
COUNTIFS公式是否仅适用于第 2 行,换句话说:第 3 行是否为=COUNTIFS(F:F,F3,A:A,"S1")?这不工作COUNT(*) OVER (PARTITION BY device, date, source)?
标签: sql sql-server tsql count