Oracle SQL:将计数分为唯一列和非唯一列
我有一个如下所示的表:
|FileID| File Info |
| ---- | ------------ |
| 1 | X |
| 1 | Y |
| 2 | Y |
| 2 | Z |
| 2 | A |
我想按 FileID 进行聚合,并将“文件信息”列拆分为 2 个单独的计数列。我希望一列包含唯一文件信息的计数,另一列包含非唯一文件信息的计数。
理想情况下,结果如下所示:
|FileID| Count(Unique)| Count(Non-unique) |
| ---- | ------------ | ----------------- |
| 1 | 1 | 1 |
| 2 | 2 | 1 |
其中非唯一计数是“Y”,唯一计数分别来自 FileID 1 和 2 的“X”和“Z, A”。
我正在寻找方法来衡量文件之间而不是内部的唯一性。
I have a table that looks like this:
|FileID| File Info |
| ---- | ------------ |
| 1 | X |
| 1 | Y |
| 2 | Y |
| 2 | Z |
| 2 | A |
I want to aggregate by FileID and split the File Info column into 2 separate count columns. I want 1 column to have the count of the Unique File Info and the other to be a count of non-Unique file info.
The result would ideally look like this:
|FileID| Count(Unique)| Count(Non-unique) |
| ---- | ------------ | ----------------- |
| 1 | 1 | 1 |
| 2 | 2 | 1 |
where the non-unique count is the 'Y' and the unique count is from the 'X' and 'Z, A' for FileID 1 and 2 respectively.
I'm looking for ways to gauge uniqueness between files rather than within.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在每一行中使用
COUNT()
窗口函数检查FileInfo
是否唯一,然后使用条件聚合来获取您想要的结果:请参阅 演示。
Use
COUNT()
window function in every row to check ifFileInfo
is unique and then use conditional aggregation to get the results that you want:See the demo.
首先,从表中选择“非唯一”行
现在您知道哪些行是唯一的,哪些是非唯一的,您可以保留该表的连接以获取“状态”并对其进行计数。
First you select the "Non Unique" rows from the table
Now that you know which ones are unique and non unique you can left join to that table to get the "status" and count it up.
有一个派生表来计算每个 fileid 的出现次数。
JOIN
和GROUP BY
:Have a derived table that counts occurrences of each fileid.
JOIN
andGROUP BY
: