
[英]Finding the most frequently-occurring values in a range of values in a table in Oracle
[英]SQL Server : return most frequently occurring values (and counts) from across 9 columns in one table
我有一个表(SQL Server 2005数据库ExpenseFormItems
)是(除其他事项外)横跨11列(存储街道地址fromtravel
, totravel
, totravel1
, totravel2
, totravel3
.... totravel9
)。
基本上,行程总共有11个行程/停靠点,以及Google计算的里程/等(此处不重要)。
我希望能够在所有11列中返回(例如15个)最频繁出现的地址,以及它们出现的次数。
所以基本上
[TO] [Occurrances]
==============
address1 328
address2 233
address3 112
....
address15 23
我猜这里将使用某种类型的un / pivot,但是我从来没有做过任何足以使用过的酷事,所以不了解如何将此情况应用(我从中了解到的内容) 。
TIA
听起来您好像想取消数据处理,将要从列中获取数据并将其转换为行。
基本结构将是:
select col, address
from ExpenseFormItems
unpivot
(
address
for col in (fromtravel, totravel, totravel1,
totravel2, totravel3, totravel4,
totravel5, totravel6, totravel7,
totravel8, totravel9)
) unpiv
然后,如果您要查找出现次数,则可以使用:
select address, count(address) occurrences
from
(
select col, address
from ExpenseFormItems
unpivot
(
address
for col in (fromtravel, totravel, totravel1,
totravel2, totravel3, totravel4,
totravel5, totravel6, totravel7,
totravel8, totravel9)
) unpiv
) d
group by address
order by occurrences desc;
然后,如果您想返回最频繁的15个地址,则可以在SELECT
添加TOP 15
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.